当前位置:网站首页>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
边栏推荐
- 《2021多多阅读报告》发布,95后、00后图书消费潜力攀升
- 深度学习笔记 —— 物体检测和数据集 + 锚框
- Knowledge points sorting: ES6
- Sword finger offer: the path with a certain value in the binary tree (backtracking)
- Deep learning notes - object detection and dataset + anchor box
- Informatics Olympiad 1955: [11noip popularization group] Swiss round | openjudge 4.1 4363: Swiss round | Luogu p1309 [noip2011 popularization group] Swiss round
- This call when the transaction does not take effect
- HRegionServer的详解
- How can continuous integration (CI) / continuous delivery (CD) revolutionize automated testing
- Leetcode 1547: minimum cost of cutting sticks
猜你喜欢
Jupyter notebook crawling web pages
The 8 diagrams let you see the execution sequence of async / await and promise step by step
Learning Android II from scratch - activity
Deep learning notes - semantic segmentation and data sets
MySQL foreign key constraint
MySQL 慢查询
The WebService interface writes and publishes calls to the WebService interface (I)
Define defines constants and macros, pointers and structures
深度学习笔记 —— 语义分割和数据集
Learning Android V from scratch - UI
随机推荐
QPushbutton 槽函数被多次触发
Sword finger offer: push in and pop-up sequence of stack
Detailed explanation of hregionserver
【数据库】MySQL基本操作(基操~)
Solve valueerror: argument must be a deny tensor: 0 - got shape [198602], but wanted [198602, 16]
[2021] Spatio-Temporal Graph Contrastive Learning
AQS source code reading
Spell it! Two A-level universities and six B-level universities have abolished master's degree programs in software engineering!
[2022 ICLR] Pyraformer: Low-Complexity Pyramidal Attention for Long-Range 时空序列建模和预测
Get the number of days between dates, get the Chinese date, get the date of the next Monday of the date, get the working day, get the rest day
mysql5. 7. X data authorization leads to 1141
Use the built-in function of win to transfer files between two computers in the same LAN (the speed is the same as that between local disks)
Tensorflow realizes web face login system
Restful toolkit of idea plug-in
Various ways of writing timed tasks of small programs
深度学习笔记 —— 微调
MySQL - index
Chapter I overall project management of information system project manager summary
Basic concepts of multithreading (concurrency and parallelism, threads and processes) and entry cases
[database] MySQL multi table query (I)