Tutorial
Compensate for a Partially Completed Workflow
When Half the Work Cannot Be Undone
An automation charges a card, then fails to create the order. It sends the message, then cannot update the record. The steps are individually correct and the sequence is now inconsistent.
A compensating action reverses the completed step, so the system returns to a state that makes sense. It is not a rollback, because the outside world does not participate in transactions.
The Distinction
Three concepts that are often conflated.
Rollback undoes a transaction atomically, which only works within one system.
Compensation performs a new action that offsets a previous one, such as a refund after a charge.
Retry attempts the same action again, which is appropriate only when the action did not take effect.
Compensation is a business operation, which is why it needs its own policy, its own approval rules and its own audit record.
The Worked Example
An order automation with four steps: reserve stock, charge the card, create the order, send confirmation.
Failure at step three, order creation. The compensations are, in reverse order: refund the charge, release the stock reservation.
Failure at step two, charging. The compensation is to release the stock.
Failure at step one. No compensation is needed.
The rule: compensate the completed steps in reverse order, and only those.
Designing Compensations
Four properties per step.
An inverse exists, or the step must be moved earlier so that a failure leaves nothing to undo.
The inverse is idempotent, so compensating twice does not refund twice.
The inverse is recorded, with its own identifier and status.
The inverse can fail, which needs a policy: retry, escalate, or leave a visible inconsistency for a human.
The fourth is the one teams miss. A compensation that fails silently leaves the system in a state nobody knows about.
Ordering and Partial Compensation
Two rules.
Reverse order, so the state unwinds the way it was built. Compensating the first step before the third leaves a window where the state is inconsistent in a new way.
Best effort, then report. If a compensation fails, continue with the rest and report the set, rather than stopping at the first failure. A partial compensation with a clear report is better than a stuck run.
What Cannot Be Compensated
Four cases, and they should shape the design.
An email or message already read. You can send a correction and you cannot unsend.
A customer decision influenced by an action. If the agent told the customer something, the state includes their knowledge.
A regulatory record, such as an invoice issued.
A physical action, such as a shipment dispatched.
For these, the design rule is to sequence them last, after everything that can fail has succeeded. That is why confirmation messages are the final step, not the first.
The Policy
Compensation needs a decision about authority.
Three tiers. Automatic for small amounts and reversible actions. Approved for anything above a threshold, with the compensation queued for a person. Prohibited where the action must instead be flagged for review.
That policy belongs in the same place as the tool allowlist, because a compensation is an action with consequences.
Recording It
The run record should show three things.
What was done. What was compensated, with references to the compensating transactions. And what remains inconsistent, if anything.
The third is the important one: an honest list of unresolved inconsistencies is what allows a person to fix them, and its absence is what produces a customer discovering the problem.
Testing
Three scenarios in the fixture.
Fail at each step and confirm the compensations run in reverse order with no duplicates. Fail a compensation and confirm the run reports rather than hangs. And replay the whole run after a compensation and confirm it does not double anything.
Those three catch most of the defects in a compensation design.
Next step
Read concurrent writes for the adjacent problem, or contact us to review a workflow's failure design.
FAQ
Question: Should compensations be automatic?
For reversible, small and well understood actions, yes. For anything financial above a threshold, a person should approve.
Question: What if the compensation itself is wrong?
That is why it is recorded and reviewed. A wrong compensation is a second incident, and the record is what makes it findable.
Question: Can we avoid compensations by ordering the steps?
Partly. Put the irreversible steps last and many compensations disappear. The remaining ones are the cost of spanning systems.