Monday, 22 September 2025

Saga Pattern

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:

  1. Order Service saves order → publishes OrderCreated.

  2. Payment Service listens → charges payment → publishes PaymentDone.

  3. Inventory Service listens → reserves stock → publishes InventoryReserved.

  4. 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:

  1. Orchestrator → tells Order Service: “Create order.”

  2. After success → tells Payment Service: “Charge payment.”

  3. After success → tells Inventory Service: “Reserve stock.”

  4. 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)

  1. Define Events/Commands

    • OrderCreated, PaymentCompleted, InventoryReserved.

  2. Saga State Machine

    • Tracks the order state and next steps.

  3. Services

    • Order API, Payment API, Inventory API listen to commands/events.

  4. Compensation

    • If PaymentFailed, send CancelOrder 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.