Tech Shot · Data architecture

Idempotent pipelines: the one property that makes retries safe

Every pipeline step will run twice one day. Idempotent writes make that harmless. Three patterns that do it, with code.

· 2 min read · Intermediate

Every pipeline step will run more than once at some point: a retry after a timeout, a backfill after a bug fix, someone pressing “rerun” on a failed job. If running a step twice gives a different result from running it once, you get duplicated rows, double-counted revenue and a long afternoon of cleanup.

A step is idempotent when running it again with the same input leaves the target in the same state. That single property is what makes retries, backfills and manual reruns safe.

Pattern 1: overwrite the slice you own

If a daily job produces one day of data, let it replace exactly that day, never append to it.

(daily_df.write
  .format("delta")
  .mode("overwrite")
  .option("replaceWhere", "order_date = '2026-12-05'")
  .saveAsTable("silver.orders"))

Run it once or five times: the table holds one copy of 5 December.

Pattern 2: MERGE on a business key

When changes arrive for rows that already exist, upsert on a key that identifies the record.

MERGE INTO silver.customers AS t
USING staged_changes AS s
ON t.customer_id = s.customer_id
WHEN MATCHED AND s.updated_at > t.updated_at THEN UPDATE SET *
WHEN NOT MATCHED THEN INSERT *

The updated_at check also stops an old change replayed later from overwriting a newer one.

Pattern 3: commit progress after the write

For streams and queues, record progress (a Kafka offset, a file bookmark, a high-water mark) only after the write succeeds. A crash between the two then causes a replay, and patterns 1 and 2 make the replay harmless.

What does not work

  • A downstream dedup job. It hides duplicates until the day it is skipped or its key is wrong.
  • “It only runs once.” Schedulers retry, people rerun, and backfills are a normal part of the job.
  • Append plus DISTINCT. Rows that differ only by a load timestamp are not removed.

Check it

Run the step twice in a test environment and compare row counts and totals. If they change, the step is not idempotent yet.

Go deeper

Published as the insight of the Daily Tech Shot for 5 Dec 2026, with a question to test yourself.