Topic 138 / Advanced
Distributed transactions and two-phase commit
Chapter 8.5 · Locks, Transactions, and Sagas
What and why
A single database's transaction (→ 70) is atomic because one engine controls the whole commit; when a logical operation spans multiple independent databases or services, no single engine can make that same all-or-nothing guarantee alone. Two-phase commit (2PC) tries to extend atomicity across participants anyway: a coordinator asks every participant to prepare (lock its resources and promise it can commit), and only once every participant says yes does the coordinator tell them all to commit — if any says no, or doesn't respond in time, the coordinator tells everyone to abort instead. Builds on: → 70 (transactions and isolation levels).
Diagram
Phase 1 (prepare): coordinator → P1, P2, P3: "can you commit?"
P1 ✓ P2 ✓ P3 ✓ (all locked, waiting)
Phase 2 (commit): coordinator → P1, P2, P3: "commit"
────────✗ coordinator crashes here ✗────────
P1, P2, P3 are still locked, waiting for an
instruction that will never arrive
How it works
- Prepare phase: the coordinator sends a prepare request to every participant; each does everything needed to guarantee it can commit (locks, a durable log entry) and replies yes or no, without yet making the change visible.
- Commit phase: if every participant replied yes, the coordinator durably records the decision and tells all participants to commit; if any replied no or timed out, it tells all to abort.
- Once a participant has said yes, it must hold its locks and wait — it cannot unilaterally commit or abort on its own, because the coordinator's final decision could still go either way.
- This is the protocol's central weakness: if the coordinator crashes after phase 1 but before every participant receives the phase-2 decision, those participants are blocked, holding locks indefinitely, unable to safely guess the decision (guessing wrong in either direction breaks atomicity).
- The coordinator is a genuine single point of failure for liveness (not safety — 2PC doesn't lose data, it stalls), and recovery typically needs the coordinator itself made durable and restarted, or manual intervention.
- This blocking across service boundaries — one slow or down service holding up every other participant's locks — is why 2PC is avoided for cross-service transactions, and why the saga pattern (→ 139) exists as an alternative that holds no cross-network locks at all.
Practical example
(illustrative). A transfer touches an account-balance service and a ledger service. The coordinator asks both to prepare; both lock the relevant rows and reply yes. The coordinator then crashes before sending "commit" to either. Both services are now holding locks on real data, correctly waiting for a decision that was never recorded durably before the crash — they cannot safely commit (the coordinator might have decided abort) or abort (it might have decided commit) without more information.
When to use it
- Use when: all participants are durable and closely coupled, typically within one trust and operational boundary, and the coordinator itself can be made highly available.
- Avoid when: spanning independently deployed services — the blocking failure mode above turns one service's downtime into every participant's downtime.
- Failure scenarios: a coordinator crash leaving participants blocked and holding locks (above); one slow participant in phase 1 holding up every other participant's locks, turning one slow dependency into a cluster-wide stall.
- Simpler alternative: if everything fits in one database, use a local transaction (→ 70); across services, use the saga pattern (→ 139).
Practice
(Mechanism kind — trace by hand; 20 min, no lab services). Draw 3 participants and a coordinator. Walk through a successful run (all prepare yes, all commit). Then walk through the coordinator-crash scenario above: after all three reply yes, the coordinator crashes before sending commit. For each participant, state what it's doing right now and what information it would need to safely proceed on its own. Done when you can explain, concretely, why no participant can safely guess the outcome without that missing information.
Key takeaway
Two-phase commit gets atomicity across participants by making them all wait for one coordinator's final word — which also makes that coordinator a single point of failure for progress, not just correctness. Done when you can: trace a 2PC round and explain exactly why a coordinator crash after phase 1 blocks participants rather than losing their data. Used next in → 139.
References
Kleppmann and Riccomini, Designing Data-Intensive Applications — see
references.md.