This is the 26th day of my participation in the August More Text Challenge
2PC
2PC (two-phase Commit) : two-phase Commit. It divides the transaction submission process into two stages, and the execution process is as follows.
Phase one: Submit the transaction request
-
Transaction to ask
The coordinator sends transaction content to all participants, asks if a transaction commit can be performed, and waits for responses from each participant.
-
Perform transactions
Each participant node performs a transaction and writes Undo and Redo information to the transaction log.
-
Each participant reports back to the coordinator the response to the transaction query
If the participant successfully executes the transaction, the coordinator is given a Yes response indicating that the transaction can be executed. If the participant does not successfully execute the transaction, a No response is returned to the coordinator indicating that the transaction cannot execute.
Phase two: Perform the transaction commit
In phase 2, the coordinator will decide whether the transaction commit operation can be carried out based on the feedback of each participant. Normally, the following two situations are involved.
Perform transaction commit
The coordinator gets a Yes response from all participants, and the transaction commit is performed.
-
Send submit request
The coordinator issues Commit requests to all the participant nodes.
-
Transaction commit
After receiving the Commit request, the participant formally performs the transaction Commit and releases the transaction resources occupied during the entire transaction execution.
-
Feedback transaction commit results
The participant sends an Ack message to the coordinator after completing the transaction commit.
-
To complete the transaction
After receiving Ack messages from all participants, the coordinator completes the transaction.
Interrupt the transaction
If any participant gives a No response to the coordinator, or if the coordinator does not receive a response from all participants after a timeout, the transaction is interrupted.
-
Send a rollback request
The coordinator issues a Rollback request to all the participant nodes.
-
Transaction rollback
After receiving the Rollback request, the participant uses the Undo information recorded in one of the phases to perform the transaction Rollback and, upon completion of the Rollback, releases the resources occupied during the entire transaction execution.
-
Feedback transaction rollback results
The participant sends an Ack message to the coordinator after completing the transaction rollback.
-
Interrupt the transaction
The coordinator completes the transaction interrupt after receiving an Ack message from all participants.
The problem
-
A synchronized block
In either phase I or Phase II, all participant resources and coordinator resources are locked, and all logic participating in the transaction operation is blocked.
-
A single point of the problem
Due to the importance of the coordinator, once the coordinator fails, participants will block forever. Especially in the second phase, all participants are locked in transaction resources and cannot continue to complete the transaction.
3PC
3PC (three-phase Commit) is an improved version of 2PC. It divides the execution transaction Commit process of the two-phase Commit protocol into two phases, forming a transaction protocol consisting of CanCommit, PreCommit and DO Commit phases. In addition, 3PC supports a participant timeout mechanism (only the coordinator has a timeout mechanism in 2PC).
Phase one: CanCommit
- Transaction to ask
- Each participant reports back to the coordinator the response to the transaction query
Phase 2: PreCommit
In phase 2, the coordinator precommits the transaction based on the feedback from the participants. There are two possibilities based on participants’ feedback.
Perform transaction pre-commit
If the coordinator gets Yes from all participants, the transaction pre-commit is performed.
- Send a pre-commit request
- Transaction precommit
- Each participant gives the coordinator feedback on the transaction execution response
Interrupt the transaction
The transaction is interrupted if any participant gives the coordinator a No response, or if the coordinator is unable to receive feedback from all participants after the wait times out.
- Send interrupt request
- Interrupt the transaction
Phase 3: doCommit
It’s similar to phase two of 2PC.
Compared with 2PC, 3PC supports timeout mechanism for participants, reducing the blocking range for participants. In addition, through the design of CanCommit, PreCommit and DoCommit, compared with 2PC, an additional buffer stage is set to ensure that the states of all participating nodes are consistent before the final submission stage.