consensus
Two Phase Commit (2PC)
Atomic distributed transactions: Phase 1 (Prepare/Vote) → Phase 2 (Commit or Abort).
phaseIDLE
transactions0
participants3
Phase 1: prepare
Phase 2: commit/abort
COORDINATOR
waiting
↓ ... ↓
○
P1
vote: pending
✓ will vote YES
○
P2
vote: pending
✓ will vote YES
○
P3
vote: pending
✓ will vote YES
Click participants to toggle failure (before starting)
// transaction log
No transactions yet
How It Works
- Phase 1: Coordinator asks all to prepare (vote)
- If ALL vote YES → Phase 2: COMMIT
- If ANY votes NO → Phase 2: ABORT
- Blocking protocol: coordinator failure blocks all
Tradeoffs
- + Atomic: all-or-nothing guarantee
- − Blocking: coordinator is single point of failure
- − Latency: 2 round trips minimum
- Used in: distributed databases, XA transactions