1️⃣ What Problem Does Saga Solve?
-
In a microservices system, one business process (like Place an Order) may need to update multiple services:
-
Order Service creates an order
-
Payment Service charges the customer
-
Inventory Service reserves the item
-
-
You can’t use a single database transaction across all these independent services.
-
Challenge: If one step fails, how do you keep data consistent?
✅ Saga Pattern is the solution.
It coordinates a series of local transactions with the ability to undo (compensate) when something fails.
2️⃣ Key Idea
-
Each microservice does its own local transaction.
-
If a later step fails, compensating actions roll back previous steps.
Think of booking a trip:
-
Book Flight → Book Hotel → Book Taxi
-
If the hotel booking fails, you cancel the flight.
3️⃣ Two Main Saga Styles
A. Choreography (Event-Driven)
-
No central controller.
-
Services communicate through events.
-
Each service listens for events and reacts.
Flow:
-
Order Service saves order → publishes
OrderCreated
. -
Payment Service listens → charges payment → publishes
PaymentDone
. -
Inventory Service listens → reserves stock → publishes
InventoryReserved
. -
If any step fails, a compensating event is published (e.g.,
PaymentFailed
→ Order Service cancels the order).
✅ Good for simple workflows
❌ Hard to track when flows become complex.
B. Orchestration (Central Coordinator)
-
A central Saga Orchestrator service tells each service what to do next.
-
Orchestrator sends commands and handles success/failure responses.
Flow:
-
Orchestrator → tells Order Service: “Create order.”
-
After success → tells Payment Service: “Charge payment.”
-
After success → tells Inventory Service: “Reserve stock.”
-
If payment fails → tells Order Service: “Cancel order.”
✅ Easier to manage complex processes
❌ Single orchestrator can become a bottleneck if not scaled.
4️⃣ Saga in .NET (Common Tools)
Message Broker
-
RabbitMQ
-
Azure Service Bus
-
Kafka
Libraries
-
MassTransit: .NET library that supports Saga state machines.
-
NServiceBus: Enterprise-grade messaging with saga support.
-
Dapr Workflow (for orchestration).
5️⃣ Example Flow with MassTransit (Simplified)
-
Define Events/Commands
-
OrderCreated
,PaymentCompleted
,InventoryReserved
.
-
-
Saga State Machine
-
Tracks the order state and next steps.
-
-
Services
-
Order API, Payment API, Inventory API listen to commands/events.
-
-
Compensation
-
If
PaymentFailed
, sendCancelOrder
to revert.
-
6️⃣ Benefits
-
✅ Data consistency across microservices without a single database transaction.
-
✅ Flexible—works with event-driven systems.
-
✅ Scalable—each service remains independent.
7️⃣ Things to Watch Out For
-
More infrastructure: need a message broker and reliable event handling.
-
Proper error handling and retries are crucial.
-
Ensure idempotency (re-processing an event should not double-charge).
🔑 Quick Recap
-
Saga = sequence of local transactions + compensating actions.
-
Choreography: services react to events (no central brain).
-
Orchestration: a coordinator tells services what to do.
-
Perfect for .NET microservices where multiple services must stay consistent without using distributed transactions.