Video in progress
What do you do when a compensation step in a saga itself fails?
A compensation is retried "until it's done": it must be idempotent and eventually succeed with retries. If it keeps failing, the saga moves to a manual-review state and an alert goes to an operator; it's never silently dropped.
📖 Answer
The saga's core assumption is that compensation is always possible in the business sense. "Refund the payment" can temporarily fail because of the network or the provider, but logically it must not be refused. So:
- Compensation is idempotent. If
refund(paymentId)is called twice, the money isn't refunded twice; the provider gets an idempotency key too. - The saga state is stored. The orchestrator writes the
COMPENSATINGstate and which step has been reversed to the database. Even if the pod restarts, it continues. - Retry with backoff, for a long time. For minutes or hours, because an external provider may be down for an hour.
- The last resort is manual. When attempts run out, the saga moves to the
FAILED_NEEDS_ATTENTIONstate, shows up in the support panel and someone resolves it.
public enum SagaStatus {
STARTED, RESERVED, PAID, COMPLETED,
COMPENSATING, COMPENSATED,
FAILED_NEEDS_ATTENTION
}
There's help at the design level too: order the steps so that a step that can't be undone (sending an SMS, shipping goods) comes last. That's called the "pivot" step; after it the saga only moves forward, and instead of compensating it retries.
On one project, compensation errors were only written to the log, and a few dozen "hanging" reservations piled up each month. They were found only during inventory counts. After we stored the state in a table and added a metric that counts stuck sagas every day, the problem became visible.
Sign up to mark studied questions and save your progress.