DataAdvanced

Payment failed after inventory was reserved. Walk me through the compensation.

“In an order saga, the payment step fails after inventory has been reserved. What runs, what state does the user see, and what can go wrong during compensation itself?”

What this tests

  • Compensation as semantic undo, not rollback
  • Persisted saga state and idempotent, retryable compensations
  • Handling failures of the compensation itself
  • Orchestration vs choreography for this flow

Answers by level

Read the beginner answer first and notice what is missing.

The saga has done Create Order and Reserve Inventory; Charge Payment failed. Compensation runs the undo steps in reverse order: Release Inventory, then Cancel Order. These are new forward transactions that semantically reverse the effect — the reservation row is marked released, not deleted, and the order moves to cancelled with a reason, not back to nonexistence. The user sees pending during the saga and payment failed at the end; they never see an order that briefly existed and vanished.

Compensation can fail too: the inventory service may be down. So the saga state (step 2 done, compensating step 2) is persisted, and the compensation is retried with backoff; it must be idempotent because retries will re-run it. If it keeps failing, the saga parks in a compensation failed state that is alerted and resolved by a person — you never leave a silently half-compensated order.

Green flags · Red flags

Strong green flag · Raises the late-success race (timeout, compensate, then provider confirms) and answers it with a refund path.
Green flags
  • Reverse-order semantic undo, with state history preserved
  • Persisted saga state; compensations are retried and idempotent
  • Handles "compensation failed" explicitly with alerting
  • Distinguishes timeout (unknown) from failure (known)
  • Places non-compensatable steps at the end
Red flags
  • "Compensation just rolls the transaction back."
  • Deletes rows during compensation and loses the audit trail
  • Retries compensation in a loop with no terminal failure state
  • Treats a payment timeout as a payment failure

Follow-up questions

F1
What if the release-inventory call times out?
F2
Orchestration or choreography for a 4-step order saga?
F3
How long may stock stay reserved?

Scenario

The choreographed order flow works until a Black Friday spike: the payment service returns 5xx for 12 minutes. Orders sit with inventory reserved; the inventory service never receives a release because the "PaymentFailed" event is only published on an explicit decline, not on a timeout. 8,000 units of a hot product show as out of stock while nobody has paid. Design the compensation and the state machine.

Learn this topic