🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
Search courses…
💼 Servicesℹ️ About✉️ ContactView Pricing Plansfrom $10

Design Saga Pattern

ArchitectureDistributed Transactions🟢 Free Lesson

Advertisement

Architecture

Design Saga Pattern

The saga pattern manages distributed transactions as a sequence of local transactions with compensating actions. If any step fails, compensating transactions undo previous steps.

  • Problem — Distributed transactions across microservices
  • Solution — Compensating transactions for rollback
  • Modes — Choreography (events) vs Orchestration (coordinator)

Sagas are the practical alternative to 2PC: trade immediate consistency for eventual consistency with compensation.

What Is a Saga?

DfSaga Pattern

A saga is a sequence of local transactions T1, T2, ..., Tn. Each Ti has a compensating transaction Ci. If T3 fails, the saga executes C2, C1 to undo T2, T1. This ensures the system reaches a consistent state without distributed locks.

Saga Execution

T1: ReserveT2: ChargeT3: ShipT4: Fail!C2: RefundC1: Release

Choreography vs Orchestration

DfChoreography

Each service listens for events and decides what to do next. No central coordinator. Services emit events when they complete their step. Simple but hard to track overall flow.

DfOrchestration

A central saga orchestrator coordinates all steps. The orchestrator tells each service what to do and handles compensations. Easier to understand and debug.

AspectChoreographyOrchestration
CouplingLoose (events)Tight (direct calls)
VisibilityHard to traceEasy to trace
ComplexityDistributedCentralized
Single Point of FailureNoneOrchestrator
Best ForSimple flowsComplex workflows

Compensating Transactions

DfCompensating Transaction

A compensating transaction reverses the effect of a completed transaction. It's not a true undo—it's a new transaction that produces the same net effect. For example, "refund payment" compensates "charge payment."

Compensating transactions can fail. Design retry mechanisms and manual intervention queues for failed compensations. The system should eventually reach consistency, even if some steps require human intervention.

Saga State Machine

Saga State Transition

State{STARTED,IN_PROGRESS,COMPENSATING,COMPLETED,FAILED}\text{State} \in \{\text{STARTED}, \text{IN\_PROGRESS}, \text{COMPENSATING}, \text{COMPLETED}, \text{FAILED}\}

Here,

  • STARTEDSTARTED=Saga initiated
  • INPROGRESSIN_PROGRESS=Executing steps
  • COMPENSATINGCOMPENSATING=Rolling back

Practice Exercises

  1. Design: Implement an order saga with: reserve inventory, charge payment, create shipment. What happens if payment fails?
  2. Compensation: Design a compensating transaction for "send email notification."
  3. Monitoring: How would you monitor saga progress and detect stuck sagas?
  4. Recovery: Design a recovery mechanism for sagas stuck in COMPENSATING state.

Key Takeaways:

  • Sagas replace distributed transactions with local transactions + compensation
  • Choreography uses events (loose coupling); orchestration uses a coordinator (visibility)
  • Compensating transactions are not true undos—they're new transactions
  • Design for compensation failures with retry and manual intervention
  • Saga state machine tracks progress and enables recovery

What to Learn Next

-> Outbox Pattern Reliable event publishing.

-> Idempotency Safe compensation semantics.

-> Circuit Breaker Saga step resilience.

-> Retry Patterns Retry in saga steps.

-> Design Amazon Checkout saga.

-> Design Uber Trip lifecycle saga.

Premium Content

Design Saga Pattern

Unlock this lesson and 900+ advanced tutorials with a Premium plan.

🎯End-to-end Projects
💼Interview Prep
📜Certificates
🤝Community Access

Already a member? Log in

Need Expert System Design Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement