diagram.mmd — sequence
Saga Pattern sequence diagram

The saga pattern is a distributed transaction strategy that achieves data consistency across multiple microservices by breaking a multi-step operation into a sequence of local transactions, each publishing an event or message that triggers the next step, and using compensating transactions to undo completed steps if a later step fails.

Traditional ACID transactions work within a single database. In microservice architectures, an operation like "place an order" spans multiple services — each owning its own database. Two-phase commit (2PC) would work in theory, but it introduces tight coupling, blocking locks, and brittleness under partial failure. Sagas replace 2PC with an orchestrated or choreographed sequence of local transactions.

In the orchestrator-based saga shown here, a dedicated Order Orchestrator drives the workflow. It issues commands to each participating service sequentially and waits for responses before proceeding. This makes the workflow logic centralized and observable — you can query the orchestrator at any point to know where a saga stands. The trade-off is that the orchestrator becomes a critical path dependency.

Failure handling is what distinguishes sagas from simple chained calls. When a step fails — say, inventory is unavailable — the orchestrator issues compensating transactions in reverse order: it asks the Payment Service to release the reserved funds, then marks the order as failed. Compensating transactions are not rollbacks in the database sense; they are explicit business operations ("cancel reservation") that undo the effect of the forward transaction.

The choreography variant eliminates the orchestrator: each service emits domain events and other services react. This is more loosely coupled but harder to observe and debug. Both approaches are compatible with Event Sourcing Pattern and the Dead Letter Queue pattern for handling saga step failures that exhaust their retry budget.

Free online editor
Edit this diagram in Graphlet
Fork, modify, and export to SVG or PNG. No sign-up required.
Open in Graphlet →

Frequently asked questions

The saga pattern is a distributed transaction strategy that achieves data consistency across multiple microservices by breaking a multi-step operation into a sequence of local transactions. Each local transaction publishes an event or sends a command triggering the next step. If a step fails, compensating transactions are issued in reverse order to undo the completed steps.
In the orchestrator variant, a dedicated saga orchestrator issues commands to each service sequentially and waits for success or failure responses. On failure, it issues compensating commands in reverse — for example, releasing a payment reservation after an inventory check fails. In the choreography variant, each service listens for domain events and emits its own events, with no central coordinator; services react to each other's events to advance the workflow.
Use the saga pattern when a business operation spans multiple microservices, each owning its own database, and you need data consistency without distributed locking. It replaces two-phase commit (2PC) in microservice architectures. Orchestrated sagas are preferable when workflow visibility and explicit failure handling are priorities; choreographed sagas are preferable when maximum loose coupling is the goal.
A frequent mistake is not designing compensating transactions to be idempotent — a compensating action may itself fail and need to be retried, so it must be safe to execute more than once. Another pitfall is not accounting for the gap between a step succeeding and the next step's command being issued: a crash in this window requires idempotent forward steps too. Teams also underestimate the difficulty of debugging choreographed sagas, where no single component holds the full workflow state.
In orchestration, a dedicated saga orchestrator directs each step by issuing explicit commands and waiting for responses. The entire workflow logic is centralised and queryable. In choreography, there is no central coordinator — each service listens for events and emits its own, with the workflow emerging from the interaction. Orchestration is easier to monitor and debug; choreography is more loosely coupled and scales better when services are independently developed by separate teams.
mermaid
sequenceDiagram participant OrchestratorService as Order Orchestrator participant PaymentService as Payment Service participant InventoryService as Inventory Service participant ShippingService as Shipping Service OrchestratorService->>PaymentService: Reserve payment ($99) PaymentService-->>OrchestratorService: Payment reserved OrchestratorService->>InventoryService: Reserve inventory (item-42, qty 1) InventoryService-->>OrchestratorService: Inventory reserved OrchestratorService->>ShippingService: Schedule shipment ShippingService-->>OrchestratorService: Shipment scheduled note">Note over OrchestratorService: All steps succeeded — commit saga OrchestratorService->>PaymentService: Confirm payment capture OrchestratorService->>InventoryService: Confirm inventory deduction OrchestratorService->>ShippingService: Confirm dispatch note">Note over OrchestratorService: --- Failure scenario --- OrchestratorService->>InventoryService: Reserve inventory (item-99, qty 1) InventoryService-->>OrchestratorService: Out of stock — FAILED note">Note over OrchestratorService: Trigger compensating transactions OrchestratorService->>PaymentService: Cancel payment reservation PaymentService-->>OrchestratorService: Payment released
Copied to clipboard