[Business] How to understand business?

1. What is a transaction?

A transaction refers to a series of operations performed as a single logical unit of work. These operations are either all done or not done at all, and are an indivisible unit of work.

2. What are the characteristics of transactions?

For a logical unit of work to become a transaction, in a relational database management system, it must satisfy four characteristics, namely the so-called ACID: atomicity, consistency, isolation and persistence.

  • Consistency: Before a transaction begins and after a transaction ends, the integrity constraints of the database are not violated.
  • Atomicity: All operations of the transaction are either completed or not completed, and will not end in an intermediate link.
  • Persistence: After the transaction is completed, the modifications made by the transaction are persisted and will not be lost.
  • Isolation: When multiple transactions access the same data in the database concurrently, the relationship shown.

Among the 4 features, 3 are related to WAL (the full name of WAL is Write-Ahead Logging, write-ahead logging system), and all of them need to be guaranteed by Redo and Undo logs.

Here are a few features:

consistency

First of all, look at consistency. Consistency actually includes two parts, namely constraint consistency and data consistency.

Constraint Consistency: It should be easy for everyone to think of constraints such as foreign keys, Check, and unique indexes specified when creating a table structure in the database. Unfortunately, in MySQL, Check is not supported, only the other two are supported, so constraint consistency is very easy to understand.

Data consistency: It is a comprehensive regulation, or a regulation that grasps the overall situation**. Because it is the result of the joint guarantee of atomicity, persistence, and isolation, rather than relying solely on a certain technology. **

atomicity

Next, let's look at atomicity. Atomicity is the two "either" mentioned above, that is, either changed or not changed. That is to say, the user cannot feel a state that is being changed. MySQL achieves this effect through WAL (Write Ahead Log) technology.

You may want to ask, what is the relationship between atomicity and WAL?

In fact, the relationship is very important. For example, if the transaction is committed, the changed data will take effect. If the dirty pages of the Buffer Pool are not flushed at this time, how to ensure that the changed data will take effect? You need to use the data recovered from the Redo log. And if the transaction is not committed and the dirty pages of the Buffer Pool are flushed, how will the data that should not exist disappear? It needs to be realized through Undo, and Undo is guaranteed through Redo, so the final atomicity guarantee is still realized by Redo's WAL mechanism.

Persistence

Let's look at persistence. The so-called persistence means that once a transaction is committed, its changes to the data in the database should be permanent, and subsequent operations or failures should not have any impact on it. As mentioned earlier, the atomicity of a transaction can guarantee that a transaction is either fully executed or not executed at all, which can logically ensure that the user cannot see the intermediate state.

But how is persistence guaranteed? Once the transaction is committed, through atomicity, even in the event of a downtime, the data can be retrieved logically and then written into the physical storage space again , thus ensuring that the data will not be lost from both logical and physical aspects, that is, The persistence of the database is guaranteed.

isolation

The so-called isolation means that the execution of a transaction cannot be interfered by other transactions, that is, the operations and data used within a transaction are isolated from other concurrent transactions. Locks and multi-version control meet isolation.

Guess you like

Origin blog.csdn.net/daohangtaiqian/article/details/130552467