Two-phase commit protocol of distributed systems theory

First, the two-phase commit protocol introduced

Two-phase commit protocol is used protocol to ensure distributed transaction system . In distributed systems, a transaction completed by the need to coordinate multiple machines, between machines to communicate through the network, how to ensure that a set of operations that are done either on multiple machines, or none of them do it? (ACID properties of a transaction)

[For example, a transaction includes three operations A, B, C, operation A, B, C are in the machine 2, 3 is completed, if the machine operate successfully submitted 1 A, 2 machines successfully submitted Procedure B, but C 3 machine perform the operation fails, you need to undo all operations ......]

And then expanded it, the two-phase commit protocol is used to ensure that multiple copies of data consistency at the scene, it can be said: It is a strong consistency of centralized copy control protocol. For example, each data block in the cluster has three copies, when modifying a data block, in order to ensure consistency, these three copies need to be modified.

Strong consistency is reflected in: After modifying the operation is successful, when reading the copy, will be able to read the latest revised data.

Centralization is reflected in: a two-phase commit protocol coordinator needs to control the transaction process. The coordinator may be regarded as the central control node.

Copy control protocol: refers to the two-phase commit protocol can be used to ensure consistency copy scenario.

 

Second, the two-phase commit protocol analysis

In this protocol, the nodes into two categories: the coordinator and participant.

Two-phase commit protocol process is divided into two stages : when the Client to coordinator after initiation request,

Stage ①: voting phase

Coordinator asked if all the participants can do a commit

The various participants to prepare for the transaction, such as the allocation of resources, locked ......

Participants send a response to the coordinator (may submit or not to submit)

It should be noted that: when the response is sent participants to submit the transaction, then it can not abandon the transaction. Therefore, before a vote requires participants to commit the transaction, it must ensure that the final agreement be able to commit its own that part, even if the participant fails to be replaced halfway. To ensure that submission, each participant must save all the objects in the transaction changes as well as its own state to a persistent store. (If a participant can submit it that part of the transaction, it will update all records to the state and its persistent store --- that is ready to be submitted)

 

Stage ②: commit phase

Coordinator received all the participants returned "submit" response. Each participant to send "officially submitted" command

Participants completed their formal submission in local operations to return after a successful response to the coordinator ( "submit success" or "failure to submit")

When the coordinator receives a response of all involved are "successfully submitted", and this transaction successfully submitted. Otherwise failure (may need to roll back or retry)

 

Third, the characteristics of two-phase commit protocol

① agreement is blocked

After the coordinator need to wait for all participants to return a response before continuing to the next step.

② network fault tolerance poor

As long as there is a participant fails (network timeout or down), coordinator need to wait until the timeout, this transaction failed.

③ coordinator single point of failure

In the second phase, when the coordinator is not issued "formally submitted" when it is down, the participants will enter the "unknown" state and did not know what to do next (resending the first stage of the "submit" response or wait for the timeout)

 

Fourth, references

Distributed transaction processing system

Guess you like

Origin blog.csdn.net/hellozhxy/article/details/93110280