Two-Phase Commit (2PC)
Two-Phase Commit (2PC)
Introduction: The “All or Nothing” Wedding
Imagine a wedding. The priest asks the bride and groom: “Do you take…?”
- If both say “Yes,” the marriage is committed.
- If one says “No” (or faints), the ceremony is aborted for both.
Two-Phase Commit (2PC) is exactly this. In a distributed system where a single transaction spans multiple databases (e.g., deducting money from Bank A and adding it to Bank B), we need a way to ensure they both agree. We use a Coordinator to manage the process.
What Problem does it solve?
- Input: A transaction involving multiple nodes.
- Output: Atomic outcome (Commit or Abort).
- The Promise: Consistency. No node will commit if another node fails.
How it Works
Phase 1: Prepare (Voting)
- The Coordinator sends a “Prepare” message to all nodes (Participants).
- Each node executes the transaction locally but does not commit. It locks the resources.
- Each node votes: “Yes” (ready) or “No” (error).
Phase 2: Commit (Execution)
- If everyone voted “Yes”: The Coordinator sends a “Commit” message. Everyone makes the changes permanent.
- If anyone voted “No” (or timed out): The Coordinator sends a “Rollback” message. Everyone undoes their local changes.
Typical Business Scenarios
✅ Distributed Databases: Relational databases (like Postgres or MySQL) use 2PC for XA transactions across different physical machines.
✅ Legacy Enterprise Systems: Older banking and ERP systems rely heavily on 2PC to keep data synchronized.
❌ High Availability: 2PC is blocking. If the Coordinator crashes during Phase 2, the other nodes are left hanging with locked resources. They can’t move forward or backward.
❌ High Performance: Because it holds locks for a long time (two round-trips), 2PC is slow. Modern systems often prefer Sagas or TCC (Try-Confirm-Cancel) for better scalability.
Performance & Complexity
- Latency: High (requires 2 rounds of network communication).
- Fault Tolerance: Weak. It is a blocking protocol.
Summary
"2PC is the 'Distributed Wedding'. It ensures atomicity by asking everyone to vote before anyone makes a final decision. It's the standard for correctness, but it's slow and fragile."
