← Lessons

quiz vs the machine

Gold1380

Concurrency

The Saga Compensation Pattern

Run a multi step business transaction as local steps with undo actions when something fails.

5 min read · core · beat Gold to climb

The problem sagas solve

A business process may span several services, each with its own database. A single distributed lock across all of them is slow and fragile. A saga instead breaks the process into a sequence of local transactions, each committed independently.

Forward and compensating actions

Each step has a paired compensating action that semantically undoes it. The saga runs steps forward. If a step fails, it runs the compensations for the already completed steps in reverse order.

  • Book hotel, compensate by cancel hotel.
  • Charge card, compensate by refund card.
  • Reserve seat, compensate by release seat.

Compensation is not a rollback to a perfect prior state. A refund leaves a record of the charge and the refund. The saga restores business consistency, not byte level state.

Orchestration versus choreography

  • Orchestration uses a central coordinator that tells each service what to do next.
  • Choreography has each service emit events that trigger the next step, with no central brain.

Key idea

A saga replaces a distributed transaction with local steps plus compensating actions that undo completed work when a later step fails.

Check yourself

Answer to earn rating on the learn ladder.

1. What is a compensating action in a saga?

2. How does orchestration differ from choreography in sagas?

3. Why is compensation not a true rollback?