SAGA Pattern Compensations
Maintain eventual consistency across microservices with compensating transactions instead of 2PC.
In distributed systems, committing transactions across multiple database nodes with Two-Phase Commit (2PC) blocks resources and limits scale. The SAGA pattern solves this by executing a series of local transactions.
If a transaction fails, SAGA triggers a sequence of compensating transactions to roll back updates in reverse order.
๐ SAGA Failure and Compensation
Normal Flow:
[ Create Order ] โโโบ [ Authorize Payment ] โโโบ [ Reserve Inventory ] (Failed!)
โ
โผ
Compensating Flow (Rollback): โ
[ Cancel Order ] โโโ [ Refund Payment ] โโโโโโโโโโโโโ
- Orchestration: A central orchestrator service coordinates the steps and commands.
- Choreography: Each service publishes events, and other services react, triggering the next step without a central manager.


