What You’ll Master Here
Idempotent means the number of times a task ran is not observable in its output. If you can tell, it is not.
Chapter 12 gave you retries, clearing and backfills. Every one of them re-runs code that has already run at least once. This chapter is the assumption underneath all three: that running a task twice leaves the world in the same state as running it once.
Pick a write pattern below and run it more than once. Only one of the four gets the row count wrong, and it is the one most people write first.
INSERT INTO orders
SELECT * FROM staging_orders
WHERE ds = '{{ ds }}';Idempotent means the number of times a task ran is not observable in its output. If you can tell, it is not.
This is not a theoretical property. Retries happen automatically, clearing happens during incidents, and backfills happen whenever history needs rebuilding. A task that is not idempotent corrupts data on every one of those events — silently, because each individual run succeeds.
- Idempotent
- Running it twice produces the same result as running it once. The precondition for every recovery mechanism in Airflow.
- Partition
- The slice of the target a run is responsible for — usually a date. Scoping writes to it is what makes replacement possible.
- Atomic
- A reader never sees a half-finished state. A different property from idempotency, solving a related problem.
Assuming a task is idempotent because it has never visibly broken. An append-only task looks perfect until the first retry. It then doubles a day of data, reports success, and the error is found weeks later by someone querying a total that does not reconcile.
Ask of every write: what happens if this runs twice? If you cannot answer immediately, it is not safe.
Treat idempotency as a requirement of the task, not a property you hope for.
Verify by running twice and comparing, rather than by reasoning about the code.
If you can look at the output and tell how many times the task ran, it is not idempotent. That single test catches almost every case.
Every recovery tool in Airflow re-runs your code. Idempotency is what makes that a recovery rather than a second failure.
