当前位置:网站首页>The default isolation level of MySQL is RR. Why does Alibaba and other large manufacturers change to RC?
The default isolation level of MySQL is RR. Why does Alibaba and other large manufacturers change to RC?
2022-07-28 04:59:00 【Young】

Mainly because MySQL The process of master-slave replication is through bin log For data synchronization , and MySQL Early only statement such bin log Format , In this format ,bin log The record is SQL The original text of the sentence .
When the transaction is out of order , This will cause the standby database to be in SQL After playback , The result is inconsistent with the content of the main library .
To solve this problem ,MySQL By default Repetable Read This level of isolation , because stay RR in , When updating the data, the record lock will be added and the gap lock will be added at the same time . This can be avoided .
Before I know MySQL The default isolation level is RR after , For a long time, I thought that no one should modify the default configuration .
But until one day , There was a deadlock on our line , I was in the process of checking , It was found that the isolation level of our database did not use the default RR, It's changed to Read Committed .( About the deadlock investigation process , You can refer to :[ A database deadlock troubleshooting process ](https://blog.csdn.net/agonie201218/article/details/122217626)
You can use this command to view the current isolation level of the database :
select @@tx_isolation;
that , There are questions here , Why did Ali change the database isolation level to RC Well , Is there any thinking behind it ?
RR and RC The difference between
Trying to figure this out , We need to find out first RR and RC The difference between , Analyze their advantages and disadvantages .
Read consistency
Read consistency , Also known as snapshot read . Snapshot is the historical version before the current row of data . Snapshot read is to use snapshot information to display query results based on a certain point in time , Regardless of changes made by other transactions running at the same time .
stay MySQL in , Only READ COMMITTED and REPEATABLE READ These two transaction isolation levels use consistent read .
stay RC in , Each read regenerates a snapshot , Always read the latest version of the line .
stay RR in , The snapshot is taken for the first time in the transaction SELECT Generated when the statement is executed , Only changes made to the data in this transaction will update the snapshot .
In the database RC In this isolation level , And support " Semi consistent reading " , One update sentence , If where Records matched by conditions have been locked , that InnoDB The most recently submitted version of the record will be returned , from MySQL The upper layer determines whether this needs to be locked .
Locking mechanism
Database lock , Under different transaction isolation levels , Different mechanisms are used . stay MySQL in , There are three types of locks , Namely Record Lock、Gap Lock and Next-Key Lock.
Record Lock Represents a record lock , The lock is the index record .
Gap Lock It's the gap lock , The lock is the gap between index records .
Next-Key Lock yes Record Lock and Gap Lock The combination of , Lock the index record and clearance at the same time . His range is left open and right closed .
stay RC in , It will only increase the index Record Lock, Not add Gap Lock and Next-Key Lock.
stay RR in , In order to solve the problem of unreal reading , In support of Record Lock At the same time , And support Gap Lock and Next-Key Lock;
Master slave synchronization
During data master-slave synchronization , In different formats binlog There are also requirements for transaction isolation level .
MySQL Of binlog It mainly supports three formats , Namely statement、row as well as mixed, however ,RC Isolation level only supports row Format binlog. If you specify mixed As binlog Format , So if you use RC, The server automatically uses row Format of logging .
and RR The isolation level also supports statement、row as well as mixed Three .
Why do Internet companies choose to use RC
Enhance concurrency
What is the biggest difference between Internet companies and traditional enterprises ?
High concurrency !
you 're right , The concurrency of Internet business is much higher than that of traditional enterprises .2020 On the eleventh day of the new year , Order creation peaked at 58.3 Ten thousand brush / second .
Many people ask , What can we do to withstand such a large amount of concurrency . Actually , The optimization behind this can't be finished for hours , Because what we have to do 、 There are so many things you can do .
There is an optimization related to our article today , That is to improve concurrency by modifying the isolation level of the database .
Why? RC Than RR The degree of concurrency is better ?
First ,RC In the process of locking , No need to add Gap Lock and Next-Key Lock Of , Just add row level locks to the records to be modified .
This makes the degree of concurrency higher than RR Much higher .
in addition , because RC And support " Semi consistent reading ", It can greatly reduce the conflict of row lock when updating statements ; For records that do not meet the update conditions , The lock can be released in advance , Increase degree of concurrency .
Reduce deadlocks
because RR This level of transaction isolation increases Gap Lock and Next-Key Lock, This makes the granularity of the lock larger , Then it will increase the probability of deadlock .
Deadlock : A transaction locks the table A, And then you access the table B; Another transaction locks the table B, And then try to access the table A; At this time, they will wait for each other to release the lock , It leads to deadlock .
summary
This paper introduces some MySQL Database RR and RC Two transaction isolation levels . They are mainly in the locking mechanism 、 There are some differences in master-slave synchronization and consistent reading .
And many big factories , In order to improve concurrency and reduce the probability of deadlock , The isolation level of the database will be changed from the default RR Adjust to RC.
Of course , This is not entirely without problems , use first RC after , You need to solve the problem of fantasy reading by yourself , This is actually ok , Many times, the problem of unreal reading can be ignored , Or it can be solved by other means .
There is also the use of RC When , Out of commission statement Format binlog, This effect is actually negligible , because MySQL Is in 5.1.5 Version starting support row Of 、 stay 5.1.8 Support for mixed, The latter two can replace statement Format .
边栏推荐
- The difference between alter and confirm, prompt
- 阿里巴巴面试题【杭州多测师】【杭州多测师_王sir】
- Rendering process, how the code becomes a page (I)
- [每日一氵]上古年代的 Visual Studio2015 安装
- After a year of unemployment, I learned to do cross-border e-commerce and earned 520000. Only then did I know that going to work really delayed making money!
- [idea] check out master invalid path problem
- [函数文档] torch.histc 与 paddle.histogram 与 numpy.histogram
- scipy.stats.chi2
- [daily one] visual studio2015 installation in ancient times
- Web渗透之域名(子域名)收集方法
猜你喜欢
![[Hongke technology] Application of network Multimeter in data center](/img/28/2ecc5a7a766454968819c7748fe48e.png)
[Hongke technology] Application of network Multimeter in data center
![[high CPU consumption] software_ reporter_ tool.exe](/img/3f/2c1ecff0a81ead0448e1215567ede7.png)
[high CPU consumption] software_ reporter_ tool.exe

How to quickly locate bugs? How to write test cases?

Domain name (subdomain name) collection method of Web penetration

Evolution of ape counseling technology: helping teaching and learning conceive future schools

Artificial intelligence and RPA technology application (I) -rpa Hongji product introduction, designer interface function explanation

What is the core value of testing?

flink思维导图

With a monthly salary of 15.5K, he failed to start a business and was heavily in debt. How did he reverse the trend through software testing?

MySQL 默认隔离级别是RR,为什么阿里等大厂会改成RC?
随机推荐
Redis类型
Transformer -- Analysis and application of attention model
基于MPLS构建虚拟专网的配置实验
(clone virtual machine steps)
[function document] torch Histc and paddle Histogram and numpy.histogram
Performance comparison between set and list
RT_ Use of thread mailbox
机器人教育在STEM课程中的设计研究
HDU 1435 stable match
驾驭EVM和XCM的强大功能,SubWallet如何赋能波卡和Moonbeam
Design and development of C language ATM system project
[II. Mobile web page development] 2D & 3D conversion and animation, mobile terminal layout, responsive layout
Leetcode 15. sum of three numbers
POJ 3417 network (lca+ differential on tree)
X Book Keyword Search
FPGA:使用PWM波控制LED亮度
Observable time series data downsampling practice in Prometheus
Introduction to testcafe
Redis configuration file explanation / parameter explanation and elimination strategy
Easycvr Video Square snapshot adding device channel offline reason display