Appearance
Two-Phase Commit (2PC)
A distributed transaction protocol that ensures all nodes (participants) in a distributed database either ALL commit or ALL abort a transaction — maintaining atomicity across multiple sites.
The Two Phases
Phase 1 — Prepare Phase (Voting Phase)
- The Coordinator sends a PREPARE message to all participants
- Each Participant checks if it can commit:
- If yes → writes READY to its log → replies "Vote-Commit"
- If no → writes ABORT to its log → replies "Vote-Abort"
- Coordinator collects all votes
Phase 2 — Commit Phase (Decision Phase)
- If ALL participants voted Yes → Coordinator sends COMMIT to everyone
- If even one voted No → Coordinator sends ABORT to everyone
- Each participant executes the decision and sends an acknowledgment
Protocol Diagram
Coordinator Participant A Participant B
| | |
|---PREPARE----------->| |
|---PREPARE---------------------------------->|
| | |
|<--YES----------------| |
|<--YES----------------------------------------|
| | |
|---COMMIT------------>| |
|---COMMIT------------------------------------>|
| | |
|<--ACK-----------------| |
|<--ACK------------------------------------------|Failure Scenarios
| Failure Point | What Happens |
|---|---|
| Participant fails in Phase 1 | Coordinator aborts the transaction |
| Coordinator fails in Phase 1 | Participants timeout and abort |
| Coordinator fails in Phase 2 | BLOCKING: participants voted Yes but don't know the decision |
The Big Problem — Blocking
2PC is a BLOCKING protocol. If the coordinator crashes after participants voted "Yes" but before sending the decision, participants are stuck waiting indefinitely — they can't commit or abort on their own. This is why 3PC (Three-Phase Commit) was introduced.
Key Properties
- Ensures Atomicity across distributed nodes
- Simple and widely used
- Blocking in case of coordinator failure
- Can cause performance bottlenecks due to waiting