A try that undoes what it finished
Sagas without compensation code: the undo is the try's own.
A money transfer touches more than one system. The bank's core debits the customer's account; a payment rail sends the money on. If the rail refuses after the debit went through, the customer has lost money that went nowhere. The debit must be reversed.
This pattern has a name, the saga: a sequence of steps across systems, each with a compensating action that undoes it, run in reverse when a later step fails. Every durable execution engine supports it. In most of them you write it by hand: keep a list of what to undo, push onto it after each step, and in the error handler walk it backwards, calling each compensation, making sure a crash in the middle of undoing does not lose your place.
In Cube Root the undo belongs to the try.
The flow
String outcome = "rejected in review";
if (decision.equals("approved")) {
try {
String posting = debit.invoke(t);
String sent = send.invoke(posting);
outcome = "sent " + sent;
} catch (Exception e) {
outcome = "refused by the rail - the debit is reversed";
}
}
That is the whole saga. If send fails, the steps the try finished are undone, last first, before the catch runs. Here
that is one step: the debit. When the catch runs, the debit has already been reversed. There is no list, no loop, no
compensation call in the flow.
Where the undo is written
The undo is part of the service, written once, next to what it undoes:
public class Debit implements Flow<Transfer, String>, Compensable<Transfer, String> {
public String invoke(Transfer t) {
// debit the account, answer the posting
}
public void compensate(Transfer t, String posting) {
// reverse that posting
}
}
compensate receives what the step was sent and what it answered, so it knows exactly what to reverse. Every flow that
calls the debit inside a try gets the same undo, without repeating it.
What happens, step by step
| Moment | What runs |
|---|---|
debit.invoke(t) |
the debit; a row records it and its answer, the posting |
send.invoke(posting) fails |
nothing more inside the try |
| before the catch | the debit's undo, given the transfer and the posting |
| the catch | outcome = "refused by the rail ..." |
A try with three finished steps undoes all three, newest first. The undo is durable like any other step: if the machine dies in the middle of undoing, the work continues from the rows and finishes the undo.
When finished steps should stand: keep:
Not every try wants this. Sometimes a later failure should leave earlier work in place: a notification already sent, a
record already archived. Write keep: on the try:
keep: try {
String note = notify.invoke(t);
String archived = archive.invoke(t);
} catch (Exception e) {
// notify and archive stand; handle the failure here
}
Undo is the default because, where money moves, an accidental partial success is the expensive mistake. Keeping is one word away when you mean it.
Why it is the default
A saga written by hand is correct only as long as every developer remembers to register every compensation, in every flow, in the right order. The day someone adds a step and forgets, the system is wrong in a way that only shows up when a later step fails, which is rare, which is exactly when nobody is watching.
Making the undo the try's own turns that from a discipline into a property of the code. The compiler knows which steps sit inside which try, so it knows what to undo and in what order.
Try it
The transfer is a sample in the browser IDE at cube-root.com. Open it, then look at its execution graph: the try and its undo are part of the graph the compiler derived.
Next in this series: two labels instead of two workflow classes, a payment file in parallel.
Next articleTwo labels instead of two workflow classes