In a monolith the database provides atomicity: a single COMMIT confirms the change as a whole, ROLLBACK undoes it. When an operation passes through several services, each with its own database, there is no common COMMIT. A saga carries the change through step by step, and if something fails it does not roll back what has been done but compensates for it with new transactions.
How a saga works
- The orders service creates the order with the status “new”.
- The payment service takes the money.
- The stock service cannot find the item and refuses the reservation.
- The payment service returns the money — a compensation for step 2.
- The orders service sets the order’s status to “cancelled” — a compensation for step 1.
Each step is an ordinary local transaction of its own database. A compensation is a new transaction too: it does not erase the old data but adds new — a refund record, the status “cancelled”. Along the way the customer may have seen “order accepted”, because a saga provides no isolation: the intermediate state is visible to everyone.
Choreography and orchestration
| Approach | How the steps are coordinated | Strength | Weakness |
|---|---|---|---|
| Choreography | services publish events and react to each other’s events | no central component, easy to start | the saga’s route is spread across the code and becomes hard to follow as the number of steps grows |
| Orchestration | an orchestrator calls the participants and collects their replies | the whole route is visible in one place | the orchestrator is a separate component with its own state |
What to consider in practice
- Steps and compensations are made idempotent: a message or a command may arrive twice.
- The saga’s status is stored explicitly (“new”, “paid”, “cancelled”) — a semaphore for concurrent operations.
- A compensation can itself fail: the steps are then retried, or the case is passed to a human.
- Not every step can be undone: an email that has been sent cannot be recalled. Such steps are placed after the point of no return (pivot transaction) — after it come only steps that are retried until they succeed.
Where the term comes from
The term was introduced by Hector Garcia-Molina and Kenneth Salem in the paper “Sagas” (1987): they called a saga a long transaction broken into a sequence of short ones, each with a compensating transaction attached. In the world of microservices the pattern was popularised by Chris Richardson in the microservices.io catalogue.
Sources
the term is written from these pages- Garcia-Molina, Salem — Sagas (SIGMOD 1987)dl.acm.org/doi/10.1145/38714.38742(opens in a new tab)
- Pattern: Saga — microservices.iomicroservices.io/patterns/data/saga.html(opens in a new tab)
- Saga design pattern — Azure Architecture Centerlearn.microsoft.com/en-us/azure/architecture/patterns/saga(opens in a new tab)
Verified 8 Nov