Two-Phase Commit (Design Pattern of Distributed Systems)
The Two-Phase Commit (2PC) is a distributed consensus protocol used to ensure consistency across a distributed system when performing a transaction that spans multiple nodes. It operates in two phases: the Prepare Phase and the Commit Phase. Here’s a detailed explanation with examples:
1. Overview of the Two Phases
Prepare Phase (Voting Phase):
Commit Phase:
Example: Distributed Bank Transfer
Imagine a scenario where a customer wants to transfer money between two bank accounts managed by different servers:
Prepare Phase:
Commit Phase:
If both banks respond "Yes", the coordinator sends a Commit message:
领英推荐
If either bank responds "No", the coordinator sends an Abort message:
This ensures either the entire transaction succeeds or it fails without partial updates, preserving consistency.
Benefits of 2PC
Drawbacks
Variants
To address these issues, systems may use alternatives or enhancements such as:
Real-World Applications