Notestransactions🌱 seedling
Two-Phase Commit
Also: 2PC · Two Phase Commit · Atomic Commit Protocol · Paxos Commit · In-Doubt Transaction
An atomic commit protocol: a coordinator asks every participant to prepare (make the change durable and vote), then logs commit only if all voted yes and tells everyone. It is atomic but blocking: if the coordinator dies after prepare, participants wait, holding locks.
Also in: Distributed Transactions › When the coordinator fails
How it works
- Prepare: each participant checks constraints, takes locks, forces its changes and a prepare record to its log, then votes yes (or no).
- Decide: all yes → coordinator forces commit to its log; any no or timeout → abort. This log write is the point of no return.
- Complete: the coordinator tells every participant, retrying after crashes until all acknowledge.
- A yes vote surrenders the right to abort; a logged decision can never change.
Trade-offs
| Gains | Costs |
|---|---|
| atomic outcome across nodes | 2 round trips and forced log writes per commit |
| any participant can veto safely | locks held through both phases |
| simple to reason about | coordinator crash after prepare leaves participants in doubt, indefinitely |
In practice
- Paxos Commit: with 2F+1 coordinators it survives F failures; classic 2PC is the F = 0 case.
- Spanner / CockroachDB make coordinator and participants consensus groups, so a new leader finishes the commit. CockroachDB's parallel commits cut it to one consensus round.
- Percolator / TiKV drive 2PC from the client, with one primary lock as the commit point.
Pitfalls
- Confusing it with Two-Phase Locking (isolation on one node).
- Losing the coordinator's log: only a manual heuristic commit or abort remains, which can break atomicity.
- Three-phase commit doesn't save you under network partitions; consensus does.
Connections
- Part of
- Distributed Transaction
- Contrasts with
- Saga Pattern
- In real systems
- SpannerCockroachDBVitess
- Mentioned
- Two-Phase Locking
Recall
These cards are also in your review deck, scheduled with spaced repetition.