MySQL-depth understanding of the principles of the four ACID properties

As usual, let's take a scene ~

Interviewer: "I know the four major characteristics of the transaction it?" You: "Understand, ACID Well, atomicity (Atomicity), consistency (Consistency), isolation (Isolation), persistent (Durability)!" Interviewer: " mysql database you are using it, to speak briefly innodb in how to achieve these four characteristics of it. "you:"? I only know that isolation is how do balabala ~~ "interviewer:" or go back to such notice it ~ "

OK, back to the topic. Many of the four properties of atomicity (Atomicity) When it comes to affairs of consistency (Consistency), isolation (Isolation), persistent (Durability), understand people. But that, of these four characteristics to achieve the principles in the database is how little into detail? Few people would be able to answer was coming up. Therefore, this article focuses on what we achieved in the principles of the four characteristics of Mysql.


We transfer the account 50 yuan from A to B account as an example to explain ACID, four characteristics.


By definition, it refers to the atomic transaction is an indivisible unit of work, which operate either do or do not do. That transfer either succeed or fail transfer, is in the middle of the state does not exist!

If you can not guarantee what will happen atomicity?

The OK, data inconsistency occurs, A minus the account 50, while the B account an increase of 50 primitive operation failed. The system will be lost for no reason 50 yuan ~


By definition, isolation refers to the concurrent execution of multiple transactions when, inside the transaction operations are isolated from other transactions, the transaction can not interfere with each other between the respective concurrent execution.

If you can not guarantee what will happen isolation?

OK, assuming there are 200 yuan account A, B accounts 0. A B account transfer account to twice the amount of 50, respectively, performed in two transactions. If you can not guarantee isolation, there will be the following scenario

MySQL-depth understanding of the principles of the four ACID properties

As shown, if not guarantee isolation, A charged twice, and B plus models only once, suddenly disappear $ 50, still appeared data inconsistency!


By definition, persistence means once the transaction commits, changing its database should be permanent. The next operation or other faults should not have any effect on them.

If you can not guarantee what will happen persistence?

In Mysql, in order to solve the CPU and disk speed inconsistencies, Mysql is to load the data on the disk into memory, the memory is operating, and then return to writing to disk. Well, assuming that is down, all modified data in memory is lost, persistence can not be guaranteed.

Imagine, you are prompted to transfer success. But you find that any amount has not changed, the data at this time there has been no legal status data, we will consider this state is data inconsistency.


By definition, it refers to the consistency before and after transaction execution, data in a legal state, this state is not the syntax on semantics.

What is the legal status of the data it?

oK, this state is to meet the predetermined constraint is called a legal state, then plainly, this state is up to you to define. Meet this state, the data is consistent, does not satisfy this condition, the data is inconsistent!

If you can not guarantee the consistency of what will happen?

Example One: A 200 yuan account, transfer out 300 yuan, account balance -100 A case element. You naturally found at this time the data is inconsistent, and why? Because you define a state, the balance of this column must be greater than zero.

Example Two: A accounts 200 yuan, 50 yuan to transfer the account B, A buckle account money, but because unforeseen B account balance has not increased. You also know that at this time the data is inconsistent, and why? Because you define a state, it requires balance A + B must be the same.

Actual answer

One problem: Mysql how to ensure consistency?

OK, this question is divided into two levels.

From the database level, the database by atomicity, isolation, durability to ensure consistency. That is among the four ACID properties, C (identity) is the goal, A (atomic), I (isolation), D (persistence) is the means, in order to ensure the consistency of the means, provided by the database. AID database must implement three major characteristics, be possible to achieve consistency. For example, can not guarantee atomicity, consistency obviously can not be guaranteed.

However, if you write code that deliberately violate the constraints of the transaction, the consistency is not guaranteed. For example, in the example you transfer in, your code is not intentionally added to the B-account money, it still can not guarantee consistency. Thus, it must also be considered from the perspective of the application layer.

From the application level, the code through a database to determine whether the data is valid, then decide to roll back or to submit data!

Second problem: Mysql how to ensure atomicity?

OK, is the use of Innodb undo log.

undo log called rollback log, is key to achieving atomic, When Rollback can undo all the sql statement has been executed successfully, he needs to record the appropriate log information you want to roll back.


  • (1) When you delete a data when you need to record information on this data, the rollback of time, insert this old data
  • (2) When you update a data of the time, you need to log old before the rollback of time, perform the update operations based on the old value
  • (3) When data then insert a time, it requires a primary key of that record, rollback, and delete operations performed according to the main key

undo log records the information required for these rollback, or when a transaction fails to call a rollback, resulting in the need to roll back the transaction, you can use the information in the undo log data is rolled back to what it was before the modification.

ps: Specific undo log logs long-sawed, you can write an article of this. And write, not many people look at, let's first understand it so simple.

Question three: Mysql how to ensure durability?

OK, Innodb is the use of redo log.

As said before, Mysql is to first load the data on the disk into memory, modify the data in memory, disk painted on the back. If at this time suddenly goes down, data in memory is lost.

how to solve this problem?

Simple, ah, the data is written directly to disk before the transaction is committed on the line ah.

You do have any questions?

  • Only modify a page in a byte, it is necessary to brush the entire page into the disk, so a waste of resources. After all, a 16kb page size, you just change one little thing, we must be content 16kb brush into the disk, listening and unreasonable.
  • After all, a single transaction may involve modification of SQL multiple pages of data, and these data pages may not be contiguous, that is, are random IO. Obviously operate random IO, the speed will be slower.

So, we decided to redo log to solve the above problems. As data modification time, not only in memory operations, this operation will be recorded in the redo log in. When the transaction commits, the redo log will log the brush plate (redo log part in memory, on the part of the disk). When the database is down restart, the contents will redo log is restored to the database, and then decide to roll back and undo log data based on content or submit binlog data.

The benefits of using the redo log?

In fact, the benefit is high brush disk redo log data pages than to brush plate efficiency, specific performance is as follows

  • redo log small volume, after which only records what the modified pages, so small size, fast brush disc.
  • redo log that has to be added at the end, it belongs to the order of IO. Efficiency is clearly coming faster than random IO.

Question four: Mysql how to ensure the isolation of?

OK, use is MVCC locks and mechanisms. Or take the example to illustrate the transfer, there is an account table below

Table name t_balance

MySQL-depth understanding of the principles of the four ACID properties

Where id is the primary key, user_id as the account name, balance is the balance. Or in two transfers, for example, as shown in FIG.

MySQL-depth understanding of the principles of the four ACID properties

As MVCC, i.e. multi-version concurrency control (Multi Version Concurrency Control), a plurality of versions of data rows snapshot data, the snapshot data in the undo log in.

If the line is doing a transaction reads DELELE or UPDATE operation, the read operation will not wait for the release of the lock on the line, but read the snapshot version of the row.

Because of MVCC mechanism repeatable read (Repeateable Read) and Read Committed (Read Commited) of MVCC in different forms, not go into details.

But one thing to explain, for the Read Committed transaction isolation level (Read Commited), a transaction can read data from another transaction has been submitted, the isolation is not satisfied. However, when the transaction isolation level is repeatable read (Repeateable Read), the barrier properties are satisfied.

to sum up

This article stresses the realization of the principle of the four characteristics of the transaction ACID Mysql, I hope you gain something.

Guess you like