Saga Pattern in Microservices – Complete Guide with Real-World Examples

Saga Pattern is one of the most important microservice design patterns for managing distributed transactions. When multiple microservices work together to complete a business process, failures in one service can cause inconsistent data across the system. The Saga pattern ensures data consistency by breaking a transaction into smaller steps and applying compensating actions if a step fails.


 What is Saga Pattern?

The Saga Pattern is a sequence of local transactions where each step updates the database and publishes an event or calls the next step. If one transaction fails, a compensation transaction is executed to undo the changes made by previous steps.

Real-world analogy: Booking a trip → flight, hotel, and payment services are involved. If the hotel booking fails, the flight reservation must be cancelled, and the payment refunded.

 Types of Saga Patterns

There are two common ways to implement Saga in microservices:

  • 1. Choreography Saga – Decentralized, services communicate via events.
  • 2. Orchestration Saga – Centralized, a coordinator service manages the workflow.

1 Choreography Saga (Event-Driven)

Each service produces and listens to events. There is no central controller.

Scenario: Order Service in E-Commerce

  • User places an order → OrderCreatedEvent is published.
  • Payment Service consumes the event → processes payment → emits PaymentCompletedEvent or PaymentFailedEvent.
  • Inventory Service reduces stock on success, restores stock on failure.
  • Notification Service sends email/SMS updates.

// Payment Service listening to OrderCreatedEvent
@EventListener
public void handle(OrderCreatedEvent event) {
    if (processPayment(event.getOrderId())) {
        eventPublisher.publish(new PaymentCompletedEvent(event.getOrderId()));
    } else {
        eventPublisher.publish(new PaymentFailedEvent(event.getOrderId()));
    }
}

Pros: Decentralized, scalable, no single point of failure.
Cons: Harder to debug as system grows, risk of cyclic dependencies.


2 Orchestrated Saga (Centralized)

A dedicated Orchestrator Service controls the workflow, calling each service in sequence and triggering compensations on failure.

Scenario: Travel Booking

  • BookingOrchestrator calls Flight Service → Hotel Service → Payment Service.
  • If Hotel booking fails → orchestrator cancels flight and refunds payment.

class BookingOrchestrator {

    void bookTrip(String userId) {
        try {
            flightService.book(userId);
            hotelService.reserve(userId);
            paymentService.charge(userId);
        } catch (Exception e) {
            // Compensation logic
            flightService.cancel(userId);
            hotelService.cancel(userId);
            paymentService.refund(userId);
        }
    }
}

Pros: Centralized control, easier monitoring and debugging.
Cons: Orchestrator may become a single point of failure.


 Real-World Use Cases of Saga Pattern

  • E-Commerce Orders – Ensuring inventory, payment, and order states remain consistent.
  • Travel Booking – Coordinating flights, hotels, and payments reliably.
  • Banking Transactions – Transferring funds across multiple accounts securely.

 Final Thoughts

The Saga Pattern is essential for building resilient microservices. Whether you choose Choreography or Orchestration depends on your system complexity:

  • Use Choreography when services are small, independent, and event-driven.
  • Use Orchestration when workflows are complex and require central monitoring.

Pro Tip: Always implement proper logging, monitoring, and compensation logic when applying Saga patterns to production systems.

Post a Comment

0 Comments

Close Menu