Design Patterns

Saga Pattern

Understanding the saga pattern for distributed transactions.

3 min readConcepts · System Design

What is the Saga Pattern?

The Saga Pattern manages distributed transactions across multiple services by breaking them into a sequence of local transactions. Each step has a compensating transaction to undo its work if a later step fails.


The Problem

Distributed transactions are hard:

The ProblemFlowchart

How Sagas Work

How Sagas WorkFlowchart
ConceptDescription
Transaction (T)Local transaction in a service
Compensation (C)Undo action for a transaction
SagaSequence of T₁, T₂, ..., Tₙ with compensations C₁, C₂, ..., Cₙ

Saga Types

Choreography

Services react to events, no central coordinator:

ChoreographyFlowchart

Pros: Decoupled, simple for few steps Cons: Hard to track, complex flows

Orchestration

Central coordinator manages the flow:

OrchestrationFlowchart

Pros: Easy to understand, centralized logic Cons: Orchestrator can be bottleneck


Example: Order Saga

Example: Order SagaFlowchart

Failure Scenario

Failure ScenarioFlowchart

Implementation Tips

ConsiderationApproach
IdempotencyMake operations repeatable safely
Correlation IDTrack saga instance across services
TimeoutHandle stuck sagas
MonitoringTrack saga state and failures

Saga vs 2PC

AspectSaga2PC
ConsistencyEventualStrong
AvailabilityHigherLower
ComplexityBusiness logicProtocol
ScalabilityBetterWorse
RecoveryCompensationRollback

Interview Tips

  • Explain choreography vs orchestration
  • Describe compensation transactions
  • Discuss idempotency requirement
  • Know trade-offs vs 2PC
  • Give examples: e-commerce order flow

Finished reading?

Mark it done to track your progress in Concepts.