SAGA Design Pattern

Last Updated : 23 Jun, 2026

The SAGA Design Pattern is used to manage distributed transactions across multiple microservices. It divides a large transaction into a series of smaller local transactions that are executed independently by different services.

  • Each service performs its own transaction and coordinates with other services through events or commands.
  • If a transaction step fails, compensating actions are executed to undo previously completed operations.

Example: In an e-commerce application, placing an order involves the Order Service, Payment Service, and Inventory Service. If payment fails after inventory is reserved, a compensating transaction releases the reserved inventory and cancels the order.

Distributed Transaction

A Distributed Transaction is a transaction that involves multiple systems or databases working together to complete a single task. All participating systems must successfully complete their part for the transaction to be considered successful.

Example: In an online shopping system, the bank processes payment, the warehouse updates stock, and the shipping service prepares delivery. If any step fails, the entire transaction may need to be rolled back.

Need of SAGA Design Pattern

The SAGA Design Pattern is needed to manage distributed transactions in microservices without using complex protocols like 2PC. It improves scalability, availability, and fault tolerance by allowing services to execute transactions independently.

  • Services perform local transactions independently, eliminating blocking and single points of failure.
  • Uses compensating transactions to undo completed steps and maintain data consistency when failures occur.

Working of SAGA Design Pattern

The SAGA Design Pattern manages distributed transactions by dividing them into a sequence of smaller transactions that execute independently across multiple services.

  • Breaking Down the Transaction: A large transaction is divided into smaller local transactions, with each service responsible for a specific step.
  • Independent Execution: Each transaction executes independently, and the next step starts only after the previous one completes successfully.
  • SAGA Execution Coordinator: A coordinator manages the sequence of transactions and ensures that each step is executed in the correct order.
  • Compensating Actions: If a transaction fails, compensating actions are triggered to undo the changes made by previously completed steps.
  • SAGA Log: A SAGA log records transaction progress and helps track successful steps and compensation activities during failures.

Example of SAGA Design Pattern

Let's understand how SAGA works using the example of an e-commerce order process with the SAGA Execution Coordinator and SAGA Log.

  • Step 1: Create Order: Reserve the product.
  • Step 2: Process Payment: Charge the customer’s card.
  • Step 3: Update Inventory: Reduce the stock.
  • Step 4: Deliver Order: Ship the product to the customer.

Working of SAGA

The SAGA Execution Coordinator controls the execution of each transaction step, while the SAGA Log tracks the status of all operations and compensating actions.

example-of-saga-design-pattern_
Example of SAGA Design Pattern
  • The SAGA Execution Coordinator manages the flow of these steps, triggering each one in sequence.
  • Each step has a compensating action that is triggered if something goes wrong (e.g., if payment fails, the product is unreserved).
  • The SAGA Log tracks the state of each step. It logs each step as in-progress, completed, or failed, and records any compensating actions needed.

Flow of SAGA

The SAGA process executes transactions step-by-step, and if any step fails, compensating actions are triggered to maintain system consistency.

flow-of-saga
Flow of SAGA

Start the SAGA

The process begins by executing the first step in the sequence.

  • Step 1: Execute First Transaction: The first local transaction is executed. If it fails, the corresponding compensating action is triggered and the process stops.
  • Step 2: Execute Second Transaction: If the first step succeeds, the second transaction is performed. If it fails, compensation actions undo both the current and previous successful transactions.
  • Step 3: Execute Third Transaction: After successful completion of Step 2, the third transaction is executed. Any failure triggers compensating actions to maintain consistency.
  • Step 4: Complete Final Transaction: The final transaction is executed after all previous steps succeed. If any earlier step fails, compensation mechanisms restore the system to a consistent state.

Approaches to Implemement SAGA Design Pattern

Below are the two main approaches to implementing the SAGA pattern:

1. Choreography-Based Approach (Event-Driven)

  • There is no central coordinator; each service knows what to do next and triggers the next step by emitting events.
  • Services communicate through events (e.g., via message queues or event streams). Each service listens for events and performs its own action when the event occurs.
  • Services operate independently and don’t need to know about each other’s details, just the events they need to react to.
  • If a service fails, it publishes a failure event, and other services can listen to it and perform compensating actions.
choreography-based-approach-event-driven_
Choreography-Based Approach (Event-Driven)

Example:

  • Order Service creates an order and publishes the "OrderCreated" event.
  • Payment Service listens for "OrderCreated" and processes payment, publishing "PaymentProcessed".
  • Inventory Service listens for "PaymentProcessed" and updates inventory, and so on.

2. Orchestration-Based Approach (Centralized)

  • A single SAGA Execution Coordinator (orchestrator) controls the flow of the entire saga.
  • The orchestrator tells each service when to start, what to do, and when to proceed to the next step.
  • The orchestrator has detailed knowledge of each service and their responsibilities in the saga.
  • The orchestrator manages failure recovery and calls the compensating actions if needed.
orchestration-based-approach-centralized_
Orchestration-Based Approach (Centralized)

Example:

  • The SAGA Execution Coordinator starts the saga and tells the Order Service to create the order.
  • Once the Order Service succeeds, the orchestrator tells the Payment Service to process the payment, and so on.

Advantages

The SAGA Pattern improves flexibility, scalability, and fault tolerance in distributed microservices systems.

  • Improves scalability by allowing services to execute transactions independently without global locking.
  • Avoids single points of failure since transactions are distributed across multiple services.
  • Provides better fault tolerance using compensating actions to handle failures gracefully.

Disadvantages

The SAGA Pattern can increase system complexity due to distributed transaction handling and compensating operations.

  • Managing compensating transactions can increase implementation complexity.
  • Debugging and tracking distributed transactions across services can be difficult.
  • Maintaining data consistency is challenging because transactions are eventually consistent instead of immediately consistent.
Comment

Explore