What is Saga Pattern
Distributed transactions through event sequences
Saga Pattern is a pattern for managing distributed transactions through a sequence of local transactions with compensating actions on failures.
The Problem
- Microservices have their own databases
- Classic ACID transactions are impossible
- Consistency between services is needed
Saga Types
- Choreography — services exchange events
- Orchestration — central coordinator manages
Choreography
- Services subscribe to each other's events
- Each service publishes its result
- No single point of failure
- Harder to track state
Orchestration
- Saga coordinator manages the flow
- Calls services sequentially
- Easier to track and debug
- Risk of single point of failure
Compensating Transactions
- Undo already completed steps
- Must be idempotent
- Semantic undo (not rollback)
- Example: refund payment instead of cancel