Implementation principle of version snapshot in MVCC

In MVCC (Multi-Version Concurrency Control, multi-version concurrency control), the snapshot version refers to the view of the data in the database at a specific point in time. It is a data snapshot created at the beginning of a transaction to provide the consistent view required by the transaction.

The snapshot version works as follows:

  1. Create a snapshot version: When a transaction starts, the database system will create a snapshot version for the transaction according to the time point when the transaction starts. This snapshot version contains a view of the data that was committed before the transaction started.

  2. Access to the snapshot version: A transaction can only access the snapshot version at the time of its creation during execution. This means that a transaction can only see snapshots of data that were committed before it started, not changes that have not been committed by other transactions.

  3. Concurrent reads are non-blocking: Since each transaction has its own snapshot version, multiple transactions can read the database concurrently without blocking each other. Each transaction reads its own snapshot version without being affected by other transactions reading or modifying data.

  4. Conflict detection and processing: When a transaction tries to modify a certain data, MVCC will check whether the data conflicts with the snapshot version of other transactions. If there is a conflict, appropriate action will be taken, such as rolling back the transaction or waiting for other transactions to complete.

  5. Commit and rollback: When a transaction is completed, decide whether to commit or rollback the transaction according to the result of conflict detection. When a transaction is committed, the modifications made by the transaction will be applied to the database so that other transactions can see the modified data.

By using the snapshot version, MVCC provides a consistent view of each transaction, enabling transactions to maintain isolation during concurrent execution, avoiding the problem of reading dirty data or overwriting each other. Each transaction can see the version of the data that has been committed before it started, thus ensuring data consistency and transaction isolation.

It should be noted that the MVCC implementation of different database systems may be different, but its core principles and goals are similar. By creating snapshot versions and concurrency control based on snapshot versions, MVCC provides an efficient and concurrency-safe method to handle concurrent access of database transactions.

Guess you like

Origin blog.csdn.net/biyn9/article/details/130971032