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.
By Ratish Jain · Updated
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
- 1The saga starts and executes the first local transaction (e.g. create order).
- 2On success, it triggers the next step — either by publishing an event (choreography) or a central orchestrator issuing a command.
- 3Each step commits independently in its own service and database.
- 4If a step fails, the saga invokes compensating transactions for all completed steps, in reverse order.
- 5Compensation is semantic undo (refund, release stock), not a literal DB rollback.
- 6The saga completes as either fully committed or fully compensated.
Choreography vs Orchestration
| Choreography | Orchestration | |
|---|---|---|
| Control | Decentralized via events | Central orchestrator |
| Coupling | Loose, no coordinator | Services coupled to orchestrator |
| Visibility | Hard to trace flow | Flow explicit in one place |
| Best for | Few, simple steps | Many, complex steps |
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
- No distributed locks or blocking 2PC.
- Services stay loosely coupled and independently deployable.
- High availability under partial failure.
- Works with heterogeneous data stores.
- 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
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 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.
- →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.