Java Interview Assault Series (13): MySQL read-write separation and master-slave delay

MySQL read-write separation and master-slave delay

Interview questions

  • How to realize the separation of reading and writing in MySQL?
  • What is the principle of MySQL master-slave replication?
  • How to solve the delay problem of mysql master-slave synchronization?

Preface

This, at this stage of high concurrency, it must be separated from reading and writing. What does it mean? Because in fact, most Internet companies, some websites, or apps, actually read more and write less. Therefore, in response to this situation, it is to write a master library, but the master library hangs multiple slave libraries, and then reads from multiple slave libraries, can't it support higher read concurrency pressure?

How to realize the separation of reading and writing in MySQL?

In fact, it is very simple. It is based on the master-slave replication architecture. Simply put, we build a master library and hang multiple slave libraries, and then we just write the master library, and then the master library will automatically synchronize the data to the slave libraries. Under normal circumstances, the main library can hang 4-5 slave libraries

01_Why does MySQL need to separate read and write?

What is the principle of MySQL master-slave replication?

There is a concept in MySQL called binlog log, which is the operation that changes data for each addition, deletion and modification operation. In addition to updating data, this addition, deletion and modification operation will also write a log file to record the log of this operation.

The main library writes the changes to the binlog log, and then after the slave library connects to the main library, the slave library has an IO thread that copies the binlog log of the main library to its local and writes it to a relay log. Then there is a SQL thread from the library that reads the binlog from the relay log, and then executes the content in the binlog log, that is, executes the SQL again locally, so that you can ensure that the data in the main library is the same.

There is a very important point here, that is, the process of synchronizing data from the master library from the slave library is serialized, that is to say, parallel operations on the master library will be executed serially on the slave library. So this is a very important point. Due to the characteristics of the slave database copying logs from the master database and executing SQL serially, in a high concurrency scenario, the data of the slave database will be slower than the master database, and there is a delay. Therefore, it often appears that the data just written into the main library may not be read, and it may take tens of milliseconds or even hundreds of milliseconds to read it.

And there is another problem here, that is, if the main library suddenly goes down, and then the data has not been synchronized to the slave library, then some data may not be available on the slave library, and some data may be lost.

So mysql actually has two mechanisms in this area, one is semi-synchronous replication, which is used to solve the problem of data loss in the main database; the other is parallel replication, which is used to solve the problem of master-slave synchronization delay.

This so-called semi-synchronous replication, semi-sync replication, refers to that after the master database writes the binlog log, it will be forced to synchronize the data to the slave database at this time, and after the slave database writes the log to its own local relay log, then An ack will be returned to the main library, and the main library will consider the write operation to be completed after receiving at least one ack from the slave library.

The so-called parallel replication refers to opening multiple threads from the library, reading the logs of different libraries in the relay log in parallel, and then replaying the logs of different libraries in parallel, which is library-level parallelism.

  • Principle of master-slave replication
  • The cause of the master-slave delay problem
  • The problem of data loss in master-slave replication and the principle of semi-synchronous replication
  • The principle of parallel replication, multi-database concurrent replay of relay logs, alleviating the problem of master-slave delay

02_MySQL master-slave replication principle

MySQL master-slave synchronization delay problem (emphasis)

We have indeed dealt with online bugs and small production accidents caused by the delay of master-slave synchronization.

show status, Seconds_Behind_Master, you can see that the data of the master library from the library is a few ms behind

In fact, we often encounter this piece of stuff. For example, after using the mysql master-slave architecture, you may find that the data just written to the library is not found, and the result is finished. . . .

So in fact, you have to consider the scenarios in which you should use this mysql master-slave synchronization. The suggestion is to generally use mysql master-slave synchronization when reading far more than writing, and when the data timeliness requirements are not so high when reading.

So at this time, one thing we can consider is that you can use mysql parallel replication, but the problem is that it is library-level parallelism, so sometimes it is not very useful

So this time. . Generally speaking, we will use the method of compulsory reading of the main library for the kind of scenes that must be guaranteed to be found immediately after writing, so that you can be sure that you can read the data. It is no problem to use some database middleware.

Generally speaking, if the master-slave delay is more serious

  • Sub-library, split a main library into 4 main libraries, the write concurrency of each main library is 500/s, at this time, the master-slave delay can be ignored
  • Turn on the parallel replication supported by mysql, and replicate multiple libraries in parallel. If the write concurrency of a certain library is extremely high, and the write concurrency of a single library reaches 2000/s, parallel replication is still meaningless. The 28 rule, for example, in many cases, is a few order tables written at 2000/s, and dozens of other tables at 10/s.
  • Students who rewrite the code and write the code should be cautious. At that time, we actually asked that student to rewrite the code for a short period of time. After inserting the data, we will update it directly without querying.
  • If it does exist, it must be inserted first, and the query will be found immediately upon request, and then some operations will be performed in reverse immediately to set the direct connection to the main database for this query. This method is not recommended. If you do so, the meaning of separation of reading and writing will be lost.

03_Problems in the production environment caused by the MySQL master-slave delay

Guess you like

Origin blog.csdn.net/weixin_43314519/article/details/109823155