当前位置:网站首页>MySQL master-slave synchronization and master-slave synchronization delay solution
MySQL master-slave synchronization and master-slave synchronization delay solution
2022-07-26 05:09:00 【On knowledge】
author : On knowledge ,CSDN Contracted lecturer ,CSDN Force author , High quality creators in the back-end field , Love to share and create
official account : On knowledge
Be good at the field : Back end full stack engineer 、 Reptiles 、ACM Algorithm
Contact information vx:zsqtcc
The problem of this exploration :
- What is? Mysql Master slave synchronization ?
- Mysql Why does master-slave synchronization have master-slave delay ?
- Master slave synchronization delay solution ?
Take it all for him this time
Why? Master slave synchronization It will expose problems ? Although master-slave synchronization meets the performance requirements , But consistency may be problematic .
Here comes the main dish
Mysql Master slave synchronization is ?
Because of the massive growth of data access , The primary key of a single database is a little hard , Write data with master database , The master-slave architecture of reading data from the library, which separates reading and writing, has come into being .
Common master-slave synchronization includes , A master from , One master, many followers , Multi master and one slave , The Lord and from much , This time take one master and many followers as examples .
Master slave synchronization principle
Two important documents are involved
- binlog( Binary log file )
- relay log( Relay log file )

Master slave synchronization principle master slave synchronization process
- The master database writes the changes of data in the database to binlog
- From the library to the main library
- From the library, a I/O The thread requests an update from the main library binlog
- The main library will create a binlog dump Thread to send binlog , From the library I/O The thread is responsible for receiving
- From library I/O The thread will receive binlog Write to relay log in .
- From library SQL Thread reads relay log Synchronize data locally ( That is, do it again SQL )
Why is there a master-slave synchronization delay ?
Warm reminder : There are two main reasons for this
Random replay
MySQL Master slave replication is a single threaded operation , The main library is for all DDL and DML The generated log is written into binlog, because binlog It's written in sequence , So it's very efficient .Slave Of SQL Thread Thread will be the main library of DDL and DML Operation events are in slave Replay in .DML and DDL Of IO The operation is random , It's not sequential , It's a lot more expensive . therefore SQL Thread The speed of the thread cannot catch up with that of the main library binlog The speed of , There will be a master-slave delay
Lock wait
On the other hand , because SQL Thread It's also single-threaded , When the concurrency of the main database is high , Produced DML More than slave Of SQL Thread The speed that can be handled , perhaps When slave There are big ones in the middle query Statement creates a lock wait Then the delay is created .
Master slave synchronization delay solution
Forced read main database
If you are doing a business that requires high real-time performance such as payment , Then the most direct way is to read the main library directly , Of course, this method is equivalent to the function of making a backup from the Library .
Delayed reading
After writing , Wait a while before reading ,Eg: The time of synchronization after writing is 0.5s, When reading, you can set 1s Read later , But the main problem of this scheme is , I don't know the time required for master-slave synchronization .
Reduce concurrency
If you understand the reason why random replay causes master-slave delay , Then it's easier to understand , Control the speed of main library writing , The probability of master-slave delay is naturally small .{ reason : Because in the main library sql May execute concurrently , The concurrency speed can be controlled }.
Parallel replication
Compared with the above three , A relatively good solution .
SQL The speed of single thread replay is limited , So can we use multithreading to replay it ?
MySQL 5.6 After version , Provides a way to replicate in parallel , By way of SQL Thread to multiple work Thread to replay , This solves the problem of master-slave delay .
summary
Common master-slave synchronization delay solutions :
Forced read main database
Delayed reading
Reduce concurrency
Parallel replication ( recommend )
Liangwen recommends
Distributed ID Common solutions for - Take it down
边栏推荐
- C语言函数
- Embedded sharing collection 21
- SAP报表开发步骤
- unity场景跳转脚本
- [pytorch] install torch 1.8.1 and check whether torch version and GPU are available
- “双碳”目标下资源环境中的可计算一般均衡(CGE)模型实践技术
- Excel VBA:将多个工作表保存为新文件
- Test of countlaunch demo
- 【Leetcode】493. Reverse Pairs
- Molecular skeleton transition tool -delinker introduction
猜你喜欢

普林斯顿微积分读本02第一章--函数的复合、奇偶函数、函数图像

Redis expiration deletion strategy and memory obsolescence strategy

注解@Autowired如何自动装配

Principle of image nonlocal mean filtering

What is the real HTAP? (1) Background article

Molecular skeleton transition tool -delinker introduction
![Meta analysis [whole process, uncertainty analysis] method based on R language and meta machine learning](/img/87/9f8353c5c9c700eaa63f66697aa44a.png)
Meta analysis [whole process, uncertainty analysis] method based on R language and meta machine learning

Week 6 Learning Representation: Word Embedding (symbolic →numeric)

Redis过期删除策略和内存淘汰策略

Alibaba cloud industrial vision intelligent engineer ACP certification - Preparation
随机推荐
Excel VBA:将多个工作表保存为新文件
【洛谷】P1383 高级打字机
Google Emoji guessing game helps parents guide their children to surf the Internet safely
Learn to map with nature medicine -- complex heat map
What points should be paid attention to in the selection of project management system?
没背景、没学历?专科测试员进入互联网大厂是不是真的没希望?
Mysql主从同步及主从同步延迟解决方案
科技论文翻译,俄语文档的语法有何特点
The pit of history can only be filled up as far as possible
JVM第五讲:纵横数据如何应对洪峰推送
LeetCode链表问题——206.反转链表(一题一文学会链表)
A material of machine learning
Distance between bus stops: simple simulation problem
[weekly translation go] how to write your first program with go
BigDecimal 的 4 个坑,你踩过几个?
Computable general equilibrium (CGE) model practice technology in resource environment under the goal of "double carbon"
MySQL基础学习
Molecular skeleton transition tool -delinker introduction
JVM Lecture 6: how to solve the frequent FGC in online environment?
Embedded sharing collection 20