3. Conformance Protocol 2PC Protocol (ZooKeeper Note)
The origin of 2PC and 3PC
In a distributed system, each machine node can clearly know whether its transaction operation is successful (it can be understood as a stand-alone operation), but it can not directly obtain the operation results of other distributed nodes. Therefore, when a transaction operation needs to span multiple distributed nodes, in order to maintain the ACID feature of the transaction, it is necessary to introduce a component called "coordinator" to schedule the execution logic of all distributed nodes uniformly, which we call "participant". Based on this idea, two protocols of 2PC and 3PC are derived.
1. 2PC (Two-Phase Commit) two-phase submission
Phase 1: commit transaction request
Business inquiry.
The coordinator sends transaction content to all participants, asks if the transaction commit operation can be performed, and starts waiting for a response from each participant. Execute a transaction
Each participant performs transaction operations and records undo and redo information in the transaction log. Each participant feedback the response of the transaction inquiry to the coordinator
If the participant successfully performs the transaction operation, feedback the Yes response to the coordinator and vice versa to the No response.
Phase 2: perform transaction commit (the result of phase 1 feedback is Yes)
Send a submission request
The coordinator issues an Commit request to all participant nodes. Transaction commit
After the participant receives the Commit request, the transaction commit operation is formally performed. Feedback on transaction commit results
After completing the transaction commit, the participant sends an ACK message to the coordinator. Complete the transaction
After receiving the ACK messages from all participants, the coordinator completes the transaction.
Phase 2: interrupt the transaction (the result of phase 1 feedback has No or timeout)
Send a rollback request
The coordinator issues an RollBack request to all participant nodes. Transaction rollback
After receiving the RollBack request, the participant uses the undo information in Phase 1 to perform the transaction rollback operation. Feedback the result of transaction rollback
After completing the transaction rollback, the participant sends an ACK message to the coordinator. Interrupt a transaction
After receiving the ACK message from all participants, the coordinator completes the transaction interruption. Advantages
The principle is simple and the realization is convenient. Disadvantage synchronous blocking single point problem data inconsistency is too conservative