Detailed analysis of MySQL master-slave delay phenomenon and principle

Detailed analysis of MySQL master-slave delay phenomenon and principle

1. Phenomenon

In the early morning, an index was added to an online table. The amount of data in the table was too large (more than 100 million data, more than 50G of data), causing a delay of several hours between the master and slave. The systems that depended on the slave database were unable to query the data, ultimately affecting the business.

Now let's sort out the principle of master-slave delay.

2. Principle

According to the description in the MySQL official document MySQL Replication Implementation Details, MySQL master-slave replication relies on three threads: one thread for master ( Binlog dump thread ) and two threads slave ( I/O thread and SQL thread ). The master-slave replication process is as follows:

When the master server and the slave server are connected, create Binlog dump thread to send bin log data:

  • One Binlog dump thread corresponds to one slave server;
  • Binlog dump thread will lock the data when getting it from bin log and release the lock immediately after getting the data.

When the slave server receives the START_SLAVE command, it creates I/O thread and SQL thread :

  • I/O thread reads events from the master in a pull manner and stores them in relay log of the slave server;
  • SQL thread reads events from relay log and executes them;
  • slave can read and update data at its own pace and can also operate the replication process (start and stop) at will.

Note: After the START_SLAVE command successfully starts the thread, if the subsequent I/O thread or SQL thread stops for some reason, there will be no warning and the business party will not be aware of it. You can view the slave's error log or the SHOW SLAVE STATUS command to view the thread status on the slave.

You can view the thread status through SHOW PROCESSLIST:

Binlog dump thread:

mysql> SHOW PROCESSLIST\G
*************************** 1. row ***************************
  Id: 2
 User: root
 Host: localhost:32931
  db: NULL
Command: Binlog Dump
 Time: 94
 State: Has sent all binlog to slave; waiting for binlog to
   be updated
 Info: NULL

I/O thread and SQL thread:

mysql> SHOW PROCESSLIST\G
*************************** 1. row ***************************
  Id: 10
 User: system user
 Host:
  db: NULL
Command: Connect
 Time: 11
 State: Waiting for master to send event
 Info: NULL
 *************************** 2. row ***************************
  Id: 11
 User: system user
 Host:
  db: NULL
Command: Connect
 Time: 11
 State: Has read all relay logs; waiting for the slave I/O
   thread to update it
 Info: NULL

Analysis

According to the above principle, since slave reads data with a single thread ( I/O thread ) and updates data with a single thread ( SQL thread ), and master writes data with multiple threads, as long as master writes more frequently than slave reads and updates, there may be a master-slave delay, such as:

  1. master write tps is higher, greater than slave update speed;
  2. slave takes a long time to execute certain statements, such as holding locks;
  3. When master executes certain DDL statements, the execution time is longer, and slave also executes them in the same amount of time;

An index is created here. After consulting the DBA, it is found that the generated bin log file is over 100 GB. The data volume is too large, causing the slave library I/O thread to keep reading the bin log events generated by DDL operation, which affects the update of normal business DML events, resulting in master-slave synchronization delay.

4. Solution

From the perspective of the causes of master-slave delay, the solution can be started from the following directions:

  1. Business selection: For architectures that cannot tolerate slave database delays, you can choose a distributed architecture to avoid slave database delay issues.
  2. Execution time: For online DDL operations on large tables, try to choose early morning hours when the business volume is low.
  3. Hardware configuration, upgrade the slave hardware configuration, such as SSD
  4. Reduce requests, increase cache layers, and reduce read request drops

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • MySQL master-slave replication delay causes and solutions
  • Troubleshooting of master-slave delay issues when upgrading MySQL 5.6 to 5.7
  • Causes and solutions for MySQL master-slave synchronization delay
  • Analysis and solution of MYSQL master-slave asynchrony delay principle
  • Detailed explanation of reducing MySQL master-slave data synchronization delay
  • In-depth explanation of MySQL master-slave replication delay problem
  • Solution to MySQL master-slave delay problem

<<:  Ubuntu installs scrcpy to complete mobile phone screen projection and control (another way to use QQ WeChat in Ubuntu)

>>:  Solve the cross-domain problem of Vue+SpringBoot+Shiro

Recommend

Master the CSS property display:flow-root declaration in one article

byzhangxinxu from https://www.zhangxinxu.com/word...

Summary of the understanding of virtual DOM in Vue

It is essentially a common js object used to desc...

JS implements random roll call system

Use JS to implement a random roll call system for...

Nine advanced methods for deduplicating JS arrays (proven and effective)

Preface The general methods are not listed here, ...

Detailed explanation of three ways to wrap text in el-table header

Table of contents Problem Description Rendering T...

MySQL 8.0.20 Installation Tutorial with Pictures and Text (Windows 64-bit)

1: Download from mysql official website https://d...

CSS3 uses scale() and rotate() to achieve zooming and rotation

1. scale() method Zoom refers to "reducing&q...

Detailed explanation of desktop application using Vue3 and Electron

Table of contents Vue CLI builds a Vue project Vu...

Two ways to implement text stroke in CSS3 (summary)

question Recently I encountered a requirement to ...

Markup language - simplified tags

Click here to return to the 123WORDPRESS.COM HTML ...

Causes and solutions for MySQL deadlock

The database, like the operating system, is a sha...

Application scenarios and design methods of MySQL table and database sharding

Many friends have asked in forums and message are...

Using js to implement a number guessing game

Last week, the teacher gave me a small homework, ...

HTML table markup tutorial (1): Creating a table

<br />This is a series of tutorials provided...