Skip to content
BENIBIX Learn

Saga Pattern

Development🏗️ ArchitectureEvents and Messages

Also known as Saga, Distributed Transaction, Compensating Transaction

Split a task across services into steps. Each step has its own undo.

Think of it like

Think of it like booking a trip step by step. If the hotel is full, you cancel the flight you booked.

Example

Lucy's travel site books a flight, then a hotel, then a car. The car step fails, so the saga cancels the hotel and then the flight, and the customer is not charged.

Why it matters

In microservices there is no single transaction to roll back. Sagas are how data stays right when one step fails.

Definition

The saga pattern handles a task that spans many services. One database transaction cannot cover it all. The task is split into steps, and each service does its own step. Every step has an undo step, called a compensating action. If a later step fails, the undo steps run in reverse to put things back. The idea comes from a 1987 paper on long-running database work.

Common mistake

Forgetting that undo is not always possible. A sent email cannot be unsent, so plan what to do next.

Key takeaways

  1. 1
    Steps across services. Each does its part.
  2. 2
    Every step has an undo. A compensating action.
  3. 3
    Fail, then reverse. Undo in back order.