Why must saga steps and compensations be idempotent?
Sketch your own answer first, then compare it with the one below.
A strong answer
Messages are delivered at least once, and an orchestrator that crashes does not know whether its last command ran. It sends it again. If the step is not safe to repeat, the customer is charged twice, or a refund is paid twice.
Make each step take an identifier, usually the saga id plus the step number, and remember it. A repeat then returns the first result. A compensation also has a case to handle: it can arrive for a step that never ran, if the request was lost. It should record that and do nothing.
Sources
- Chris Richardson (2018). Pattern: Saga.
- Pat Helland (2007). Life beyond Distributed Transactions: an Apostate's Opinion.