Video in progress

Strong Middle 🧩 Microservices

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:

  1. Compensation is idempotent. If refund(paymentId) is called twice, the money isn't refunded twice; the provider gets an idempotency key too.
  2. The saga state is stored. The orchestrator writes the COMPENSATING state and which step has been reversed to the database. Even if the pod restarts, it continues.
  3. Retry with backoff, for a long time. For minutes or hours, because an external provider may be down for an hour.
  4. The last resort is manual. When attempts run out, the saga moves to the FAILED_NEEDS_ATTENTION state, 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.