Skip to main content

Command Palette

Search for a command to run...

SAGA Pattern Compensations

Maintain eventual consistency across microservices with compensating transactions instead of 2PC.

Updated
โ€ข1 min readโ€ขView as Markdown

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.