Transactions across services: saga and outbox
The setup: order service, payment service, inventory service — each properly owning its own database. An order requires all three to change together. And here's the wall: there is no transaction spanning three databases owned by three services. Tier 3's beautiful ACID machinery stops at the service boundary. What replaces it? A famous failed answer, then the two patterns that actually run the world's order flows.
Two-phase commit: correct, blocking, and mostly absent
The textbook answer: a coordinator asks every participant "prepared to commit?"; when all say yes, it orders "commit." Atomic across systems — in theory. In practice, two fatal flaws: participants hold locks between prepare and commit, and if the coordinator dies in that window, they hold them indefinitely, blocked, waiting for a verdict that isn't coming. And it requires every participant to speak the 2PC protocol — which most modern datastores don't, and no plain HTTP API ever will. Verdict: correct, brittle, and rarely used across service boundaries. Know it mainly to explain its absence.
Sagas: a chain of small transactions, each with an undo
A saga decomposes the big transaction into a sequence of local ones — each atomic within its own service — where every step carries a compensating action that semantically reverses it:
reserve inventory → charge card → confirm order
↑ compensate: release ↑ compensate: refund
If step 2 fails, run step 1's compensation, and the world returns to (roughly) where it started. Two ways to wire the chain:
- Choreography — no boss. Each service reacts to the previous one's events: inventory hears "OrderPlaced" and reserves; payment hears "InventoryReserved" and charges. Loosely coupled, no coordinator to keep alive — and after five steps and a couple of failure branches, nobody can tell you what the flow actually is without reading five codebases. The logic exists only as an emergent property.
- Orchestration — one coordinator explicitly drives: call inventory, then payment, then confirmation; on failure, run compensations in reverse. The entire flow lives in one readable, debuggable place. The cost: that place is now a component whose availability you own.
Now the subtlety that separates people who've read about sagas from people who've operated one: compensation is not rollback. A rollback erases history as if nothing happened. A compensation is a new action in a world where the original visibly occurred. You cannot un-send an email — you send an apology. You cannot un-charge a card instantly — you issue a refund that takes days to appear. Which means intermediate states — "charged but not yet confirmed" — are visible to real users, and deciding whether that's acceptable, and what the customer experiences during it, is a product decision wearing engineering clothes. Bring the PM.
The outbox pattern: fixing the subtlest bug in event-driven systems
Sagas run on events, which surfaces a trap so quiet most teams ship it without noticing. The naive publish:
db.commit(order) // succeeds kafka.publish(OrderCreated) // fails — network blip
Your database says the order exists; the rest of the company never hears about it. No inventory reservation, no email, no shipment — a silent, permanent hole. Flip the order? Worse: you announce orders that then fail to commit, and downstream services act on fiction with no way to recall it. The root problem: a database write and a message publish are two systems, and no transaction covers both.
The fix is elegant: make them one system, temporarily. Write the event into an outbox table in the same local transaction as the business data — one database, one transaction, genuinely atomic: order and its announcement commit together or not at all. Then a separate relay process reads unsent outbox rows, publishes them to the broker, and marks them sent — retrying on failure until delivery succeeds. Delivery becomes at-least-once (the relay can crash after publishing, before marking) — which is fine, because Lesson 6.04 already made all your consumers idempotent. See how the pieces lock together? That's the tier working as one machine.
Publishing an event from inside a transaction that later rolls back. The commit fails, the order never existed — but the event already escaped, and subscribers have already reserved inventory and emailed a confirmation for a ghost. There is no un-publish. The rule is absolute: events describing a transaction's outcome leave the building only after the commit — which, done properly, means: always outbox.
The tradeoff sheet
- Problem solved
- A business operation spanning services where a distributed transaction is unavailable and a partial failure would leave the system inconsistent.
- Rejected, and why
- Keeping the operation inside one service and one transaction is dramatically simpler and is the right answer more often than the boundaries suggest. Two-phase commit exists and takes availability from every participant.
- The trade
- Every step needs a compensating action that must itself be reliable, and intermediate states become visible to users — an order can exist while its payment does not.
- First to break at 10x
- Compensation coverage. At 10x, the rare paths stop being rare, and the compensations nobody tested run for the first time under load.
- What it does to the bill
- Modest infrastructure, meaningful complexity. The outbox adds a table and a relay; the saga adds state nobody budgeted for.
- Day-2 operations
- You need a view of in-flight sagas and their age, because the failure mode is a saga stuck halfway with no error anywhere. Alert on age, not on error rate.