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 clauseVersion AVersion BCarries the property?
DELETE target(absent)DELETE FROM daily_ordersYes: 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 targetINSERT INTO daily_ordersINSERT INTO daily_ordersNo: the same in both
INSERT column listSELECT the 3 order columnsSELECT the 3 order columnsNo: the same in both
INSERT sourceFROM ordersFROM ordersNo: the same in both
INSERT filterWHERE 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.

SituationVersion A leavesVersion B leaves
Clean run, no failures4 rows, $1504 rows, $150
Crash after 2 rows, then a retry6 rows, $2254 rows, $150
Timeout after all 4 rows, then a retry8 rows, $3004 rows, $150
An engineer reruns the day a week later8 rows, $3004 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 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?

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'

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.