Problem
A business process spanning several services cannot use a single ACID transaction, and some steps cannot be rolled back by the database. If step three fails, steps one and two have already committed and visible effects exist.
Also known as: compensation, undo-steps, semantic-rollback
Undo the effects of completed steps in a multi-step process by running explicit inverse operations when a later step fails.
A business process spanning several services cannot use a single ACID transaction, and some steps cannot be rolled back by the database. If step three fails, steps one and two have already committed and visible effects exist.
Compensating actions are the rollback mechanism of the saga approach. Instead of preventing partial results, the system repairs them by performing business-level inverses.
Each forward step is paired with a compensation, and a coordinator or choreography tracks progress.
1 reserve inventory <-> release inventory
2 charge card <-> refund payment
3 create shipment <-> cancel shipment
step 3 fails -> run 2's refund, then 1's release
The saga state is stored durably, for example in a table or workflow engine such as Temporal or Step Functions, so recovery continues after a crash. Compensations run in reverse order, are retried with backoff, and carry idempotency keys. Where the world cannot be restored exactly, such as an email already sent, the compensation sends a correction instead.
Low, uses existing services
Medium to high, monitor stuck sagas
High, reasoning about partial states
Compensation fails repeatedly and leaves the process half-undone
Non-idempotent compensation runs twice, such as double refund
Other users act on intermediate state before it is undone