Side by Side: Idempotent vs Not
Concepts covered: Idempotency
Version A: the original load
Here is the bookshop's load as it ran on the night of the dropped connection. The date in the filter stands in for the day the run is responsible for. The orchestrator hands that day to the task, and every retry of the same run receives the same day.
INSERT INTO daily_orders
SELECT
order_id,
order_date,
amount
FROM orders
WHERE order_date = DATE '2026-03-14'Version B: the fix
The fix keeps Version A's statement as it is and places one new statement in front of it.
DELETE FROM daily_orders
WHERE order_date = DATE '2026-03-14'| Statement and clause | Version A | Version B | Carries the property? |
|---|---|---|---|
| DELETE target | (absent) | DELETE FROM daily_orders | Yes: removes what earlier attempts left |
| DELETE filter | (absent) | WHERE order_date = DATE '2026-03-14' | Yes: limits the clear to the run's day |
| INSERT target | INSERT INTO daily_orders | INSERT INTO daily_orders | No: the same in both |
| INSERT column list | SELECT the 3 order columns | SELECT the 3 order columns | No: the same in both |
| INSERT source | FROM orders | FROM orders | No: the same in both |
| INSERT filter | WHERE order_date = DATE '2026-03-14' | WHERE order_date = DATE '2026-03-14' | Partly: it must match the DELETE filter |
Same nights, both versions
Put both versions through the situations a nightly task meets in its first year.
| Situation | Version A leaves | Version B leaves |
|---|---|---|
| Clean run, no failures | 4 rows, $150 | 4 rows, $150 |
| Crash after 2 rows, then a retry | 6 rows, $225 | 4 rows, $150 |
| Timeout after all 4 rows, then a retry | 8 rows, $300 | 4 rows, $150 |
| An engineer reruns the day a week later | 8 rows, $300 | 4 rows, $150 |
On a clean night the 2 versions cannot be told apart, which is why Version A passes any check that runs it once. The difference appears only when something runs the task a second time. Version B's column reads the same from top to bottom, and that sameness is the 2-run test passing in every situation.
Which lines carry the property
3 things make Version B idempotent
- The clear. The DELETE removes whatever earlier attempts left in the day's partition, so the write always starts from empty.
- The matching scope. The DELETE filter and the INSERT filter name the same date, so the clear removes one day and the write puts back that same day.
- The fixed day. The date is handed over by the orchestrator, so attempt 1 and every retry clear and write the same partition.
The lines that carry none of it matter as much. The INSERT INTO line is identical in both versions. The column list, the source table, the 02:00 schedule and the retry count of 3 are all unchanged. The property came from one added statement and from the agreement between 2 filters.
A reviewer proposes 4 separate changes to Version A. Which one makes the task pass the 2-run test?
- Raise the retry count from 3 to 5, so the task gets more chances to finish its load.
- Move the schedule from 02:00 to 04:00, when the warehouse is quieter and fails less often.
- Add a statement ahead of the insert that deletes the rows for the day being loaded.
- Sort the selected orders by order_id, so every attempt writes its rows in the same order.
A teammate tries to make Version A idempotent by running this statement before the usual insert for 2026-03-14. The table already holds 5 rows for 2026-03-13. The task runs, fails after its insert completes, and is retried. What's the result?
DELETE FROM daily_orders
WHERE order_date = DATE '2026-03-13'- The 5 rows for 2026-03-13 are deleted, and 2026-03-14 ends with 8 rows after the retry.
- The task is idempotent now, because a delete placed before an insert makes any rerun start clean.
- 2026-03-13 keeps its 5 rows and 2026-03-14 ends with 4, since the delete finds nothing to remove.
- Both days end up empty, because the retry's delete clears what the first attempt inserted.
A review of a batch task can start with one question: what does this table hold after the task runs 2 times for the same day? When the answer matches the table after one run, the retries, the reruns and the bad nights are already handled.
About this lesson
This section is part of the Idempotent Reruns: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.