Making Pipeline Steps Idempotent
This lesson explains how to design pipeline steps that give the same result however many times they run, since orchestrators retry failed tasks and a task can fail after writing its output.
Learning outcomes
After completing this lesson, you will be able to:
- Apply idempotent write patterns such as partition overwrite and MERGE.
- Recognise patterns that are not idempotent, such as appends.
- Use Spark dynamic partition overwrite.
Idempotent write patterns
A pipeline step is idempotent if running it twice gives the same result as running it once, which makes retries and reruns safe. The simplest pattern is to overwrite the partition determined by the run's logical date instead of appending to the table, so a rerun replaces its earlier output.
When records must be updated individually, an upsert, MERGE on a business key, into Apache Iceberg, Hudi or Delta Lake tables, or into Amazon Redshift, updates existing rows instead of adding duplicates. Deterministic file names, derived from the input rather than from the time or a random value, mean that a rerun overwrites rather than adds files. Finally, recording which inputs have been processed, with Glue job bookmarks or a control table, lets a rerun skip them.
The following diagram compares the table after a retry in each write mode.

Idempotent or not?
Patterns that are not idempotent
A step is idempotent if running it twice gives the same result as running it once, which matters because retries and reruns are routine. Several common patterns fail this test. INSERT INTO appending a day's data duplicates the day on a rerun. Deleting the day's rows and inserting them again in one transaction, or using MERGE, makes it safe. Writing files with random or timestamped names leaves both the old and new copies; deterministic names, or overwriting the whole partition, replace them instead.
Incrementing a counter for each event counts retried events twice, so either record which event IDs have been processed or recompute totals from the data. Sending a notification on every attempt sends duplicates, so record that it was sent and check before sending. The following table summarises these patterns and their alternatives.
| Not idempotent | Idempotent alternative |
|---|---|
INSERT INTO appending a day's data | Delete the day's rows, then insert, in one transaction; or MERGE |
| Writing files with random or timestamped names | Deterministic names, or overwriting the partition |
| Incrementing counters per event | Recording processed event IDs, or recomputing totals |
| Sending a notification on every attempt | Recording that the notification was sent |
Spark partition overwrite
In Spark, setting spark.sql.sources.partitionOverwriteMode to dynamic overwrites only the partitions present in the output, rather than the whole table. A daily job that writes its date's partition in overwrite mode can then be rerun safely without affecting other days.
Overwriting partitions in Spark
A daily Glue job appends yesterday's orders to a Parquet table. When the job is retried after a timeout, totals double.
The retry appended the same orders a second time. Make the write idempotent: overwrite the date partition being processed with dynamic partition overwrite, or MERGE into an Iceberg table on order ID. Either way, a retry replaces the earlier output instead of adding to it.
Why is appending with INSERT INTO not idempotent?
Show answer
Running the step again adds the same rows a second time. Partition overwrite or MERGE on a key gives the same result however often it runs.
Exam Tips
Remember for the exam
- Design every write to be idempotent: partition overwrite, MERGE on a key, deterministic file names.
- Appending with INSERT INTO is not idempotent.
- Dynamic partition overwrite replaces only the partitions a run writes.
- Record processed inputs so reruns skip them.
- Retries are normal; idempotence makes them safe.