Distributed Transactions & 2PC
Coordinate one atomic outcome across independent participants—and understand the blocking cost.
2–5 minute refresher
Coordinator
1 · PREPARE↓2 · COMMIT / ABORT
Inventoryready ✓
Paymentready ✓
Ledgerready ✓
Prepare everyone → record one decision → commit or abort everyone
30second
refresher
refresher
Two-phase commit coordinates one atomic outcome across participants. In prepare, each participant durably promises it can commit. The coordinator then records and broadcasts commit or abort. The protocol protects atomicity but can block when the decision is unavailable.
What problem does 2PC solve?
Some invariants span resources: debit one account and credit another, or reserve inventory and record payment. Without coordination, a partial success can expose an impossible business state.
Decision guide
Key trade-offs
| Choice | What you gain | What it costs |
|---|---|---|
| 2PC | Atomic outcome across transactional participants | Blocking, locks, and coordinator dependency |
| Saga | Independent local commits and long-running workflows | Visible intermediate states and compensations |
| Single-owner redesign | Simpler correctness and failure recovery | Less service or data independence |
What happens if?
Coordinator fails after every participant prepares
Participants cannot safely infer commit or abort. They remain in-doubt until the coordinator recovers or a replicated transaction manager exposes the recorded decision.
What happens if?
Commit succeeds but the client times out
The client must retry with the same transaction or idempotency identifier. Treating the retry as new work can double-apply the business operation.
When to avoid it
Do not stretch synchronous 2PC across unreliable, independently operated services by default. If the business can tolerate intermediate state and compensating actions, a saga often aligns better with service ownership.
Senior interview modeWhy is classic two-phase commit called blocking?Show answer
Once a participant votes yes, it cannot unilaterally abort or commit. If the coordinator’s decision is unreachable, the participant may hold prepared state and locks until that decision becomes available.