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

8 min read

You'll learn to

  • -Design a saga as a sequence of local transactions with compensating actions, for a workflow spanning multiple services
  • -Distinguish orchestration (a central coordinator) from choreography (services reacting to each other's events) as two ways to implement a saga

A single-database transaction guarantees all-or-nothing across multiple writes - but that guarantee doesn't extend across service boundaries, where each service owns its own data store. A saga is the pattern for achieving an equivalent "all succeed, or everything is undone" outcome across a multi-service workflow, without a distributed transaction spanning every service.

The Problem: A Multi-Service Workflow

Placing an order might involve: charging a payment, reserving inventory, and scheduling a shipment - three separate services, each with its own database. If inventory reservation fails after payment already succeeded, something needs to undo the payment; a plain database transaction can't span all three services to make that automatic.

Compensating Actions

Each step in a saga has a matching compensating action to undo it
// Forward steps:                        // Compensating actions:
// 1. ChargePayment                       1. RefundPayment
// 2. ReserveInventory                    2. ReleaseInventory
// 3. ScheduleShipment                    3. CancelShipment

// If step 2 (ReserveInventory) fails, the saga runs the compensating
// action for every step that already succeeded, in reverse order:
// -> RefundPayment (undoing step 1, since step 2 never completed)

A saga is a sequence of local transactions, each scoped to one service's own database (so each individual step IS a normal, safe, all-or-nothing transaction), with a compensating action defined for each step that can undo its effect if a later step in the sequence fails. Note that a compensating action isn't always a perfect literal reversal - "cancel a shipment" that already left the warehouse might mean "intercept and return" rather than undoing something that hasn't happened yet, which is a real design nuance sagas have to account for.

Orchestration: A Central Coordinator

In orchestration, a dedicated saga coordinator service explicitly calls each step in sequence and explicitly triggers compensating actions on failure - the workflow logic lives in one visible place, which makes the overall sequence easy to understand and modify, at the cost of that coordinator becoming a service every other service in the saga now depends on.

Choreography: Services Reacting to Events

In choreography, there is no central coordinator - each service publishes an event when its own step completes, and the next service in the sequence subscribes to that event and reacts by performing its own step, publishing its own completion event in turn. This avoids the single coordinator dependency, but the overall workflow logic becomes implicit, scattered across every service's event subscriptions - understanding "what happens when an order is placed" means tracing a chain of event subscriptions across many services instead of reading one coordinator's code.

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 frequently, since the sequence lives in one place; choreography tends to fit better for a small number of steps with services that are already naturally 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.

ScaleDojo Logo
Initializing ScaleDojo