System design lab · Episode 16

Saga: one order, three services, and a step that fails

An order needs three services: payment charges the card, stock reserves the item, shipping books a courier. Each has its own database, so there is no single transaction around all three. What if shipping fails after the money is taken? Follow the lessons next to the picture.

  1. Episode 1Load balancer
  2. Episode 2Cache
  3. Episode 3Rate limiter
  4. Episode 4Message queue
  5. Episode 5Database index
  6. Episode 6Replication and consensus
  7. Episode 7Circuit breaker
  8. Episode 8Backpressure
  9. Episode 9Retries and jitter
  10. Episode 10Consistent hashing
  11. Episode 11Partitioned log
  12. Episode 12Write-ahead log
  13. Episode 13Quorums
  14. Episode 14Unique IDs
  15. Episode 15The last ticket
  16. Episode 16Saga
  17. Episode 17Cache problems
  18. Episode 18Bloom filter
ORDERSPAYMENTSTOCKSHIPPING
Ball: “do your part”. Tile: a decision. Red tile: an undoOrder service at the left, three services at the right, the last orders at the front

Controls

How the order service runs an order
Shipping
Coordinator (two-phase commit only)
Time

Want the details?

Why is this hard?

Inside one database, a transaction makes several changes happen all together or not at all. But payment, stock and shipping are separate services, each with its own database. No single transaction covers all three. If the third step fails after the first two succeeded, something must clean up, or the customer pays for nothing.

How to read the bench
Part on the benchWhat it means in a real system
Box at the leftThe order service, which runs each order (the coordinator).
Three boxes at the rightPayment, stock and shipping services.
Ball“Do your part” (or, in two-phase commit, “prepare your part”).
Flat tileCommit or abort. A red tile is an undo (a compensating step).
PadlockA service holding a prepared order locked.
Cards at the frontThe last orders: dark shipped, white cancelled cleanly, red broken.
The numbers
  • A new order every 2 seconds. Each message takes 0.3 s; each service needs 0.4 s for its part.
  • After 20 seconds: no plan with shipping down, 9 broken; saga with shipping down, 8 cancelled cleanly; saga when every third order cannot ship, 6 shipped and 2 cancelled; two-phase commit with everything working, 9 shipped.
  • Coordinator crash at 3 s: all three services stay locked, and after 12 s, 12 requests wait with only 1 order shipped.
In real systems
  • Sagas come in two styles. Orchestration: one service runs the steps and the undos, as here. Choreography: each service listens for events on a queue and reacts (“payment done” → stock reserves). Orchestration is easier to follow; choreography has no central point.
  • Undos are new actions, not time travel: a refund is a new payment in the other direction, and it shows on the statement. Some steps cannot be undone (an email was sent), so put them last, or send a correction.
  • Every step and every undo must be safe to repeat, because messages are retried. Use an idempotency key per order and step.
  • The outbox pattern: a service must update its database and send “step done” together. Write the event into an outbox table in the same transaction, and let a separate process publish it. Otherwise a crash between the two loses the event.
  • Two-phase commit is still used inside databases and some message brokers, where the coordinator is reliable and close. Across independent web services it is rare: it blocks when the coordinator fails, and it holds locks for a whole network round trip.
Words to know
Saga
A long business transaction made of small local ones, each with an undo.
Compensating Transaction
The undo of one saga step, such as a refund.
Two-Phase Commit (2PC)
Prepare everywhere, then commit everywhere; all or nothing, but blocking.
Coordinator
The one that runs the steps and decides commit or abort.
Outbox
A table where a service writes events in the same transaction as its data, to publish them reliably later.