What is MVCC and why are gap locks designed?
This article will take you to understand MVCC, introduce the relationship between MVCC and isolation level, from a design perspective, talk about why MVCC is designed, and what is the difference between the isolation levels of RC and RR.
The role of MVCC
MVCC makes most transaction engines that support row locks no longer simply use row locks for database concurrency control. Instead, the row lock of the database is combined with the row version number, and non-locking reading can be achieved with only a small overhead. Thus improving the concurrency performance of the database.
MVCC uses a lock-free form to solve the problem of read-write conflicts. The read here refers to the snapshot read. That is, snapshot reading implemented by MVCC! ! !
What is MVCC
Multi-version concurrency control (MVCC) is a lock-free concurrency control that resolves read-write conflicts.
Each row of records has two hidden columns: creation version number and rollback pointer. There is a transaction id after the transaction is started. Multiple concurrent transactions operate a certain row at the same time. Different transactions' update operations on the row will produce multiple versions, and then use the rollback pointer to form an undo log chain. The snapshot reading of MVCC is achieved through the transaction ID and the creation version number.
The relationship between MVCC and isolation level
MVCC is to solve the read-write problem. And through different configurations, the problem of non-repeatable reading of snapshots after the transaction is started can also be solved.
Non-repeatable read: Some data read in the same transaction has changed, or some records have been deleted.
Phantom reading: A transaction re-reads previously retrieved data according to the same query conditions, only to find that other transactions have inserted new data that meets the query conditions. This phenomenon is called Phantom reading.
Both RC and RR implement MVCC, but why does RR solve the problem of non-repeatable reading in RC?
You can think that the reason why RC has the problem of non-repeatable reading is just because the developers set it intentionally (setting multiple isolation levels, the user can set it according to the situation). Originally, the data has been submitted to the database, so there is no problem when RC reads it? Moreover, the isolation level of the Oracle database itself is RC.
READ-COMMITTED (Read Committed)
Read Committed RC. Under this isolation level, consistent reading can be achieved at the SQL level. Each SQL statement will generate a new ReadView. This means that other transactions were submitted between the two queries, and inconsistent data can be read.
REPEATABLE-READ (repeatable read)
Repeatable read RR, after the ReadView is created for the first time, this ReadView will be maintained until the end of the transaction, that is, Visibility does not change during transaction execution, enabling repeatable reads within a transaction.
MVCC and gap lock
MVCC lock-free solves the problem of read-write conflicts. And solves the problem of non-repeatable reading. This achieves two isolation levels: RC and RR.
And Gap lock is still essentially a lock, which will block the execution of two concurrent transactions.
So why does RR enter the gap lock? Is it just to solve the problem of phantom reading?
Note: Gap locks only exist at the RR isolation level.
Gap locks can solve the problem of phantom reading to a certain extent, but I think the introduction of gap locks is more to deal with bugs in the statement mode of binlog.
The master-slave replication of mysql database relies on binlog. Before mysql5.0, binlog mode only had statement format. The characteristics of this mode: the recording order of binlog is in the order of database transaction commit order.
When there is no gap lock, there will be the following scenario:
The master library has two transactions:
1. Transaction a first delete id<6, and then before commit;
2. Transaction b directly insert id=3, and complete the commit;
3. Transaction a commits;
The log recorded by binlog at this time is: Transaction b is executed first, and transaction a is executed (the binlog records the commit order)
Then the master database has a record of id=3 in the table at this time, but the slave database inserts first and then deletes it. In the slave database, it is Not recorded.
This leads to inconsistency between master and slave data.
In order to solve this bug, gap lock is introduced at the RR level.
[Related recommendations: mysql video tutorial]
The above is the detailed content of What is MVCC and why are gap locks designed?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting
