Course contentsShow
← Cloud and Infrastructure
Lesson 2580 of 2,98955. Microservices on CloudFree lesson

Saga Pattern for Distributed Transactions

Coordinating multi-service transactions using choreography or orchestration-based saga patterns.

Saga Pattern for Distributed Transactions

What you'll learn: How to coordinate multi-service transactions across microservices without traditional ACID guarantees using the Saga pattern.

The Challenge of Distributed Transactions

In microservices, a single business operation often spans multiple services—each with its own database. Traditional database transactions don't work across service boundaries. The Saga pattern solves this by breaking large transactions into a sequence of smaller, local transactions with compensating actions for rollback.

Two Approaches to Sagas

Choreography-Based Saga

Services react to events published by other services without a central coordinator. Think of a relay race where each runner knows to start when they receive the baton.

Example flow:

  1. OrderService creates order → publishes OrderCreated
  2. InventoryService reserves items → publishes InventoryReserved
  3. PaymentService charges customer → publishes PaymentProcessed
  4. If payment fails → PaymentService publishes PaymentFailed
  5. InventoryService reacts → releases reservation (compensating action)

Orchestration-Based Saga

A central orchestrator service explicitly tells each participant what to do and when. Like a conductor directing an orchestra.

Example flow:

  1. SagaOrchestrator sends ReserveInventory command
  2. Waits for response
  3. Sends ProcessPayment command
  4. On failure → sends ReleaseInventory compensation command

Choosing Your Approach

Choreography works well for simple flows with few services but can become complex as interactions multiply. Orchestration provides better visibility and control for complex workflows but introduces a central dependency.

Key Takeaway: The Saga pattern enables distributed transactions by chaining local transactions with compensating actions, using either event-driven choreography or centralized orchestration to maintain eventual consistency across microservices.