In this lesson · topic 2 of 7
Topic 2
Steps, undos and the reverse order
4 min read
The pieces
A saga is a list of steps. Each step is a local transaction in one service. Each step has a compensating action, a second local transaction that cancels the first. A saga for an order might have these pairs: reserve and release, charge and refund, book and cancel.
Running it
The steps run in order. Step one commits, then step two, and so on. If every one succeeds, the saga is done.
If step three fails, steps one and two stay committed. The saga now runs the undo of step two, then the undo of step one. It runs them in reverse order, because a later step may rely on an earlier one. Step three failed, so it has nothing to undo.
In the lab at its defaults, 210 sagasmeasured out of 400 ticks of arrivals ended compensated. In the steps that did run, 11%measured were later undone. That is work done and thrown away.
Compensation is a new change
A compensation cannot restore the old state, because others have seen the new one. A refund does not remove the charge from the statement. A cancellation does not unsend an email. Put steps that cannot be undone at the end, after every step that might fail, and put the cheap, reversible ones first.