Idempotent Jobs: Doing the Same Work Twice Without Doing It Twice
Why the second run is inevitable
Every automated job will, eventually, run twice over the same work. A scheduler fires again after a crash it cannot distinguish from a hang. A queue delivers a message a second time because the acknowledgement was lost, not the work. An operator, unsure whether the first attempt finished, presses the button again — reasonably, because the alternative is guessing. Systems that treat the second run as an anomaly to be prevented spend their effort on locks and prayers. Systems that treat it as a certainty design for it, and the name of that design is idempotency: performing the operation again produces the same state as performing it once.
The stakes are concrete. A non-idempotent billing job charges twice. A non-idempotent notification job apologizes to the same customer five times. A non-idempotent provisioning job creates a second account, a second server, a second subscription, and a reconciliation project. None of these failures requires exotic conditions — only a retry, which is to say, only an ordinary Tuesday in any real system.
The three places idempotency lives
The first place is the key. Every unit of work carries a stable identifier derived from the work itself — the order number, the message identity, the content hash — and the system records which keys it has completed. Before performing an operation, the job asks whether this key has already been done, and treats a yes as success rather than as an error. This sounds trivial and is routinely defeated by keys that change between runs: timestamps folded into identifiers, random values generated per attempt, filenames that differ by directory. The key must be a function of the work, never of the attempt.
The second place is the write. Operations shaped as declarations — set this record to this state — are naturally safe to repeat, while operations shaped as increments — add one, append this, charge the card — are not. Wherever the design allows, prefer the declarative form: write the desired end state and let repetition converge instead of accumulate. Where increments are unavoidable, bind them to the key, so the increment itself is recorded once and recognized on the second pass.
The third place is the boundary
The hardest idempotency problems live at boundaries with systems you do not control: payment processors, email services, third-party APIs. You cannot make their operations declarative, and their retries compound yours. The working pattern is to ask the boundary for help — most serious services accept a client-supplied idempotency key precisely for this purpose — and, where they do not, to narrow the window: record your intent before the call, record the outcome after it, and make the recovery path reconcile the two records rather than blindly repeat the call. The dangerous moment is the crash between the call and the recording. Designing for it means accepting that the state may be unknown, and building the check that resolves unknown into known before any retry is allowed.
That check — a lookup before a retry — is the boundary form of a general rule: a retry should never be a reflex. It should be a decision the system makes with evidence about what the previous attempt actually did. Systems that retry blindly are not resilient; they are merely persistent, and persistence applied to an unknown state is how small incidents become duplicate charges and doubled emails.
Testing the property, not the hope
Idempotency that is not tested is a hope with good intentions. The test is simple to state and uncomfortable to run: execute the job, interrupt it at the worst moment you can arrange, run it again, and compare the world to the world the single clean run would have made. Do this for the crash before the work, during the work, and after the work but before the bookkeeping. Each interruption point is a different failure with a different cure, and a job that survives all three has earned the property rather than merely claimed it.
The payoff compounds quietly. Jobs that can be re-run safely can be operated by humans without fear, retried by schedulers without ceremony, and recovered from incidents by the simple instruction to run them again. In an automated estate, that is the difference between a toolbox and a minefield — and it is built one stable key, one declarative write, and one honest test at a time.
More field notes: Designing Automations That Fail Quietly and Report Clearly · Business Automation: A Complete Guide for 2026.