The underlying implementation principle of MySQL transaction

The role of MySQL transactions is to ensure the reliability, consistency, and concurrent processing of the database. The implementation of transactions requires three technologies, namely log files, including redo log and undo log, lock technology, and MVCC.

What is the redo log? The redo log is used to record the modification statement of the submitted transaction, because the database will not write it to the disk immediately for each modification operation in order to ensure efficiency, but will first store it in the memory buffer. In this case, there is a problem. When the server is down, the memory data is gone. At this time, the redo log is used, which can guarantee the persistence of the committed transactions.

What is an undo log? The undo log is used to record information before the data is modified. When a rollback occurs, the undo log can be used to roll back to the state before it was modified.

Lock technology refers to read-write locks. Through read-write locks, reading and reading can be done in parallel, but writing and reading cannot be done in parallel.

MVCC (MultiVersion Concurrency Control) is called multi-version concurrency control. InnoDB's MVCC is realized by saving two hidden columns behind each row of records. One of these two columns saves the creation time of the row, and the other saves the The expiration time of the row, of course, does not store the actual time value, but the system version number. Its main realization idea is to achieve read-write separation through multiple versions of data. So as to realize the reading without locking and then achieve parallelism of reading and writing.

The implementation of MVCC in mysql relies on undo log and read view. The undo log records multiple versions of a row of data. read view is used to determine the visibility of the current version of data.

Transactions have four characteristics, namely

  • Atomicity

  • Consistency

  • Isolation

  • Durability

So how does MySQL guarantee these 4 characteristics?

  • Atomicity: use undo log to achieve rollback
  • Persistence: use redo log to achieve recovery after failure
  • Isolation: Using locks and MVCC, the optimization ideas used include read-write separation, read-read parallelism, and read-write parallelism
  • The ultimate boss consistency of transactions is achieved through atomicity, persistence, and isolation! ! ! Consistency is achieved through rollback, recovery, and isolation in concurrent environments.

The purpose of atomicity, persistence, and isolation is also to ensure data consistency!

In short, ACID is just a concept, and the ultimate goal of transactions is to ensure the reliability and consistency of data.

reference

  1. The underlying implementation principle of MySQL transaction_xlshi1996's blog - CSDN blog
  2. The underlying implementation principle of mysql transaction - Baidu Library

Guess you like

Origin blog.csdn.net/qq_40382400/article/details/132154545