MySQL Interview - separate read and write

 

MySQL Interview - separate read and write

 

Interview questions

You do not have separate read and write MySQL? How to read and write separation of MySQL? MySQL master-slave replication principle is valid? How to solve the delay problem MySQL master-slave synchronization?

Interviewer psychological analysis

High concurrency at this stage, certainly need to do is to read and write separation, What do you mean? Because in fact most Internet companies, some websites or app, they are actually reading and writing less. So for this case, is to write a main library, but more from the main library hanging library and read from the library from multiple, it can not support concurrent read more pressure yet?

Face questions analysis

How to read and write separation of MySQL?

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.

MySQL master-slave replication principle is valid?

The main library is 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, write a relay 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.

mysql-master-slave

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, is there a delay of. 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 is actually in this one there are two mechanisms, one is semi-synchronous replication , the main library used to solve data loss problems; one is parallel to copy , master-slave synchronization to solve the delay problem.

This so-called semi-synchronous replication , also known as  semi-sync replication, refers to the main library after writing binlog log, it will be forced after this time will be immediately synchronized to the data from the library, from the library to log write their own local relay log, then will returns an ack to the main library, main library receives at least one from the library after the ack will consider a write operation is completed.

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

MySQL master-slave synchronization delay problem (essence)

Indeed treated previously online bug online master-slave synchronization delays because of problems caused by, is a small accidents.

What is this scene. One student is to write the code logic. A first insert data, then check it out, and then update this data. In a production environment peak, concurrent writes reached 2000 / s, this time, the master-slave replication latency is probably small in tens of milliseconds. Online will find that there are always some data every day, we expect to update the status of some important data, but no update at peak times. User feedback with the customer, and the customer service will give us feedback.

We MySQL command:

show status

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

In general, if the master is more serious from the delay, the following solution:

  • Sub-library, a master library into multiple master database, the master database for each concurrent writes is reduced several times when the primary delay from negligible.
  • Open MySQL supports parallel replication, multiple libraries parallel copy. If concurrent write a library is particularly high, single library to write concurrent reached the 2000 / s, parallel to copy or meaningless.
  • Rewrite the code, write code classmates, be careful, the query may not find out immediately when data is inserted.
  • If there is indeed a need to insert, immediately requested to inquire into, then we must immediately turn to do something for this query set up direct main library . I do not recommend this method, so if you do, read and write separation of significance is lost.

 

Reprinted Source: https://github.com/doocs/advanced-java/blob/master/docs/high-concurrency/mysql-read-write-separation.md

Guess you like

Origin www.cnblogs.com/leeego-123/p/11588814.html