BeginnerPipeline Architecture · 25 min · 10 challenges

Idempotent Reruns: Beginner

Take a nightly job at Airbnb that loads yesterday's bookings. At 02:00 it crashes halfway, and the orchestrator quietly retries it. By breakfast the revenue chart is up 40%. Did guests suddenly book more stays, or did the retry load the same bookings again? One property of that job's write decides which, and this lesson teaches it.

The Retry That Doubled the Rows

Daily Life
Interviews

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.

operational database
orders
nightly batch task
load daily orders
warehouse table
daily orders
read each morning
revenue dashboard

Each night the task copies one day of orders from the operational database into the warehouse, where the dashboard reads them.

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.
INSERT INTO daily_orders
SELECT
order_id,
order_date,
amount
FROM orders
WHERE order_date = DATE '2026-03-14'

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.

Source: orders placed on 2026-03-14
order_idamount
100130
100245
100320
100455
daily_orders after the failed attempt
order_idamount
100130
100245
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 all leave partial work behind when they crash.

The retry finishes the job, and the damage

The orchestrator waits 5 minutes and runs the task again at 02:08. The retry starts from the first line of the task. It reads all 4 orders for 2026-03-14 and appends all 4. This time the connection holds, the task turns green, and every downstream task is told the load succeeded.
order_idorder_dateamountwritten by
10012026-03-1430attempt 1
10022026-03-1445attempt 1
10012026-03-1430retry
10022026-03-1445retry
10032026-03-1420retry
10042026-03-1455retry

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

This failure needs 3 things at once: a write that appends, an attempt that fails after writing some of its rows, and something that runs the task again. Take away any one of them and the duplicates never appear, which is why the bug can hide for months in a pipeline that rarely fails.
Signs that a retry duplicated rows
  • ▸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_id appears 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.
The size of the overcount depends on how far the failed attempt got. Had it written all 4 rows and crashed before reporting success, the day would end with 8 rows and doubled revenue.
Multiple Choice

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?

On both attempts the task did what its code says. The flaw is in what the write does when it runs a second time, and that behavior has a name.

Idempotency in One Sentence

Daily Life
Interviews

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

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.
✓Idempotent: names the end state
  • 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.
✗Not idempotent: moves from the current state
  • 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

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.
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.
Here is the test applied to 5 operations. Stock starts at 0, and the table starts empty except in the last row, where it starts with the day's 4 orders.
OperationState after run 1State after run 2Idempotent?
Set stock of book 77 to 12stock = 12stock = 12Yes
Add 12 to stock of book 77stock = 12stock = 24No
Append the 4 orders for 2026-03-144 rows, $1508 rows, $300No
Replace the rows for 2026-03-14 with its 4 orders4 rows, $1504 rows, $150Yes
Delete every row dated 2026-03-140 rows for that day0 rows for that dayYes
The last 2 rows deserve a second look. A delete is idempotent because 'no rows for that day' is an end state, and deleting from an empty day leaves it empty. The replace shows that an idempotent operation may still do real work on its second run: it clears 4 rows and writes 4 rows again. The test compares what remains, and what remains is identical.
Multiple Choice

Stock for book 77 starts at 12. Apply the 2-run test to this statement. What does the test show?

UPDATE books
SET stock = stock - 1
WHERE book_id = 77

Hold the input still

Step 1 is the step most often skipped. If new orders reach the source between the runs, the end states can differ even for a write that is safe to rerun, and the comparison says nothing about the write. Fix the day and the source rows first. The rest of this lesson holds the source still and studies the write.
A good way to practice is to take any write statement from a pipeline you can read and fill in the 4 columns of the table above: the operation, the state after run 1, the state after run 2, and the verdict. Writing out those cells exposes an append long before a failed night does.

Replace, Do Not Append

Daily Life
Interviews

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)RowsRevenue
2026-03-123$95
2026-03-135$180
2026-03-144$150
After 3 clean nights the table holds 12 rows in 3 partitions, one per day.

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.

DELETE FROM daily_orders
WHERE order_date = DATE '2026-03-14'
INSERT INTO daily_orders
SELECT
order_id,
order_date,
amount
FROM orders
WHERE order_date = DATE '2026-03-14'

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.
StepWhat happensRows for 2026-03-14Revenue shown
Attempt 1 clearsThe partition is already empty0$0
Attempt 1 writes2 orders land, then the connection drops2$75
Retry clearsThe 2 leftover rows are removed0$0
Retry writesAll 4 orders land4$150
A third run, for any reasonClears 4 rows, writes the same 44$150
The retry never needed to know how far attempt 1 got. It emptied the partition and wrote the full day, so the leftover rows couldn't survive. Check the last 2 rows of the table against the 2-run test: 4 rows and $150 after one complete run, 4 rows and $150 after the next. The partitions for 2026-03-12 and 2026-03-13 were never touched and still hold their 8 rows.

Why this is the default for daily batch

What partition overwrite gives a daily pipeline
  • 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.
Overwrite has a cost. It rewrites the whole day even when a single order changed, so it does more writing than an append. For a daily batch table the trade is usually worth making: one day of rows is a bounded amount of work, and a table that can be rerun without inspection saves far more time than the extra writes cost.
✓Do
  • 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.
✗Don't
  • 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.
Multiple Choice

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?

Overwrite takes the question 'what did the last attempt leave behind?' and makes its answer irrelevant. That matters most at the hours when nobody is awake to answer it.

Why Retries Need Idempotency

Daily Life
Interviews

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.

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.
operational database
orders
nightly batch task
load daily orders
RETRY3IDEMPOTENCYnone: plain append
warehouse table
daily orders
read each morning
revenue dashboard

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

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 through it, or after it. In the third case the task writes all 4 rows, and then the connection drops before the warehouse's confirmation arrives, or the task hits its time limit while waiting. The rows are in the table and the task is red.
Where attempt 1 failedRows it left behindRows after an append retryCorrect?
Before the write04Yes, by luck
Midway through the write26No, 2 duplicates
After the write finished48No, every row doubled
All 3 cases look identical in the orchestrator: one failed attempt, then one successful retry. Only the first leaves a correct table. An append task is safe to retry only when it fails before writing anything, and the orchestrator can't tell which kind of failure it is retrying.

Tracing the accumulation

Duplicates add up across attempts. Take a bad night when the warehouse is slow to confirm. Each attempt writes all 4 orders and then times out while waiting, until the final retry gets its confirmation. The task uses all 4 of its attempts.
Append: the 2026-03-14 partition after each attempt
AttemptReportedRowsRevenue
1failed4$150
2failed8$300
3failed12$450
4succeeded16$600
Overwrite: the 2026-03-14 partition after each attempt
AttemptReportedRowsRevenue
1failed4$150
2failed4$150
3failed4$150
4succeeded4$150
With an append, the final count is the sum of everything every attempt wrote: 4 + 4 + 4 + 4 = 16 rows, and a dashboard showing $600 for a $150 day. With overwrite, each attempt replaces the work of the one before it, so the count is whatever the last attempt wrote, however many retries ran.

The choice without idempotency

✓Retries off
  • 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.
✗Retries on, append write
  • 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.
Neither column is acceptable, and idempotency removes the choice. When the write is a partition overwrite, a retry after any of the 3 kinds of failure ends in the same 4 rows, so the orchestrator can retry freely. A person who clicks rerun on last Tuesday's task performs that same second run.
Multiple Choice

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?

Turn retries on for a task once the 2-run test says its write can take a second run.

Side by Side: Idempotent vs Not

Daily Life
Interviews

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

Multiple Choice

A reviewer proposes 4 separate changes to Version A. Which one makes the task pass the 2-run test?

Questions to ask of any write
  • ▸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?
Multiple Choice

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.
PUTTING IT ALL TOGETHER

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

The overpayment is the duplicate-row failure from the first section: an append, a partial write and a retry, with a green task at the end.
The 2-run test would have caught it on paper: one run leaves each Friday trip once, and a second run on the same input leaves a second copy.
Partitioning the table by trip date and overwriting Friday's partition makes the retry clear the half-written rows before it writes the full day.
With that write in place the automatic retries become safe, whether the failure comes before, during or after the write.
In the task's code the change is one clear statement scoped to the run's day, placed ahead of the insert that was already there.
KEY TAKEAWAYS
An append adds rows without looking at the table, so a retry after a partial failure keeps the failed attempt's rows and adds a full copy of the day.
An operation is idempotent when running it 2 times, with the same input, leaves the same end state as running it once.
The 2-run test: fix the input, run the code, record the end state, run it again, and compare.
Partition overwrite clears one day and writes that day, so every attempt starts from empty and other days stay untouched.
An orchestrator can't see how much a failed attempt wrote, so automatic retries are safe only on tasks with idempotent writes.
In the fix, the property lives in the clear statement, its matching date filter, and a run day that is the same on every attempt.

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

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

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

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

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

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