← Lessons

quiz vs the machine

Gold1410

Concurrency

Sagas and Compensation

Replace one distributed transaction with local steps and compensating undo actions.

5 min read · core · beat Gold to climb

Long transactions without locks

A saga breaks a long business transaction into a sequence of local transactions, each on a single service. There is no global lock and no two phase commit. If a later step fails, earlier steps are undone by compensating transactions.

Forward and backward

The saga runs steps in order. Each step commits locally and becomes visible immediately.

  • On success it moves to the next step.
  • On failure it runs compensations for all completed steps in reverse, semantically undoing their effects.

Compensation is not a rollback to a prior state. A charged card is refunded, a reserved seat is released. The world moved forward and then moved forward again to cancel.

Coordination styles

  • Orchestration a central coordinator tells each service what to do and triggers compensations on failure.
  • Choreography services react to each other events with no central brain.

Because steps commit independently, a saga is not isolated. Other transactions can observe intermediate states, so designs must tolerate that.

Key idea

A saga trades atomic isolation for availability by chaining local transactions and undoing completed steps with compensating actions when a later step fails.

Check yourself

Answer to earn rating on the learn ladder.

1. How does a saga recover from a failed step?

2. What isolation does a saga provide?

3. What distinguishes orchestration from choreography?