One-phase commit, two-phase commit, three-phase commit

Original reference: http://blog.csdn.net/bluishglc/article/details/7612811



Database requirements: ACID
Atomicity, Consistency, Isolation, Durability.


Atomicity: All changes are either made or not made.
Consistency: Data remains in a consistent state.
Isolation: Some changes are invisible to other users.
Durability: Once the transaction is confirmed to the user, the data is in a safe state (usually on disk).

One-phase commit
One-phase commit is very straightforward, which is the process from when the application sends a commit request to the database to when the database completes the commit or rollback and returns the result to the application. One-phase commit does not require the role of "coordinator", and there is no coordination operation between nodes, so the transaction execution time is shorter than that of two-phase commit, but the "dangerous period" of commit is the actual commit time of each transaction. Compared to two-phase commits, one-phase commits are more likely to be "inconsistent". But we must pay attention: only when there is a problem with the infrastructure (such as network outage, downtime, etc.), the one-phase commit may appear "inconsistent". Compared with its performance advantages, many teams will choose this plan.

The data can achieve final consistency through the transaction compensation mechanism (that is, the data has been recorded on some nodes, and the data will be synchronized and updated to all nodes after a later period of time, so that the data on all nodes is consistent) .


Two-phase commit
Two-phase commit mainly guarantees the atomicity of distributed transactions: that is, all nodes either do all or none of them. The
so-called two phases refer to:
the first phase: the preparation phase.
The second stage: the submission stage.

1. Preparation phase: The transaction coordinator (transaction manager) sends a Prepare message to each participant (resource manager), and each participant either returns failure directly (such as permission verification failure), or executes the transaction locally and writes locally The redo and undo logs, but not committed, reach a state of "everything is ready, only the wind is owed".

2. Commit stage: If the coordinator receives the participant's failure message or timeout, it directly sends a Rollback message to each participant; otherwise, it sends a Commit message; the participant executes the commit according to the coordinator's instructions Or roll back the operation, releasing all lock resources used during transaction processing. (Note: The lock resource must be released in the final stage)


The three-phase commit
is the process of putting a prepare_commit in the middle of the two-phase commit to ensure the consistency of the data of each node.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327041223&siteId=291194637