Bookmarks

Saga

A pattern for running a distributed transaction: the change is split into the services’ local transactions, and if something fails, what has been done is undone with compensating transactions.

Also: saga pattern, saga, compensating transaction

checked 8 Nov

All terms

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

  1. The orders service creates the order with the status “new”.
  2. The payment service takes the money.
  3. The stock service cannot find the item and refuses the reservation.
  4. The payment service returns the money — a compensation for step 2.
  5. 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

ApproachHow the steps are coordinatedStrengthWeakness
Choreographyservices publish events and react to each other’s eventsno central component, easy to startthe saga’s route is spread across the code and becomes hard to follow as the number of steps grows
Orchestrationan orchestrator calls the participants and collects their repliesthe whole route is visible in one placethe 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

Verified 8 Nov

Not to be confused with

what it is mixed up with and how it differs

  • ACIDACID transaction properties

    An ACID transaction in a single database is rolled back as a whole; a saga runs across services, and the rollback is replaced with compensations.

  • transactional outbox pattern

    The outbox guarantees that an event is delivered together with the write to the database; a saga coordinates the sequence of steps taken by different services.

In depth in the references

where the topic is covered in detail