"Saga" is a distributed transaction pattern used in microservices to ensure data consistency without using 2-phase commit or distributed locks.
✔ In simpler words:
Saga ensures that a long-running business process composed of multiple microservice operations either completes successfully OR compensates (undoes) the already completed steps.
๐ Use Saga when:
- A single business operation touches multiple microservices, each with its own DB.
- You cannot use distributed transactions (like ACID/2PC).
- You need an eventual consistency model.
๐ฏ Why Saga Is Needed
Typical distributed system problems:
- Every service owns its database → no cross-service transaction possible.
- Operations may fail in the middle.
- You must prevent partial success (e.g., payment done but inventory not reserved).
Traditional ACID transactions do not work across services—Saga solves this using compensating actions.
๐ง Saga Pattern Key Principles
- A business workflow is broken into multiple local transactions.
- Each local transaction:
- Performs a step.
- Publishes an event.
- If any step fails → Saga triggers compensation (undo logic).
๐ Two Types of Saga: Orchestration vs Choreography
๐ ฐ️ 1. Choreography-Based Saga (Event-Driven)
Services talk to each other using events.
✔ Flow example:
- Order Service → "OrderCreated" event
- Inventory Service → reserves stock → "StockReserved"
- Payment Service → charges customer → "PaymentSuccess"
- Shipping Service → ships product → "OrderShipped"
If failure:
- Payment fails → publish "PaymentFailed"
- Inventory Service → receives and releases reserved stock
- Order Service → marks order as cancelled
✔ Pros
- No central coordinator
- Simple for small flows
- Very loosely coupled
❌ Cons
- Hard to visualize / debug
- Complex chains of events → “distributed spaghetti”
- Hard to evolve as workflow grows
๐ ฑ️ 2. Orchestration-Based Saga
A central Saga Orchestrator commands each step.
✔ Flow example:
- Orchestrator → ReserveInventory()
- Orchestrator → ChargePayment()
- Orchestrator → CreateShipment()
If failure:
-
Orchestrator calls CompensateInventory(), RefundPayment(), etc.
✔ Pros
- Centralized logic → easier to understand
- Traceable
- Easier for complex workflows
❌ Cons
-
Orchestrator becomes the “brain” → slightly more coupling
๐ฆ Real Example: E-Commerce Order Workflow
Step-by-step:
- Order Service
- Creates order
- Saga starts
- Inventory Service
- Reserves stock
- Payment Service
- Charges customer
- Shipping Service
- Schedules delivery
- Saga completes
If Payment fails:
Saga automatically:
- Cancels shipment
- Releases stock
- Cancels order
๐งฐ C# Example — Orchestration Saga Using MassTransit
This orchestrates:
- Reserve inventory
- Process payment
- Ship order
- Handle compensation
๐งฐ C# Example — Choreography Saga (Event-Driven)
Order Service publishes event:
Inventory handles event:
Payment handles event:
๐งจ Compensating Transaction Example
๐ฏ When to Use Saga
Use Saga when:
- Business processes affect multiple services.
- Rollbacks must be handled safely.
- Workflow is long-running (minutes → hours).
- Consistency between services is required.
Don't use Saga when:
- Single service owns all data.
- Strong ACID guarantees needed.
- Steps cannot be compensated.
No comments:
Post a Comment