BeginnerPipeline Architecture · 25 min · 10 challenges

Idempotent Reruns: Beginner

Early Sunday morning at Airbnb, the 01:00 job that loads Saturday's bookings into the warehouse dies partway through with a connection reset. The on-call engineer shrugs at the error and clicks retry. Monday's revenue dashboard shows the best Saturday in company history. Nobody made a booking twice; the retry loaded most of them a second time. The write pattern this lesson teaches is the difference between those 2 Mondays.

The Retry That Doubled the Rows

Daily Life
Interviews

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

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 have already arrived. It appends every one of them. The task finishes green. The warehouse now holds 430,000 rows for a day that had 250,000 orders, and 180,000 of those rows are exact copies of rows above them.
EventRows in sourceRows written this attemptRows in warehouse for 2026-04-25
Run 1 starts250,00000
Run 1 crashes at chunk 19250,000180,000180,000
Retry starts 5 minutes later250,0000180,000
Retry completes250,000250,000430,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.

The query that exposes the damage compares the number of rows to the number of distinct order ids. A day loaded once shows the 2 numbers equal. A day loaded one and a bit times shows the gap.
SELECT
order_date,
COUNT(*) AS row_count,
COUNT(DISTINCT order_id) AS distinct_orders,
COUNT(*) - COUNT(DISTINCT order_id) AS duplicate_rows
FROM orders
WHERE order_date = DATE '2026-04-25'
GROUP BY order_date
order_daterow_countdistinct_ordersduplicate_rows
2026-04-25430,000250,000180,000

Naming the failure mode

Three ingredients of the doubled day
  • ▸A partial failure: the run crashed after some of its writes were committed and before the rest.
  • ▸An append write: INSERT INTO adds 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 first ingredient can't be removed; networks drop and processes die. The third is something you want, because a retry is how a failed night becomes a finished night without anyone rewriting data by hand. That leaves the write. The rest of this lesson changes the write so that the other 2 ingredients become harmless.
TIP
The row count against distinct order ids is a 1-line check you can run on any table that has a unique id. Run it on the last 7 days of every daily table you own and you will learn which loaders append.
Multiple Choice

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?

Idempotency in One Sentence

Daily Life
Interviews
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

Start from a known state and run the operation once. Record what the system looks like.
Start from a known state and run the operation once. Record what the system looks like.
Run the exact same operation again with the same input. Record the state again.
Run the exact same operation again with the same input. Record the state again.
Compare the 2 records. Identical means idempotent. Any difference means it is not.
Compare the 2 records. Identical means idempotent. Any difference means it is not.
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. Point it at a whole pipeline or at a single SQL statement inside one task, and it gives the same kind of answer.

Everyday operations under the test

OperationAfter run 1After run 2Idempotent?
Set the thermostat to 20 degrees2020Yes
Raise the thermostat by 2 degrees2224No
Create the folder `reports` if it is missingfolder existsfolder existsYes
`INSERT INTO orders` the day's 250,000 rows250,000 rows500,000 rowsNo
Delete the day's rows, then insert the day's 250,000 rows250,000 rows250,000 rowsYes
`UPDATE accounts SET balance = balance + 10`110120No
`UPDATE accounts SET balance = 110`110110Yes
A pattern runs through the table. Operations that state the end result (set to 20, make the folder exist, the day's rows are exactly these) pass. Operations that describe a change relative to what is already there (raise by 2, add these rows, add 10) fail, because the second run starts from a different place than the first did.
TIP
When you cannot tell whether a step is idempotent, ask what it would do if you pressed the button twice by accident. If the answer contains the word 'more', it fails the test.

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.

def record_total_append(totals, day, amount):
totals.append((day, amount))
def record_total_set(totals, day, amount):
totals[day] = amount
log = []
record_total_append(log, '2026-04-25', 1_250_000)
record_total_append(log, '2026-04-25', 1_250_000)
print(len(log))
book = {}
record_total_set(book, '2026-04-25', 1_250_000)
record_total_set(book, '2026-04-25', 1_250_000)
print(len(book))
Both functions receive the same input twice. The first grows the list by one entry per call, so the second run is visible in the result. The second writes to a key named by the input, so the second call lands on the same key and the dictionary looks exactly as it did after the first call. The key makes the difference: the write is addressed by the day, and the day is part of the input.
Multiple Choice

Apply the two-run test. Which statement leaves the table in the same state whether it runs once or twice?

Replace, Do Not Append

Daily Life
Interviews

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.

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.
DELETE FROM orders
WHERE order_date = DATE '2026-04-25'
INSERT INTO orders
SELECT
order_id,
customer_id,
amount,
order_date
FROM source_orders
WHERE order_date = DATE '2026-04-25'

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

ScenarioPartition beforeDELETE removesINSERT addsPartition after
Clean first run00250,000250,000
Rerun after a clean run250,000250,000250,000250,000
Retry after a crash at chunk 19180,000180,000250,000250,000
Third rerun, for no reason250,000250,000250,000250,000
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 produces the correct end state whatever the previous state was, and no step in it detects or repairs the failure.

The partition must match the run

The DELETE and the INSERT must describe the same set of rows. If the run reads one day from the source, it clears one day in the target. A loader that deleted the whole table and inserted one day would wipe 2 years of history on every run. A loader that deleted one day and inserted a week would leave 6 days doubled. Match the scope of the delete to the scope of the insert, and make both come from the same value.

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.

def load_orders(conn, run_date):
conn.execute('DELETE FROM orders WHERE order_date = ?', [run_date])
conn.execute(
'INSERT INTO orders '
'SELECT order_id, customer_id, amount, order_date '
'FROM source_orders WHERE order_date = ?',
[run_date],
)
✓Append with INSERT INTO
  • 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
✗Replace the partition
  • 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
✓Do
  • 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
✗Don't
  • 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.

Multiple Choice

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?

Why Retries Need Idempotency

Daily Life
Interviews

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

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.
AttemptRows committed in this attemptPartition after (append)Partition after (replace)
1180,000180,000180,000
260,000240,00060,000
3240,000480,000240,000
4250,000730,000250,000
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 clearing whatever the previous one left, so only the last attempt's rows remain, and the last attempt is the one that completed. The replace column passes through 3 wrong states on its way, but the state the retries settle on is right.
480,000
phantom rows after 3 retries
2.9x
warehouse rows per real order

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.

Orders database
source orders
Orchestrator
load orders
RETRY3BACKOFF5 minutesIDEMPOTENCYreplace partition by run date
Warehouse
orders

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

The orchestrator's retry is the most visible second run, and far from the only one. A scheduler that fires twice after a restart, an engineer who clicks run on the wrong day, a deploy that restarts a task mid-flight, a backlog of failed nights all rerun on one afternoon: each is a second run of the same operation on the same input. With an idempotent write, every one of them leaves the partition as a single run would; with an append, each one doubles a day.
What idempotency buys the on-call engineer
  • ▸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.
Multiple Choice

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?

Side by Side: Idempotent vs Not

Daily Life
Interviews

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.

from datetime import date, timedelta
def load_orders(source, target):
yesterday = date.today() - timedelta(days=1)
rows = source.query(
'SELECT order_id, customer_id, amount, order_date '
'FROM orders WHERE order_date = ?',
[yesterday],
)
for chunk in chunks(rows, 10_000):
target.execute('INSERT INTO orders VALUES (?, ?, ?, ?)', chunk)
target.commit()
log.info('loaded %d rows', len(rows))
def load_orders(source, target, run_date):
rows = source.query(
'SELECT order_id, customer_id, amount, order_date '
'FROM orders WHERE order_date = ?',
[run_date],
)
target.execute('DELETE FROM orders WHERE order_date = ?', [run_date])
target.commit()
for chunk in chunks(rows, 10_000):
target.execute('INSERT INTO orders VALUES (?, ?, ?, ?)', chunk)
target.commit()
log.info('loaded %d rows for %s', len(rows), run_date)
LineBeforeAfterCarries 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 preamblenone`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 chunkNo: unchanged, an append is fine once the partition is cleared
Commit per chunkafter each chunkafter each chunkNo: decides how much a crash leaves behind, and the DELETE handles any amount
Log linerow countrow count and run dateNo: 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 sequenceBefore (append, clock)After (replace, run_date)
Run once on 2026-04-26 for the 25th250,000 on the 25th250,000 on the 25th
Crash at chunk 19, retry the same night430,000 on the 25th250,000 on the 25th
Rerun on 2026-04-28 for the 25th25th unchanged, 27th doubled250,000 on the 25th, no other day touched
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.
TIP
When reviewing a loader, find the 3 places: where the run's date is fixed, where the read is bounded by it, and where the write clears by it. If any of the 3 is missing, the code is one retry away from a doubled day.
Multiple Choice

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?

❯❯❯PUTTING IT ALL TOGETHER

> 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.

With an append loader, the 140,000 partial rows plus every later attempt's partial rows plus the full 310,000 land in Tuesday's partition, and riders are paid for deliveries that never happened.
With a replace loader scoped by the orchestrator's run date, each retry begins by clearing Tuesday's partition, so the completed attempt is the only one whose rows survive and the payout is exact.
The two-run test is how the team proves it before Friday: run the loader twice for Tuesday in a test schema and compare the row count against distinct delivery ids.
Because every statement in the loader is scoped by run_date rather than by the clock, a rerun on Thursday for Tuesday repairs Tuesday and leaves Wednesday untouched.
KEY TAKEAWAYS
A partial failure plus an append plus a retry produces duplicate rows, and the retry succeeds, so nothing alerts.
An operation is idempotent when 1 run and 2 runs leave the same end state; the two-run test checks it mechanically.
Replace the partition instead of appending to it: DELETE the run's day, then INSERT the run's day from the source.
The run date comes from the orchestrator, and every statement in the load is scoped by that one value.
Retries multiply whatever the write does, so automatic retries are safe only on an idempotent write.

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

  1. 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

  2. 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

  3. 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

  4. 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

  5. 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.