Archprimer
PatternAdvanced 8 min read

Saga

A saga coordinates a distributed transaction as a sequence of local transactions, each with a compensating action to undo work on failure — via choreography or orchestration.

sagadistributed-transactionsmicroservicescompensation

By Ratish Jain · Updated

Diagram with 4 components: Orchestrator, Order Svc, Payment Svc, Shipping Svc. Connections: Orchestrator → Order Svc (1 create); Orchestrator → Payment Svc (2 charge); Orchestrator → Shipping Svc (3 ship); Payment Svc → Order Svc (compensate).
Service
Orchestrated saga with compensations

Definition

A saga implements a business transaction spanning multiple services as a series of local transactions. Each step commits in its own service; if a later step fails, the saga runs compensating transactions to semantically undo the already-committed steps.

Sagas trade atomic ACID guarantees for availability and loose coupling — they provide *eventual* consistency without distributed locks.

The Problem It Solves

Across microservices with separate databases, a classic two-phase commit (2PC) is slow, blocking, and tightly couples participants. Yet business operations like *place order → charge card → reserve stock → ship* must succeed or fail as a unit.

Sagas solve this without a distributed lock: each service commits locally, and failures are handled by explicit compensation rather than rollback.

How It Works

  1. 1The saga starts and executes the first local transaction (e.g. create order).
  2. 2On success, it triggers the next step — either by publishing an event (choreography) or a central orchestrator issuing a command.
  3. 3Each step commits independently in its own service and database.
  4. 4If a step fails, the saga invokes compensating transactions for all completed steps, in reverse order.
  5. 5Compensation is semantic undo (refund, release stock), not a literal DB rollback.
  6. 6The saga completes as either fully committed or fully compensated.

Choreography vs Orchestration

ChoreographyOrchestration
ControlDecentralized via eventsCentral orchestrator
CouplingLoose, no coordinatorServices coupled to orchestrator
VisibilityHard to trace flowFlow explicit in one place
Best forFew, simple stepsMany, complex steps

Design idempotent compensations

Design idempotent compensations

Compensations may be retried or arrive after a partial failure. Make each step and its compensation idempotent, and remember some actions (like sending an email) cannot be undone — sequence irreversible steps last.

Tradeoffs

Pros
  • No distributed locks or blocking 2PC.
  • Services stay loosely coupled and independently deployable.
  • High availability under partial failure.
  • Works with heterogeneous data stores.
Cons
  • Only eventual consistency; no isolation between steps.
  • Compensation logic is complex and error-prone.
  • Hard to debug, especially choreographed sagas.
  • Some actions cannot truly be compensated.

Interview Questions

Why not just use two-phase commit?

2PC requires all participants to hold locks until the coordinator decides, which blocks, hurts availability, and couples services. It also assumes all stores support XA transactions. Sagas avoid locks by committing locally and compensating on failure.

When would you choose orchestration over choreography?

When the workflow has many steps or complex branching. A central orchestrator makes the flow explicit, easier to monitor and modify. Choreography suits simple flows but becomes hard to trace as event chains grow.

60-Second Revision
  • →Saga = sequence of local transactions + compensating actions.
  • →Provides eventual consistency, not ACID isolation.
  • →Choreography = event-driven/decentralized; orchestration = central controller.
  • →Compensations must be idempotent and semantic undos.
  • →Avoids blocking 2PC across microservices.

Related

Everything on Archprimer is connected — follow the graph.