Try
↑ ↓ move↵ openesc close

Interview prep / Sagas

Mid · Sagas · System design interview question

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

Learn it by running it

The sagas and compensation lesson explains the mechanism behind this question, and its lab lets you change the parameters and watch the effect.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms