In a distributed microservices architecture, managing data consistency across services without using distributed transactions (which are fragile and complex) is a challenge. This is where the Saga Pattern comes into play.
In this blog, weβll explore:
- What is the Saga Pattern?
- The two types: Choreography and Orchestration
- Real-world analogies
- Use cases, pros/cons
- Code and tools you can use
π‘ What is the Saga Pattern?
A Saga is a sequence of local transactions in each microservice, where each step triggers the next one. If something goes wrong, compensating transactions roll back the work done by previous steps.
It avoids 2-phase commits (2PC) and is asynchronous and event-driven.
π§ Two Types of Saga Patterns
Aspect | Choreography | Orchestration |
---|---|---|
Coordination | Distributed (each service listens) | Centralized (one service coordinates) |
Communication | Events (event bus) | Commands/Events (often HTTP or messages) |
Coupling | Loose | Tighter (central service knows others) |
Complexity | Hidden (in event chains) | Visible (central logic) |
Debugging | Harder (no single control flow) | Easier (centralized logic) |
1οΈβ£ Choreography Saga
π¦ How It Works:
Each microservice emits an event after completing its task. Other services listen for events they care about and react accordingly.
π Example: Order Fulfillment (Choreography)
-
Order Service emits
OrderCreated
-
Payment Service listens β processes payment β emits
PaymentCompleted
-
Inventory Service listens β reserves stock β emits
StockReserved
- Shipping Service listens β ships order
[Order Service]
β
βββ emits β OrderCreated
β
[Event Bus]
β
[Payment Service] listens to OrderCreated
β
βββ emits β PaymentCompleted
β
[Event Bus]
β
[Inventory Service] listens to PaymentCompleted
β
βββ emits β InventoryReserved
β
[Event Bus]
β
[Shipping Service] listens to InventoryReserved
β
βββ emits β OrderShipped
β Pros:
- Fully decentralized
- Scales well
- No single point of failure
β Cons:
- Harder to understand the whole flow
- Event storming can lead to complexity
- Difficult to debug/troubleshoot
π οΈ Tools:
- Kafka, RabbitMQ, AWS SNS/SQS, NATS
2οΈβ£ Orchestration Saga
π§ How It Works:
A central orchestrator service controls the entire saga. It sends commands to services and awaits replies or emits events accordingly.
π Example: Order Fulfillment (Orchestration)
-
Order Service sends
StartOrderSaga
to Saga Orchestrator - Orchestrator β calls Payment Service via command (e.g., REST or Kafka)
- On success β orchestrator calls Inventory Service
- On success β orchestrator calls Shipping Service
- On failure β orchestrator triggers compensating actions
[Order Service]
β
βββ calls β [Saga Orchestrator]
β
βββββ> [Payment Service] β emits β PaymentCompleted
β
βββββ> [Inventory Service] β emits β InventoryReserved
β
βββββ> [Shipping Service] β emits β OrderShipped
β Pros:
- Clear control flow
- Easier to debug
- Business logic is centralized
β Cons:
- Orchestrator becomes a single point of failure
- More coupling
- Can turn into a God service if not modularized
π οΈ Tools:
- Camunda, Netflix Conductor, Axon, Temporal, or build-your-own with Spring Boot + Kafka
π€ When to Use What?
Use Case | Recommended Saga Type |
---|---|
Small services, loosely coupled | Choreography |
Complex workflows, need for audit/debugging | Orchestration |
Decentralized teams | Choreography |
Complex compensating logic | Orchestration |
π§ͺ Bonus: Compensating Transactions
In either style, if a step fails, you must run compensating transactions to undo the work.
Example: If PaymentCompleted
β but StockReserved
fails β emit PaymentRefunded
or call refund logic.
π Sample Technologies to Use
Task | Tools |
---|---|
Event Bus | Kafka, RabbitMQ, NATS, SNS/SQS |
Saga Orchestrator | Temporal, Camunda, Conductor, Axon |
Code Frameworks | Spring Boot, Micronaut, NestJS |
Observability | Zipkin, Jaeger, OpenTelemetry |
π Final Thoughts
Both Choreography and Orchestration are valid saga implementations. Choosing one depends on:
- Your team size and autonomy
- Workflow complexity
- Operational maturity
- Failure handling needs
π For smaller, loosely coupled microservices: Choreography is cleaner
π For large workflows with rollback needs: Orchestration is safer
Top comments (0)