Idempotent Reruns: Beginner
A nightly load that appends
The Airbnb question is easier to answer on a load small enough to follow row by row, so this section shrinks it to a bookshop whose day holds 4 orders. An online bookshop keeps every order in an orders table inside its operational database. Each night at 02:00, a task called load_daily_orders copies the previous day's orders into a warehouse table named daily_orders. A revenue dashboard reads that warehouse table every morning, and the finance team reads the dashboard.
Each night the task copies one day of orders from the operational database into the warehouse, where the dashboard reads them.
INSERT INTO is an append. It adds the rows it is handed to whatever the table already holds, and it never looks at the table first. daily_orders has no enforced unique key on order_id, as is typical of warehouse tables, so nothing stops a second copy of a row from landing. On a night with no failures that gap costs nothing: 4 orders in the source become 4 rows in the warehouse.
Attempt 1 dies halfway
The task sends its rows to the warehouse in batches. On this night it has written the first batch, orders 1001 and 1002, when the connection to the warehouse drops at 02:03. The task exits with an error. The 2 rows it already wrote stay in daily_orders, because the crash stopped the work where it stood and cleaned up nothing.
| order_id | amount |
|---|---|
| 1001 | 30 |
| 1002 | 45 |
| 1003 | 20 |
| 1004 | 55 |
| order_id | amount |
|---|---|
| 1001 | 30 |
| 1002 | 45 |
A warehouse will often undo a single statement that fails midway. A load is rarely a single statement: batches, files and multi-step tasks all leave partial work behind when they crash.
The retry finishes the job, and the damage
| order_id | order_date | amount | written by |
|---|---|---|---|
| 1001 | 2026-03-14 | 30 | attempt 1 |
| 1002 | 2026-03-14 | 45 | attempt 1 |
| 1001 | 2026-03-14 | 30 | retry |
| 1002 | 2026-03-14 | 45 | retry |
| 1003 | 2026-03-14 | 20 | retry |
| 1004 | 2026-03-14 | 55 | retry |
The last column is added for this page. The real table keeps no record of which attempt wrote a row.
daily_orders now holds 6 rows for a day with 4 orders. Orders 1001 and 1002 each appear 2 times. The dashboard sums the amount column and reports $225 for a day that earned $150, which is 50% too high. No alarm fires, because the final attempt succeeded and the task is green.
Recognizing the pattern
- ▸One day's total jumps with no matching change in the source system.
- ▸The warehouse holds more rows for a day than the source holds for that day.
- ▸The same
order_idappears more than once in a table meant to hold each order once. - ▸The task's history for that night shows a failed attempt followed by a successful one.
On another night the source has 10 orders for the day. Attempt 1 appends 7 of them and crashes. The retry runs the whole task and succeeds. How many rows does the warehouse hold for that day?
The sentence
An operation is idempotent when running it 2 times, with the same input, leaves the same end state as running it once.
The sentence has 3 parts. Same input keeps the comparison fair: the second run is handed the same day and the same source rows as the first. End state means the test looks at what is left behind, such as the rows in a table, and ignores how much work each run did. 2 times covers every larger count too: if run 2 changes nothing that run 1 left, then run 3 starts from that same state and changes nothing either.
Set versus add
- Set the thermostat to 20 degrees.
- Press the call button for an elevator that is already on its way.
- Set the stock of book 77 to 12.
- Replace the rows for 2026-03-14 with these 4 rows.
- Turn the thermostat up by 2 degrees.
- Add one more copy of a book to the cart.
- Add 12 to the stock of book 77.
- Append these 4 rows to the table.
The 2-run test
- Step 1: fix the input. Pick one day and one set of source rows, and hold them still for both runs.
- Step 2: run the code once and write down the end state, as a row count and a total such as summed revenue.
- Step 3: run the same code again on the same input, changing nothing in between.
- Step 4: compare the 2 end states. Identical means idempotent. Any difference means the code is unsafe to rerun.
| Operation | State after run 1 | State after run 2 | Idempotent? |
|---|---|---|---|
| Set stock of book 77 to 12 | stock = 12 | stock = 12 | Yes |
| Add 12 to stock of book 77 | stock = 12 | stock = 24 | No |
| Append the 4 orders for 2026-03-14 | 4 rows, $150 | 8 rows, $300 | No |
| Replace the rows for 2026-03-14 with its 4 orders | 4 rows, $150 | 4 rows, $150 | Yes |
| Delete every row dated 2026-03-14 | 0 rows for that day | 0 rows for that day | Yes |
Stock for book 77 starts at 12. Apply the 2-run test to this statement. What does the test show?
Hold the input still
One day, one partition
A daily batch pipeline has a natural unit of work: one run produces one day of rows. The write pattern in this section gives that unit a matching place in the table. A partition is the slice of a table that holds all the rows sharing one value of a chosen column. When daily_orders is partitioned by order_date, each day's rows sit together as one slice, and the warehouse can replace one slice without touching the others.
| Partition (order_date) | Rows | Revenue |
|---|---|---|
| 2026-03-12 | 3 | $95 |
| 2026-03-13 | 5 | $180 |
| 2026-03-14 | 4 | $150 |
Clear the day, then write the day
Partition overwrite is a write that replaces the entire contents of one partition with the rows the current run produced. Whatever sat in that partition before is gone afterwards, whether that was nothing, half of a failed load, a complete load, or a load computed with a bug. Its effect equals 2 statements run in order.
Many warehouses and lake tables offer overwrite as a single write option that performs both steps together. The table ends in the same state either way.
Replaying the failed night
| Step | What happens | Rows for 2026-03-14 | Revenue shown |
|---|---|---|---|
| Attempt 1 clears | The partition is already empty | 0 | $0 |
| Attempt 1 writes | 2 orders land, then the connection drops | 2 | $75 |
| Retry clears | The 2 leftover rows are removed | 0 | $0 |
| Retry writes | All 4 orders land | 4 | $150 |
| A third run, for any reason | Clears 4 rows, writes the same 4 | 4 | $150 |
Why this is the default for daily batch
- No memory needed: a run does not have to know whether earlier attempts wrote nothing, some rows, or all of them.
- Matching units: one run owns one day and one day is one partition, so a run replaces only its own output.
- Other days stay safe: the clear is scoped to a single date, so rerunning one day cannot disturb another.
- Reruns repair mistakes: after a bug in the task is fixed, rerunning 2026-03-14 swaps that day's wrong rows for right ones.
- Partition a daily table by the date each run is responsible for.
- Replace the whole partition on every run, including the first.
- Scope the clear and the write to the same single date.
- Append to a table that a retry or a rerun can reach.
- Skip the load when rows already exist: a half-loaded day passes that check.
- Clear more than the run is about to write back.
daily_orders holds 3 partitions: 2026-03-12 with 3 rows, 2026-03-13 with 5 rows, and 2026-03-14 with 4 rows. A pricing bug is fixed and the task for 2026-03-14 is rerun with partition overwrite, producing 4 corrected rows. What does the table hold afterwards?
Retries run while nobody watches
An orchestrator retries failed tasks on its own. A common setting is 3 retries, 5 minutes apart, and the bookshop's load_daily_orders task uses it. Retries exist because many failures clear up without help: a dropped connection, a warehouse that was busy for a minute, a machine that restarted. Running the task again a few minutes later usually works, and nobody is woken at 02:00.
The orchestrator may run the load up to 4 times in one night: a first attempt plus 3 retries. Every attempt writes to the same warehouse table.
A red task says little about the table
| Where attempt 1 failed | Rows it left behind | Rows after an append retry | Correct? |
|---|---|---|---|
| Before the write | 0 | 4 | Yes, by luck |
| Midway through the write | 2 | 6 | No, 2 duplicates |
| After the write finished | 4 | 8 | No, every row doubled |
Tracing the accumulation
| Attempt | Reported | Rows | Revenue |
|---|---|---|---|
| 1 | failed | 4 | $150 |
| 2 | failed | 8 | $300 |
| 3 | failed | 12 | $450 |
| 4 | succeeded | 16 | $600 |
| Attempt | Reported | Rows | Revenue |
|---|---|---|---|
| 1 | failed | 4 | $150 |
| 2 | failed | 4 | $150 |
| 3 | failed | 4 | $150 |
| 4 | succeeded | 4 | $150 |
The choice without idempotency
- Every dropped connection becomes a failed night that waits for a person.
- Someone must inspect the table and remove leftover rows by hand before rerunning.
- The dashboard is late whenever anything fails.
- Most failures recover without anyone noticing.
- Some of those recoveries leave duplicate rows behind.
- The dashboard is on time and sometimes wrong, beside a green task.
An append task with 2 retries loads a day that has 4 orders. Attempt 1 writes 3 rows and fails. Attempt 2 writes 1 row and fails. Attempt 3 writes all 4 and succeeds. How many rows does the day hold, and how many are duplicates?
Version A: the original load
Version B: the fix
| 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
| 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 |
Which lines carry the property
- 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?
- ▸Does the write add to what's there, or replace what's there?
- ▸If it replaces, what gets cleared: one day, several days, or the whole table?
- ▸Does the range that is cleared equal the range that is written back?
- ▸Would a retry be handed the same day as the first attempt?
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?
> A ride-hailing company pays drivers every Monday from a daily_driver_earnings table that a nightly task fills. On Saturday night the task appends half of Friday's trips, loses its warehouse connection, and is retried by the orchestrator. The retry succeeds, the task is green, and Monday's payout run reads a Friday in which thousands of trips are counted 2 times.
Make a daily pipeline safe to run 2 times: the same rows after a retry as after a single clean run.
- 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)
A nightly load that appends In SQL terms, the load for the night of 2026-03-15 does this. It reads the orders placed on 2026-03-14 and adds them to the warehouse table. Attempt 1 dies halfway The source holds 4 orders worth $150. The warehouse holds 2 of them, worth $75. At this point the problem is visible: the task is red and the day is short by 2 orders. A warehouse will often undo a single statement that fails midway. A load is rarely a single statement: batches, files and multi-step tasks a
- Idempotency in One Sentence (concepts: Idempotency)
The sentence Set versus add Most operations fall on one side or the other by how they are phrased. An operation that names where the state should end up is idempotent. An operation that says how far to move from wherever the state is now will move it again on every run. The 2-run test The definition turns directly into a test. It works on any piece of pipeline code, on paper or against a scratch table, and it has 4 steps. Here is the test applied to 5 operations. Stock starts at 0, and the table
- Replace, Do Not Append (concepts: Idempotency)
One day, one partition After 3 clean nights the table holds 12 rows in 3 partitions, one per day. Clear the day, then write the day Many warehouses and lake tables offer overwrite as a single write option that performs both steps together. The table ends in the same state either way. Replaying the failed night Run the night of the dropped connection again, this time with overwrite, and follow the 2026-03-14 partition. The retry never needed to know how far attempt 1 got. It emptied the partition
- Why Retries Need Idempotency (concepts: Idempotency)
Retries run while nobody watches A retry is a blunt tool. It runs the task's code again from the top with the same inputs, and it carries no record of which rows the failed attempt wrote. Every retry is run 2 of the 2-run test, performed on the production table instead of on paper. A red task says little about the table The orchestrator learns one thing from a failed attempt: the task didn't report success. It doesn't learn how much the attempt wrote. A task can fail before its write, midway thr
- Side by Side: Idempotent vs Not (concepts: 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. Version B: the fix The fix keeps Version A's statement as it is and places one new statement in front of it. Same nights, both versions Put both versions through the situations a nightly task meets in its first year. On