Two-phase commit (2PC)?
Two-phase Commit (2PC) Phase 1: The coordinator asks the participant if the transaction was successfully executed and the participant sends back the result of the transaction execution. The coordinator at this stage has a timeout mechanism, assuming that there is no response from one of the participants due to network reasons or one of the participants hangs up, then the transaction is judged to have failed and a rollback command is sent to all participants. Phase 2: If the transaction executes successfully on each participant, the transaction coordinator sends a notification for the participant to commit the transaction; Otherwise, the coordinator sends a notification to the participant to roll back the transaction. The coordinator of this phase cannot time out and has to keep retrying.
The coordinator is a single point with a single point of failure. Assuming the coordinator hangs before sending the prepare command, ok equals the transaction has not started. Let’s say the coordinator hangs after sending the prepare command, which is not very good, and some participants are in a transactional resource lock state. Not only does the transaction fail to execute, but it also blocks other operations in the system because some common resources are locked. Assuming that the coordinator dies before sending the rollback transaction command, the transaction also fails to execute and the participants who are ready to succeed are blocked in the first phase. Suppose the coordinator hangs after sending the rollback transaction command, which is fine, at least if the command is sent, there is a high probability that the rollback will succeed and the resource will be released. However, if there is a network partition problem, some participants will be blocked because they cannot receive commands. Suppose the coordinator dies before sending the commit transaction command. All resources are blocked. Suppose the coordinator hangs after sending the commit transaction command, which is fine, and at least the command is sent. There is a high probability that the commit will succeed and the resource will be released, but if there is a network partition problem some participants will be blocked because they can’t receive the command.
Disadvantages: Synchronous blocking All transaction participants are synchronous blocked while waiting for other participants to respond and cannot perform other operations. The single point of problem coordinator plays a very large role in 2PC, and failure can have a big impact. In particular, when phase two fails, all participants are kept in a waiting state, unable to complete other operations. Data inconsistency In phase 2, if the coordinator sends only part of the Commit message, an exception occurs on the network, and only part of the participants receive the Commit message, that is, only part of the participants Commit the transaction, making the system data inconsistent. Too conservative, the failure of any node will lead to the failure of the whole transaction, without a perfect fault tolerance mechanism.