当前位置:网站首页>Interview summary
Interview summary
2022-04-23 05:09:00 【Lyzxii】
Did you do it MySQL Read / write separation ? How to achieve mysql Separation of reading and writing ?MySQL What is the principle of master-slave replication ? How to solve mysql Delay problem of master-slave synchronization ?
This , High concurrency stage , That must be the separation of reading and writing , What do you mean by that? ? Because in fact most Internet companies , Some websites , Or is it app, In fact, it's all about reading more and writing less . So in this case , It's about writing a main library , But the master library has multiple slave libraries , Then read from multiple slave Libraries , That can support higher read concurrency pressure ?
Analysis of interview questions
(1) How to achieve mysql Separation of reading and writing ?
It's very simple , It's based on the master-slave replication architecture , Simply speaking , Just a main library , Hang multiple slave Libraries , And then we just write the main library , Then the master database will automatically synchronize the data to the slave database .
(2)MySQL What is the principle of master-slave replication ?
The master database will change to write binlog journal , Then connect the slave library to the master library , There's one from the library IO Threads , Will be the main library of binlog Copy the log to your own place , Write to a relay log . And then there's one from the library SQL The thread reads from the relay log binlog, And then execute binlog The contents of the log , That is to do it again on your own SQL, In this way, you can ensure that your data is the same as that of the main database .
Here's a very important point , That is, the process of synchronizing master database data from slave database is serialization , In other words, parallel operations on the main database , It will be executed serially on the slave library . So this is a very important point , Because the slave database copies logs from the master database and executes it serially SQL Characteristics , In high concurrency scenarios , The data of the slave database must be slower than that of the master database , There is a delay . So there's a lot of , The data just written to the main database may not be read , It's going to take tens of milliseconds , Even hundreds of milliseconds to read .
Extended problem :
If the main library suddenly goes down , Then it happens that the data has not yet been synchronized to the slave library , Then some data may not exist in the slave database , Some data may be lost .
therefore mysql In fact, there are two mechanisms in this area
- Semi-synchronous replication , It is used to solve the problem of data loss in the main database
- Parallel replication , It is used to solve the problem of master-slave synchronization delay
This is called semi synchronous replication ,semi-sync Copy , Write to the main database binlog After the log , It will force the data to be synchronized to the slave library immediately at this time , Write the log from the library to your own local relay log after , And then there's going to be a return ack To the main library , The master library received at least one slave Library's ack After that, the write operation will be considered completed .
Parallel replication , It refers to opening multiple threads from the library , Parallel reading relay log Logs from different libraries in , Then replay the logs of different libraries in parallel , This is library level parallelism .
- The principle of master-slave replication
- The cause of the master-slave delay problem
- Data loss of master-slave replication , And the principle of semi synchronous replication
- The principle of parallel replication , Multi database concurrent replay relay journal , Alleviate the problem of master-slave delay
(3)mysql Master slave synchronization delay problem ( The essence of )
I did deal with it online because of the delay of master-slave synchronization , Lead to online bug, Small production accidents
show status,Seconds_Behind_Master, You can see how far behind is copying data from the master library ms
In fact, we often come across this thing , For example, it uses mysql After the master-slave architecture , You might find , The data just written into the database was not found , And it ended up ....
So in fact, you have to think about the scene where you should use this mysql Master slave synchronization , The suggestion is that reading is much more than writing , In addition, when reading, the requirement for data timeliness is not so high , use mysql Master slave synchronization
So at this point , One thing we can think about is , You can use it. mysql Parallel replication of , But the problem is that it's Library level parallelism , So sometimes it doesn't work very much
So at this point .. Generally speaking , We will make sure that we can find the scene immediately after we write it , The main library is read by force , So that you can read the data for sure . In fact, it's no problem to use some database middleware .
Generally speaking , If the master-slave delay is serious
- sub-treasury , Split a main library into 4 A main storehouse , The write concurrency of each main library is 500/s, In this case, the master-slave delay is negligible
- open mysql Supported parallel replication , Parallel replication of multiple libraries , If we say that the write concurrency of a library is particularly high , Single library writing and developed to 2000/s, Parallel copy still doesn't make sense .28 The laws of , Many times, for example , Just a few order forms , Written 2000/s, Dozens of other tables 10/s.
- Rewrite the code , Students who write code , Be careful , At that time, we actually asked that student to rewrite the code for a short time , After inserting data , Just update , Don't inquire
- If it does exist, it must be inserted first , Immediately ask to find out , And then you immediately have to do something in reverse , Set up a direct connection to the main database for this query . This method is not recommended , If you do this, the meaning of the separation of reading and writing will be lost
版权声明
本文为[Lyzxii]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220549138389.html
边栏推荐
- Where, on when MySQL external connection is used
- How to exit VIM
- Tensorflow realizes web face login system
- 机器学习---线性回归
- Differences between redis and MySQL
- 5 minutes to understand MySQL row column conversion
- Grpc long connection keepalive
- mysql5. 7. X data authorization leads to 1141
- 深度学习笔记 —— 微调
- 深度学习笔记 —— 数据增广
猜你喜欢
Leetcode 1547: minimum cost of cutting sticks
独立站运营 | FaceBook营销神器——聊天机器人ManyChat
Details related to fingerprint payment
MySQL memo (for your own query)
Cross border e-commerce | Facebook and instagram: which social media is more suitable for you?
Perfect test of coil in wireless charging system with LCR meter
Define defines constants and macros, pointers and structures
AQS源码阅读
Transaction isolation level of MySQL transactions
多线程基本概念(并发与并行、线程与进程)和入门案例
随机推荐
Luogu p2731 horse riding fence repair
独立站运营 | FaceBook营销神器——聊天机器人ManyChat
The 8 diagrams let you see the execution sequence of async / await and promise step by step
Agile practice | agile indicators to improve group predictability
Redis data type usage scenario
The WebService interface writes and publishes calls to the WebService interface (2)
Learning Android V from scratch - UI
scp命令详解
leetcode——启发式搜索
Logrus set log format and output function name
深度学习笔记 —— 微调
[2021] Spatio-Temporal Graph Contrastive Learning
PHP counts the number of files in the specified folder
AQS源码阅读
DIY is an excel version of subnet calculator
Knowledge points sorting: ES6
Deep learning notes - object detection and dataset + anchor box
Unity C e-learning (IV)
Summary of MySQL knowledge points
Graduation project