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.