Two-Phase Commit Protocol
You'll learn: How the two-phase commit (2PC) protocol achieves atomic commitment across distributed nodes and why coordinator failure remains its Achilles' heel.
The Core Problem
When a distributed transaction spans multiple databases or services, you need all participants to either commit or abort together—no partial success allowed. The two-phase commit protocol solves this atomic commitment problem, ensuring "all or nothing" semantics.
How 2PC Works
The protocol uses a coordinator and multiple participants:
Phase 1 – Prepare (Voting Phase):
- Coordinator sends a
PREPARErequest to all participants - Each participant prepares the transaction (locks resources, writes to log)
- Participants reply with
VOTE-COMMIT(ready to commit) orVOTE-ABORT
Phase 2 – Commit (Decision Phase):
- If all votes are commit, coordinator sends
COMMITto everyone - If any vote is abort, coordinator sends
ABORTto everyone - Participants execute the decision and acknowledge
- Coordinator completes the transaction
Think of it like a wedding ceremony: the officiant (coordinator) asks "Do you...?" to both parties (participants). Only if both say "I do" does the officiant pronounce them married. One "no" cancels everything.
The Coordinator Failure Vulnerability
Here's the critical weakness: if the coordinator crashes after sending PREPARE but before broadcasting the final decision, participants are left in limbo. They've voted and locked resources but don't know whether to commit or abort. They must wait (potentially indefinitely) for the coordinator to recover—this is called blocking.
Unlike crash failures in replication where replicas can proceed, 2PC participants cannot safely make progress without the coordinator's decision, violating liveness under certain failure scenarios.
Key Takeaway: Two-phase commit guarantees atomic commitment across distributed nodes through a prepare-then-decide protocol, but coordinator failure can block participants indefinitely, making it unsuitable for systems requiring high availability during failures.