Why Retries Need Idempotency
Concepts covered: Idempotency
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.
- orders (source, operational database)
- load_daily_orders (transform, nightly batch task); retries: 3; idempotency: none: plain append
- daily_orders (storage, warehouse table)
- revenue dashboard (consumer, read each morning)
- orders -> load_daily_orders
- load_daily_orders -> daily_orders
- daily_orders -> revenue dashboard
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 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 |
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.
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.
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?
- 4 rows and 0 duplicates, because the successful attempt replaces the earlier partial writes.
- 12 rows and 8 duplicates, because each of the 3 attempts counts as a full load of 4.
- 7 rows and 3 duplicates, because only the first failed attempt leaves its rows behind.
- 8 rows and 4 duplicates, because the 3 + 1 rows from failed attempts stay under the final 4.
Turn retries on for a task once the 2-run test says its write can take a second run.
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.