It's three in the morning, a load failed halfway through and needs to be rerun. The scary question is: if I run it again, will the data it already loaded be duplicated? If the answer is “it depends”, the pipeline is not idempotent.

What it means

A process is idempotent when running it once or several times with the same input leaves the same result. Rerunning it is safe. That property, which sounds academic, simplifies everything else: automatic retries, backfills of past days and incident recovery.

Three patterns to achieve it

1. Replace instead of append

If a process loads one day of data, have it first delete or overwrite that day and then write it in full. Working by partitions (by date, for example) makes this cheap: only the affected partition is replaced. Running it twice leaves exactly the same thing.

2. Merge on a key

When you can't replace a whole block, a merge operation (insert if missing, update if present) on a unique business key avoids duplicates. The key has to be genuinely unique and stable; if it isn't, that is the first problem to solve.

3. Explicit parameters, not “yesterday”

A process that works out “yesterday” from the system date gives different results depending on when it runs. If it receives the date to process as a parameter, rerunning the 1 December load on 3 December does exactly what it did on the 2nd.

The usual traps

  • External side effects. Sending an email, calling a paid API or publishing a message are not undone by reprocessing. Record what was sent and check it before repeating.
  • Auto-generated IDs. If each run generates new IDs, two runs produce “different” records with the same content.
  • Changing source data. If the source no longer has last month's data, reprocessing that month is impossible. That is why it helps to keep the original data in a landing layer (the bronze layer of the medallion architecture).

A simple test

Run the process twice in a row in a test environment and compare the result. If anything changes, you know where to look before you find out at 3 a.m.