CMD Guide
HomeSystem DesignMicroservices Patterns

Saga — A Worked Orchestration Example

From definition to a real flow

You’ve seen what a Saga is (2PC vs Saga vs TCC): a sequence of local transactions, each with a compensating transaction that undoes it if a later step fails. Let’s walk a concrete orchestration — a central coordinator drives the steps.

The flow: place an order

  Orchestrator drives 3 local transactions, each in its own service/DB:

   1. Order Service     → CREATE order (status=PENDING)     compensate: CANCEL order
   2. Inventory Service → RESERVE items                     compensate: RELEASE items
   3. Payment Service   → CHARGE card                       compensate: REFUND

  Happy path:  1 → 2 → 3 → mark order CONFIRMED
  Payment fails at step 3:  run compensations in REVERSE → RELEASE items → CANCEL order

Step order is not cosmetic. Reserve (cheap, invisible to undo) comes before charge because the charge is the saga’s pivot — once real money moves, you are past the point of no return: a failure before the pivot aborts backward with internal-only compensations, while charging first and refunding on a failed reservation would move real money and claw it back — an externally visible inconsistency (and often a fee). Everything after the pivot must be retriable.

Saga: a sequence of local transactions with compensating transactions
Saga: a sequence of local transactions with compensating transactions

Orchestrator logic (pseudocode)

 placeOrderSaga(cart):
   order = orderSvc.create(cart)            # step 1
   try:
       inventorySvc.reserve(order)          # step 2
       try:
           paymentSvc.charge(order)         # step 3
       except PaymentError:
           inventorySvc.release(order)      # compensate 2
           orderSvc.cancel(order)           # compensate 1
           return FAILED
   except InventoryError:
       orderSvc.cancel(order)               # compensate 1
       return FAILED
   orderSvc.confirm(order); return OK

What makes it correct

Takeaways

What the worked example hides — production concerns

The pseudocode above illustrates the core idea, but a production orchestrator must survive realities that the happy-path drawing omits:

Sources: Designing Data-Intensive Applications (Kleppmann, ch. 9) for saga semantics and 2PC trade-offs; Building Microservices (Newman) and Azure Architecture Center saga guidance for orchestration patterns; Google SRE Book for idempotency and retry discipline.


Re-authored from-scratch for this guide. Diagrams adapted from Karan Pratap Singh’s System Design (MIT); patterns follow Azure Architecture Center / microservices.io / DDIA conventions.

Concretely: recovering a crash mid-compensation

Make the saga log a real state machine — PENDING → RESERVED → CONFIRMED on the happy path, or … → COMPENSATING → FAILED when a step at or before the pivot aborts — and write every transition durably before the next action. Now trace a crash during backward recovery: the charge (the pivot) is declined, the orchestrator writes COMPENSATING, calls release(reserve_id), and dies before cancel(order_id). On restart it replays the log, sees COMPENSATING with the release already recorded done, skips the completed release, and resumes at cancel. Because release and cancel are keyed by (sagaId, step), even a release that was in flight at crash time is safe to re-drive — the inventory service no-ops the duplicate. This is why the log records each compensation’s outcome, not merely that “compensation started”: resume has to know precisely which undo already happened.

Steps after the pivot (e.g. a confirm/ship step) are retriable: they are retried with backoff until they succeed and are never compensated backward — if a step can fail for a business reason, it must be placed before the pivot.

🤖 Don't fully get this? Learn it with Claude

Stuck on Saga — A Worked Orchestration Example? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Saga — A Worked Orchestration Example** (System Design) and want to truly understand it. Explain Saga — A Worked Orchestration Example from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Saga — A Worked Orchestration Example** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Saga — A Worked Orchestration Example** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Saga — A Worked Orchestration Example** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes