← Lessons

quiz vs the machine

Gold1450

System Design

The Saga Pattern

Replacing one giant lock with a chain of local commits and undo steps.

5 min read · core · beat Gold to climb

What a saga is

A saga breaks a long business transaction into a sequence of local transactions, one per service. Each step commits its own data immediately. If a later step fails, the saga runs a compensating transaction for every completed step in reverse order to logically undo the work.

Unlike two phase commit, a saga never holds locks across services, so it stays available even when a participant is slow.

Choreography vs orchestration

  • Choreography has each service emit an event that triggers the next service. There is no central brain, but the flow is spread across many handlers and can be hard to trace.
  • Orchestration uses a single orchestrator that tells each service what to do and reacts to results. The logic lives in one place and is easier to reason about.

Compensation is not rollback

A compensation is a new action, not a database rollback. Refunding a charge or releasing a reservation are typical examples. Because steps already committed, other readers may briefly see intermediate state.

Key idea

A saga trades cross service locking for local commits plus compensating actions that undo work on failure.

Check yourself

Answer to earn rating on the learn ladder.

1. What does a saga run when a later step fails?

2. How does orchestration differ from choreography?