Skip to main content

Saga Pattern

The Saga pattern keeps data consistent across microservices by sequencing local transactions and undoing them with compensating actions when a step fails. It replaces unscalable distributed transactions in service-based workflows.

Organization
Microsoft
Published
Jan 1, 2018

Best Practice: Saga Pattern

The Saga pattern manages consistency across a business process that spans multiple microservices, each with its own database. Distributed two-phase-commit transactions do not scale or fit well in microservices, so a saga breaks a process into a sequence of local transactions. After each step succeeds, it triggers the next; if a step fails, the saga runs compensating transactions to undo the prior steps. The original concept comes from a 1987 paper by Garcia-Molina and Salem, and it is now a standard microservices pattern documented by Microsoft and others. It matters because without it, multi-service workflows leave data in inconsistent states when one service fails.

Step-by-Step Implementation Guidance

  1. Map the business process into discrete local transactions, one per service.
  2. Define a compensating transaction for each step that can semantically undo it.
  3. Choose coordination style: choreography (services react to each other's events) or orchestration (a central coordinator directs the steps).
  4. Make every step idempotent so retries do not cause duplicate effects.
  5. Persist saga state so an interrupted saga can resume or compensate.
  6. Handle failures explicitly, triggering compensation in reverse order.
  7. Add observability so you can trace a saga end to end across services.

Common Mistakes Teams Make When Ignoring This Practice

  • Reaching for distributed transactions that do not scale across services.
  • Forgetting compensating actions, leaving partial work stranded on failure.
  • Building non-idempotent steps that double-charge or double-ship on retry.
  • Choosing choreography for complex flows until the event web becomes unreadable.
  • Losing track of in-flight sagas because state is not persisted.

Tools and Techniques That Support This Practice

  • Orchestration engines such as Temporal, Camunda, or AWS Step Functions.
  • Message brokers like Kafka or RabbitMQ for choreography.
  • Idempotency keys and outbox patterns for reliable messaging.
  • Distributed tracing tools to follow a saga across services.

How This Practice Applies to Different Migration Types

  • Cloud Migration: Coordinate workflows that span on-premise and cloud services during a phased move.
  • Database Migration: Keep multi-service business processes consistent while data stores are split apart.
  • SaaS Migration: Orchestrate steps that touch both legacy systems and the new SaaS via compensations.
  • Codebase Migration: Replace in-process transactions with sagas as a monolith is decomposed into services.

Checklist

  • Decompose the process into per-service local transactions.
  • Define a compensating action for every step.
  • Choose orchestration or choreography deliberately.
  • Make all steps idempotent.
  • Persist saga state for recovery.
  • Implement reverse-order compensation on failure.
  • Add end-to-end tracing across the saga.