Interview 39 MySQL separate read and write

(1) how to read and write mysql separation?

 

In fact, very simple, is based on master-slave replication architecture, in short, to engage in a main library, hanging more from the library, and then we just simply write the main library and the main library will automatically sync to the data to go from the library.

 

(2) MySQL master-slave replication principle is valid?

 

The main library written binlog change log, and then after connecting from the library to the main library, the library has an IO thread, the main library binlog log to your local copy, written in a relay log. Then there is the library from a SQL thread reads the relay logs from binlog, and then execute binlog contents of the log, that is, in their own local SQL execution once again, so that you can ensure that the data yourself with the main library is the same.

 

There is a very important point is that the process of database synchronization from the main library data is serialized, that is to say the parallel operation of the main library, the library will be executed from the serial. So this is a very important point, because the implementation of SQL features from the main library as well as a copy of the log from the library serial, under high concurrency scenarios, from the library's main library data would certainly be slower, there is a delay. So often, the data just written to the primary database might not be read, to over tens of milliseconds, or even hundreds of milliseconds to read.

 

And there's another problem, that is, if the primary database goes down suddenly, and not just the data synchronized to the library, some data may not be from the library, some data may be lost.

 

So mysql actually in this one there are two mechanisms, a semi-synchronous replication, the main library used to solve data loss problems; a parallel copy, master-slave synchronization to solve the delay problem.

 

This so-called semi-sync, semi-sync copy, refers to the primary database binlog after writing the log, this time will be immediately forced to synchronize data from the library, from the library to write the log after their local relay log, then returns an ack to the main library, main library receives at least one from the library would think after ack write operation is complete.

 

The so-called parallel copying, referring to the open library from multiple threads, parallel read relay log log different libraries, and then replay the logs in parallel in different libraries, which is the library-level parallelism.

Principle 1) copy of the master-slave

Reason 2) The main problems arising from the delay

3) loss of data copied from the main issue, as well as the principle of semi-synchronous replication

4) Copy of the principle of parallel, concurrent multi-library replay relay logs from the main alleviate delays

 

(3) mysql master-slave synchronization delay problem (cream)

 

Online since bug actually treated master-slave synchronization delay problems, leading to the line, small accidents

 

show status, Seconds_Behind_Master, you can see the main library to copy data from the library behind a few ms

 

In fact, we often encounter this stuff, you use the example after mysql master-slave architecture, may find just the result of the write data warehousing not found, the results will be ruined. . . .

 

So good in fact you should consider using this mysql master-slave synchronization, the general advice is to read much more than write and read time when the general time-critical data is not so high, mysql master-slave synchronization with what scene down

 

So this time, a thing we can consider is that you can use a parallel copy of mysql, but the problem is that the library-level parallelism, so sometimes the role is not large

 

So this time. . Normally, after we wrote for that can be found immediately necessary to ensure the scene, by way of compulsory reading the main library, so you can guarantee you certainly can read the data of the bar. In fact, with some database middleware is not the problem.

 

Generally, the more serious if the delay from the master

 

1, sub-libraries, one master library split into four main library, concurrent writes to each of the main library 500 / s, this time delay from the master negligible

2, open parallel copy mysql support, multiple parallel library copy, if concurrent writes a library is particularly high, single library to write concurrent reached the 2000 / s, parallel to copy or meaningless. 28 law, very often say, is that the few orders table, written in 2000 / s, dozens of other tables 10 / s.

3, rewrite the code, students write the code, be careful when we are actually short term is to get the students to rewrite the code, and then insert the data directly on the update, do not query

If there is indeed a need to insert, immediately requested to inquire into, then we must immediately turn to do something, set directly connected to the main database query. Do not recommend this method, you do so results in meaning to read and write separation is lost

Guess you like

Origin www.cnblogs.com/xiufengchen/p/11259314.html