Every pipeline fails eventually, and the first thing anyone does next is run it again. Whether that is safe depends on one property: idempotence.
01 · The idea
Running it twice leaves the same result as running it once.
An operation is idempotent when repeating it has no further effect after the first time. Setting a value to 5 is idempotent. Adding 5 to a value is not: run it twice and the answer is different.
Data jobs are full of the second kind. A load that appends rows, a counter that increments, an email that is sent: each one quietly doubles its effect when a failed run is repeated.
If a re-run is frightening, the job is not recoverable, however well it works on a good day.
02 · Why it matters
Failures and retries are normal, not exceptional.
Networks drop, sources arrive late, and schedulers run a job again after a timeout. A job that was halfway through when it stopped has already changed something, and nobody can be sure how much.
An idempotent job turns that uncertainty into a non-event. Run it again from the start, and the end state is the same whether the first attempt did nothing, part of the work, or all of it.
03 · Three habits
Replace, key and stage.
Each habit is small. Together they mean the recovery plan for almost any failure is the same sentence: run it again.
- Replace a slice, do not appendWrite one partition or one day by deleting what is in it and inserting the new rows, in one transaction. A re-run replaces the same slice with the same data.
- Merge on a keyLoad with an upsert on a stable business key, so a row that already exists is updated in place instead of duplicated.
- Stage, then swapBuild the result in a staging table and switch it in at the end, so a half-finished run never shows up for readers.
04 · The edges
Some effects cannot be taken back.
Sending a message, charging a card or calling an outside system cannot be undone by running the job again. For those, record that the effect happened, with a unique request identifier, and check the record before acting. The receiving side can do the same with an idempotency key.
Dates deserve care too. A job that reads "now" gives a different answer on every run. Pass the period it should process as a parameter instead, so re-running yesterday really does re-run yesterday.
05 · Worked example
A daily sales load, made safe to repeat.
A job loads one day of sales into a reporting table. Written as a plain insert, a retry after a timeout adds the same day again, and every total for that day doubles.
Written to replace the slice, it deletes the rows for that load date and inserts the new ones inside one transaction. Run it once, twice or five times and the table ends in the same state. The only change to the code is the delete, and the recovery plan becomes “run it again”.
06 · Pitfalls
Ways a job quietly stops being repeatable.
- Using “now”A job that reads the current time gives a different result on each run. Pass the period in as a parameter.
- No unique keyAn upsert needs a real unique constraint to match on. Without one it just inserts.
- Side effects in the middleSending an email or calling an outside service inside a transformation repeats on every retry. Separate them and guard with a request identifier.
- Half-written outputReaders can see a partial result unless you stage the data and swap it in at the end.
- Never testing a re-runRun each job twice on test data and compare. It takes a minute and finds the problem before an outage does.
What I took away
- Design every job so that a second run is harmless.
- Replace a slice, merge on a key, and stage before swapping.
- Give jobs the period to process as a parameter, never "now".