Skip to content
API Design Learn/Event-Driven APIs
Browsing as a guest. Sign in to save your progress and earn XP as you complete chapters.

The Saga Pattern

4 min read

You'll learn to

  • -Explain why a database transaction cannot cover a workflow that spans several services
  • -Build a saga as local steps with compensating actions, and watch a failure undo the earlier steps
  • -Compare orchestration (one coordinator) with choreography (services reacting to each other's events)

A transaction in a single database guarantees all-or-nothing across several writes. That guarantee does not reach across service boundaries, where each service owns its own data store. A saga is the pattern for getting the same "everything succeeds, or everything is undone" outcome across a workflow of several services, without one distributed transaction spanning all of them.

An Analogy: Booking a Holiday

You book a flight, then a hotel, then a rental car, each with a different company. If the car company turns you down, you do not get a magic "undo all" button. You phone the airline and the hotel and cancel what you already booked. Each booking has a matching cancellation, and you run the cancellations for the steps that succeeded. That is a saga.

The Problem: A Multi-Service Workflow

Placing an order might involve charging a payment, reserving inventory and scheduling a shipment: three services, three databases. If the inventory reservation fails after the payment has already succeeded, something must refund the payment. A plain database transaction cannot span all three to make that happen on its own.

Compensating Actions

  • -Forward steps: ChargePayment, then ReserveInventory, then ScheduleShipment.
  • -Compensating actions: RefundPayment, ReleaseInventory, CancelShipment.
  • -If ReserveInventory fails, the saga runs the compensation for every step that already succeeded, in reverse order. Here that is RefundPayment alone, because the reservation never completed.

A saga is a sequence of local transactions, each inside one service's own database, so each step is an ordinary safe transaction. Each step that can be undone has a compensating action defined for it. Note that a compensation is not always a perfect reversal. A shipment that has already left the warehouse might be "intercepted and returned" rather than canceled, and that is a real design nuance.

Orchestration: A Central Coordinator

In orchestration, a dedicated coordinator calls each step in turn and triggers the compensations on failure. The workflow lives in one visible place, which makes the sequence easy to understand and change. The cost is that the coordinator becomes a service every other service in the saga depends on. The first demo is a tiny orchestrator, run once with every step succeeding and once with a failure in step 2.

Press Run: the failure triggers a refund, in reverse order, for the steps that had succeeded

Choreography: Services Reacting to Events

In choreography there is no coordinator. Each service publishes an event when its own step completes, and the next service subscribes and performs its step. This avoids the single coordinator, but the workflow becomes implicit, scattered across every service's subscriptions. Understanding "what happens when an order is placed" means tracing a chain of subscriptions across services, not reading one coordinator's code. The second demo shows the same chain with no coordinator at all.

Press Run: three services, no coordinator, and a workflow you can only see by reading all of them
Orchestration vs. Choreography
Central coordinator, explicit workflow, one new dependency
Orchestration
No coordinator, fully decoupled, workflow logic scattered across services
Choreography

Orchestration tends to scale better for workflows with many steps, or that change often, because the sequence lives in one place. Choreography fits a small number of steps, with services that are already event-driven for other reasons. Neither is a universal default.

Interview Signal is part of Pro

See a real weak answer next to a real strong one for this exact topic.

Quiz is part of Pro

Test what you just read with a short quiz, and bank the XP.

Ready to Build This?

Design Saga Master in the API Design Lab's gRPC & Event-Driven act.

Discussion0

Join the Discussion

Sign in to leave comments, reply to others, or like insights.

Sign In to ScaleDojo

No comments yet. Be the first to start the thread!