[Operation and Maintenance Interview] Interviewer: How to deal with mysql master-slave delay

Preface

Operation and maintenance interview questions about mysql, the most common one is mysql master-slave synchronization. A little bit deeper about master-slave synchronization is how the mysql master-slave delay is generated and how to solve it.

Internet companies at the current stage generally read more and write less. One master library is equipped with several slave libraries to read data, reducing the pressure of high concurrent reading.

About mysql delay

There are many reasons for the mysql master-slave delay, one of the most important is the delay in high concurrency scenarios.
The slave database needs to copy logs from the master database and execute SQL serially, so in a high concurrency scenario, the data of the slave database will be slower than the master database, and there will often be newly written data, which cannot be found on the slave database , It takes tens of milliseconds to read, sometimes even hundreds of milliseconds.

mysql replication is divided into semi-synchronous replication and parallel replication, these two methods are actually to solve the problem of data loss and master-slave synchronization delay.

Semi-synchronous replication:

After the master library writes the binlog log, it will force the data to be synchronized to the slave library immediately. After the slave library writes the log to its own local relay log, it will give a response to the master library, and the master library will receive at least one response from the slave library. Only later will the writing be considered complete.

Parallel replication:

Start multiple threads from the library, read the logs of different libraries in the relay log in parallel, and then replay the logs of different libraries in parallel, which is library-level parallelism.

The cause of the master-slave delay

  • Master-slave delay caused by network jitter
  • There is a large transaction on the main library, which causes the delay of the slave library
  • There are slow query statements, lock waiting and other reasons on the main library, and there are more slave libraries
  • A large transaction occurred from the library, blocking the operation of all subsequent transactions
  • The amount of concurrency from the library is relatively large, causing delays
  • The binlog format does not set the row format

Master-slave delay solution

The general troubleshooting idea is to first check whether it is network jitter and external reasons. If it is not an external reason, check the main library, and the main library will check the slave library if there is no problem.

Introduce several common solutions to master-slave delay:

  • Sub-library, split a main library into multiple main libraries, after the write concurrency of each main library is reduced, the delay is reduced
  • Open the parallel replication of mysql, and replicate multiple libraries in parallel.
  • Rewrite the code, do not query immediately after writing the code, it may not be possible to query.
  • Improve the hardware configuration of the slave library (there are many companies whose configuration is not high)
  • Set the slave library to read-only mode
  • Turn on 5.7 Enhanced semi-synchronization to avoid data loss
  • Open binlog raw format

One note: From the library raid card, set the write strategy to write back.

to sum up

For the interview, you can just say a few words from it, because the proficiency of operation and maintenance of the database is not as bad as the operation and maintenance oneself imagined, and it is not as good as the company imagined.
So if you encounter database problems during the interview, try your best to guide yourself.

Guess you like

Origin blog.csdn.net/xinshuzhan/article/details/108533780