Idempotent Reruns: Beginner
Every night at 01:00 a loader copies the previous day's orders from the orders database into the warehouse table orders. For 2026-04-25 the source holds 250,000 rows. The loader reads them in chunks of 10,000, and after each chunk it runs an INSERT INTO and commits, so a crash never loses more than one chunk of progress. That sounds careful, and it's exactly the design that doubles the rows.
The night of 2026-04-25
| Event | Rows in source | Rows written this attempt | Rows in warehouse for 2026-04-25 |
|---|---|---|---|
| Run 1 starts | 250,000 | 0 | 0 |
| Run 1 crashes at chunk 19 | 250,000 | 180,000 | 180,000 |
| Retry starts 5 minutes later | 250,000 | 0 | 180,000 |
| Retry completes | 250,000 | 250,000 | 430,000 |
Why nobody notices until Monday
The retry succeeded, so no alert fires. Every one of the 430,000 rows is a real order with a real customer and a real amount; the problem is that 180,000 of them appear twice. The day's row count is 1.72 times the true figure. Revenue summed from amount for 2026-04-25 is inflated by about as much, since the doubled chunks are ordinary orders with ordinary amounts, and a sales lead spends Monday morning asking why Saturday was the best day of the year.
| order_date | row_count | distinct_orders | duplicate_rows |
|---|---|---|---|
| 2026-04-25 | 430,000 | 250,000 | 180,000 |
Naming the failure mode
- ▸A partial failure: the run crashed after some of its writes were committed and before the rest.
- ▸An append write:
INSERT INTOadds rows and never asks what's already there. - ▸A retry: a person or the orchestrator started the whole run again from the first row.
- ▸Remove any one of the 3 and the duplicates vanish.
The same loader runs the next night for 2026-04-26, which has 300,000 source rows. It crashes after committing 3 chunks, and the on-call engineer retries once. The retry completes. How many rows does the warehouse hold for 2026-04-26?
The two-run test
Everyday operations under the test
| Operation | After run 1 | After run 2 | Idempotent? |
|---|---|---|---|
| Set the thermostat to 20 degrees | 20 | 20 | Yes |
| Raise the thermostat by 2 degrees | 22 | 24 | No |
| Create the folder `reports` if it is missing | folder exists | folder exists | Yes |
| `INSERT INTO orders` the day's 250,000 rows | 250,000 rows | 500,000 rows | No |
| Delete the day's rows, then insert the day's 250,000 rows | 250,000 rows | 250,000 rows | Yes |
| `UPDATE accounts SET balance = balance + 10` | 110 | 120 | No |
| `UPDATE accounts SET balance = 110` | 110 | 110 | Yes |
Same operation, same input
The test says the exact same operation with the same input. A loader that reads WHERE order_date = CURRENT_DATE is a different operation on Thursday than it was on Tuesday, because its input moved under it. Idempotency is judged for a fixed input: the run for 2026-04-25, with the source rows for 2026-04-25. Two runs of that are the comparison. A run for a different day is a different operation, and it is supposed to produce different rows.
Apply the two-run test. Which statement leaves the table in the same state whether it runs once or twice?
The replace pattern needs one idea about how the warehouse table is organized. The orders table is split by order_date: every row for 2026-04-25 is stored together under that date, and every row for 2026-04-26 under its own. One date's slice is called a partition. A query that filters on a single date touches one partition, and a write that targets a single date can replace that partition without reading or touching any other day.
Some engines offer one statement that performs both steps at once, often written INSERT OVERWRITE. Written out, that statement is the pair above: clear the partition, fill the partition. The end state is the same either way. The partition holds exactly the day's rows from the source, and nothing from any previous run survives.
The two-run test on the replace
| Scenario | Partition before | DELETE removes | INSERT adds | Partition after |
|---|---|---|---|---|
| Clean first run | 0 | 0 | 250,000 | 250,000 |
| Rerun after a clean run | 250,000 | 250,000 | 250,000 | 250,000 |
| Retry after a crash at chunk 19 | 180,000 | 180,000 | 250,000 | 250,000 |
| Third rerun, for no reason | 250,000 | 250,000 | 250,000 | 250,000 |
The partition must match the run
Where the date comes from
That value is the run's date, and it must come from the orchestrator rather than from the clock. The orchestrator schedules a run for 2026-04-25 and hands that date to the task. If the task computed CURRENT_DATE - INTERVAL '1' DAY instead, a rerun on Thursday for Tuesday's failed run would delete and insert Wednesday, leaving Tuesday still broken and Wednesday rewritten for nothing. Every statement in the run for a given day is scoped to that day, however many days later the run happens to execute.
- A second run doubles the day
- A partial failure leaves rows the retry can't see
- Correct only if the run happens exactly once
- One statement to write, a long cleanup afterwards
- A second run equals the first
- A partial failure is erased by the next DELETE
- Correct for any number of runs
- Two statements, both scoped by the run date
- Scope the DELETE and the INSERT by the same run date
- Take the run date from the orchestrator, never from the clock
- Partition the target by the same unit the loader processes
- Append daily loads with a bare INSERT INTO
- Filter on CURRENT_DATE inside a scheduled task
- Delete a wider range than the run inserts
Between the DELETE and the INSERT the partition is empty for a few seconds, and a query during that window sees no rows for the day. A single overwrite statement closes that gap, which is one reason engines offer it. The end state is identical.
A loader deletes WHERE order_date = CURRENT_DATE - INTERVAL '1' DAY and then inserts the rows for the run_date the orchestrator passed. Tuesday's run failed partway. On Thursday the on-call engineer reruns it for Tuesday. What happens to the table?
An orchestrator retries a failed task on its own. You set retries: 3 on the loader so that a dropped connection at 01:00 does not need a human at 01:05. The orchestrator waits and runs the task again, and it repeats that after each failure for up to 3 extra attempts. That behaviour is the orchestrator doing its job; the trouble comes from what each attempt does to the table.
Tracing 3 retries on an append
| Attempt | Rows committed in this attempt | Partition after (append) | Partition after (replace) |
|---|---|---|---|
| 1 | 180,000 | 180,000 | 180,000 |
| 2 | 60,000 | 240,000 | 60,000 |
| 3 | 240,000 | 480,000 | 240,000 |
| 4 | 250,000 | 730,000 | 250,000 |
Retries multiply whatever the write does
The retry repeats the write, and the write creates the duplicates. A retry is a multiplier on the write's behaviour. An idempotent write run 4 times still leaves one day's rows. An append run 4 times leaves the full day from the attempt that succeeded plus the partial fragment from every attempt that failed before it, up to nearly 4 days' worth of rows. The retry count is the wrong dial to turn. Setting retries: 0 on an append loader doesn't protect the table: the on-call engineer retries by hand, the manual run appends the same way, and the day doubles exactly as before.
One day of orders moves left to right; the retry chips are safe to set only because the write replaces the day's partition
Anything that can run twice, will
- ▸Retry without reading the logs first, because the worst case of a retry is wasted compute.
- ▸Rerun a week of failed nights in any order, because each run owns its own partition.
- ▸Stop asking whether the first attempt wrote anything before pressing the button.
An append loader has retries: 2. One night attempt 1 commits 100,000 rows and fails, attempt 2 commits 50,000 and fails, and attempt 3 completes all 250,000. What does the partition hold, and what would it hold if the write replaced the partition instead?
Here is the loader as it ran on the night of 2026-04-25, followed by the version that replaced it. The 2 differ in 3 places. In both, chunks splits the rows into lists of 10,000 and log is the task's logger. Read both, then the table that lists each difference and what it contributes to idempotency.
| Line | Before | After | Carries the property? |
|---|---|---|---|
| Function signature | `load_orders(source, target)` | `load_orders(source, target, run_date)` | Yes: the caller fixes the input, so 2 runs can receive the same day |
| Date used for the read | `date.today() - 1 day` | `run_date` | Yes: the same run reads the same rows on Thursday as on Tuesday |
| Write preamble | none | `DELETE ... WHERE order_date = run_date` | Yes: clears every row a previous attempt wrote for this day |
| Insert loop | `INSERT INTO` per chunk | `INSERT INTO` per chunk | No: unchanged, an append is fine once the partition is cleared |
| Commit per chunk | after each chunk | after each chunk | No: decides how much a crash leaves behind, and the DELETE handles any amount |
| Log line | row count | row count and run date | No: logging has no effect on the table |
The property depends on 3 lines working together. The run_date parameter makes 2 runs comparable. The read filtered by run_date makes both runs fetch the same rows. The DELETE filtered by run_date makes the second write land on a cleared partition. Remove any one and the two-run test fails, at the latest when a run executes on a different day from the one it is for. Without the parameter the day drifts with the clock; without the read filter the loader clears one day but reads every day in the source, so every other day is reloaded on top of itself; and if the DELETE goes, the rows pile up.
Lines that look important and are not
The INSERT INTO is unchanged between the 2 versions. An append is a fine statement; an append without a preceding clear is what fails the test. The chunk size and the commit per chunk decide how much a crash leaves behind, 180,000 rows or 10,000 or none, and the next DELETE erases any amount, so they don't change the end state once a rerun succeeds. They do change what a reader of the table sees during the load. The DELETE commits first and every chunk commits as it lands, so for the whole load, far longer than the few seconds of the second section's pair of statements, a query sees an empty day and then a partial one. If every retry fails, the partial day stays. Running the DELETE and all the inserts in one transaction, or using a single overwrite statement, closes that window. The log line is the one most often mistaken for a safeguard. It records that something happened; it can't stop it from happening twice.
Running the test against both
| Run sequence | Before (append, clock) | After (replace, run_date) |
|---|---|---|
| Run once on 2026-04-26 for the 25th | 250,000 on the 25th | 250,000 on the 25th |
| Crash at chunk 19, retry the same night | 430,000 on the 25th | 250,000 on the 25th |
| Rerun on 2026-04-28 for the 25th | 25th unchanged, 27th doubled | 250,000 on the 25th, no other day touched |
A colleague edits the fixed loader so the DELETE reads DELETE FROM orders WHERE order_date = CURRENT_DATE - INTERVAL '1' DAY, and leaves the read on run_date. Which run sequence still passes the two-run test?
> A food delivery company loads each day's completed deliveries into a warehouse partition at 02:00, and the courier payout team sums that partition every Friday to pay riders. The loader fails on Tuesday night with a dropped connection after 140,000 of 310,000 rows. The orchestrator retries with retries: 3, the first 2 retries also fail partway, and the third retry completes all 310,000 rows. On Friday the payout query runs.
run_date rather than by the clock, a rerun on Thursday for Tuesday repairs Tuesday and leaves Wednesday untouched.A retry that runs a write twice must leave the table the same as a write that ran once.
- Category
- Pipeline Architecture
- Difficulty
- beginner
- Duration
- 25 minutes
- Challenges
- 10 hands-on challenges
Topics covered: The Retry That Doubled the Rows, Idempotency in One Sentence, Replace, Do Not Append, Why Retries Need Idempotency, Side by Side: Idempotent vs Not
Lesson Sections
- The Retry That Doubled the Rows (concepts: Idempotency)
The night of 2026-04-25 At chunk 19 the network connection to the warehouse resets. The loader raises an error and exits. Chunks 1 through 18 are already committed: 180,000 rows for 2026-04-25 sit in the warehouse table. The orchestrator marks the task failed and pages the on-call engineer, who sees a dropped connection that won't happen again and clicks retry 5 minutes later. The retry starts from the beginning. It reads all 250,000 source rows, because nothing tells it that 180,000 of them hav
- Idempotency in One Sentence (concepts: Idempotency)
An operation is idempotent when running it once and running it again leave the system in the same state. The number of runs does not change the result; 50 runs end in the same place as 1. The definition ignores what the second run costs in time and money, and it does not care whether that run does any work. It only compares end states. The two-run test The test is mechanical, which is its value. You don't need to understand what a piece of code intends; you run it twice and diff the results. Poi
- Replace, Do Not Append (concepts: Idempotency)
The daily loader's unit of work is one day. The partition's unit of storage is one day. When those 2 units match, the idempotent write is 2 statements: remove whatever the partition holds, then insert the day's rows computed fresh from the source. The two-run test on the replace Every row of the table ends at 250,000. The partial 180,000 from the crashed run are exactly what the DELETE removes, so the retry cleans up the mess the first attempt left without knowing the mess exists. The replace pr
- Why Retries Need Idempotency (concepts: Idempotency)
Tracing 3 retries on an append Take a bad night. Attempt 1 crashes after 180,000 rows, attempt 2 after 60,000 and attempt 3 after 240,000; attempt 4 completes all 250,000. The task's final status is success, and the orchestrator's run page shows 1 green box. Trace the partition through the 4 attempts under both write modes. Under append, every attempt's partial progress survives into the next, and the finished partition holds 730,000 rows for 250,000 orders. Under replace, each attempt begins by
- Side by Side: Idempotent vs Not (concepts: Idempotency)
Lines that look important and are not Running the test against both The before column fails on the second line and produces something stranger on the third: a rerun meant to fix the 25th instead loads the 27th a second time, because the clock chose the day. The after column is one number repeated, which is what the two-run test looks for.