Alternative: Saga Pattern Introduction
What you'll learn: How the Saga pattern breaks complex distributed transactions into manageable local steps with undo capabilities.
The Core Problem
You've learned that distributed transactions using 2PC are blocking, expensive, and fragile. In many real-world systems, you don't actually need atomic commits across all services—you need a way to coordinate multi-step business processes that can gracefully handle failures.
What is the Saga Pattern?
A Saga splits a distributed transaction into a sequence of local transactions, where each local transaction updates data within a single service. If one step fails, the Saga executes compensating transactions to undo the work of preceding steps.
Think of it like booking a vacation: you reserve a flight, then a hotel, then a car. If the car rental fails, you don't need everything to stay locked—you cancel the hotel, then cancel the flight. Each cancellation is a compensating action.
Key Characteristics
- No distributed locking: Each step commits immediately in its own database
- Forward recovery: Continue execution with retries or alternative paths
- Backward recovery: Use compensating transactions to undo completed steps
- Eventual consistency: The system reaches a consistent state over time, not instantly
Example Flow
- Order Service creates order → commits locally
- Payment Service charges card → commits locally
- Inventory Service reserves items → fails
- Compensation: Payment Service refunds card
- Compensation: Order Service marks order as failed
The system traded atomicity for availability and scalability.
Key Takeaway: The Saga pattern replaces distributed transactions with a choreographed sequence of local transactions, using compensating actions to maintain consistency without blocking or distributed locks.