Distributed Transactions
maang.io System Design Series
A distributed transaction must be all-or-nothing across multiple databases or services. The classic answer, two-phase commit (2PC), coordinates a prepare-then-commit handshake — but a crashed coordinator can leave participants blocked, holding locks. That failure mode is why scalable systems often prefer the Saga pattern (chained local transactions with compensations) and TCC instead.
This deep dive comes in three parts:
- Foundations & Core Concepts — atomic commit, 2PC, and the coordinator-blocking problem.
- Internals — 3PC, Saga (orchestration vs choreography), TCC, and the transactional outbox, with worked numbers.
- Interview — Staff/Principal Q&A: 2PC vs Saga, idempotency, and failure handling.
Distributed Transactions — Part 2: Internals & Protocols
maang.io System Design Series
What is this?
Back to the travel agent. In Part 1 she asked each vendor "are you ready?" before booking anything — a careful two-step dance. Now we're going to watch that dance frame by frame, learn its exact rules, and then — critically — watch what happens when the agent faints between the two steps, leaving three vendors holding reservations and no idea whether to charge them. That failure is the single most important thing in this chapter, because it's the reason half the industry stopped using the agent altogether and switched to a different strategy entirely.
We'll dissect four mechanisms: Two-Phase Commit (and why it blocks), Three-Phase Commit (and why it doesn't really save you), the Saga (do-and-compensate), and the humble transactional outbox that quietly powers most real systems.
The one-line idea: every protocol here is fighting the same enemy — a crash at the worst instant — and each makes a different peace with it. 2PC buys strong atomicity but can freeze; sagas never freeze but give up isolation; the outbox sidesteps the fight by shrinking it back to a single local transaction plus safe retries.
Sign in to continue reading
The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.
Sign in to read the full lesson