Yashotantra Academy logo

5 · Real-World Architectures

9. Saga Pattern & Distributed Transactions

Two-Phase Commit (2PC), Choreography vs Orchestration Sagas, and compensating transactions.

10 min read · 3 MCQs

The Problem with Two-Phase Commit (2PC)

Traditional distributed transactions use 2PC (Prepare and Commit phases). 2PC is a blocking protocol: if the coordinator crashes during the commit phase, all participating service databases hold locks indefinitely, destroying system availability.

The Saga Pattern

A Saga breaks a distributed transaction across microservices into a sequence of local transactions. Each step updates its local database and publishes an event. If a subsequent step fails, the Saga executes compensating transactions backwards to undo changes.

Orchestration vs Choreography

Choreography: Services react to each other's domain events with no central controller (decoupled, but complex to trace). Orchestration: A central orchestrator state machine (e.g. Temporal, AWS Step Functions) explicitly commands each service and handles rollbacks.

Chapter quiz

3 questions · pass mark 75%
  1. 1. Why is the Saga pattern preferred over 2PC in cloud-native microservices?

  2. 2. What is a compensating transaction in a Saga?

  3. 3. In an Orchestrated Saga, what drives the workflow?