Part 03 / Topic 4 of 6
Idempotency
A job is idempotent when running it again with the same input leaves the same result. This is the single most valuable property an integration job can have, because in real deployments re-runs are normal: a network fails midway, a scheduler fires twice, someone re-runs yesterday's job "just in case". If the job is idempotent, recovery is simply "run it again".
The difference is usually one design decision — write keyed by identity, instead of appending:
# examples/02_idempotency_test_running_a_job_twice.py
def load(records, table):
for r in records:
table[r["id"]] = r # keyed upsert: same input -> same state
return table
batch = [{"id": "S-1001", "status": "delivered"}, {"id": "S-1002", "status": "in_transit"}]
once = load(batch, {})
twice = load(batch, load(batch, {}))
print("idempotent:", once == twice, "| rows:", len(twice))
appended = []
for _ in range(2):
appended.extend(batch) # append-only load: NOT idempotent
print("append-only rows after two runs:", len(appended))
# idempotent: True | rows: 2
# append-only rows after two runs: 4
And it is testable: run the job twice, assert the state matches a single run. That one test prevents the classic integration incident — duplicated rows after a retry. Part 7 turns this into a real SQL upsert.
🧪 Quick quiz
- A job is idempotent when…
Show the answer
Running it again with the same input leaves the same result. A keyed upsert gave 2 rows after two runs; an append-only load gave 4. (verified against the example output above)
- Why does idempotency matter so much in integrations?
Show the answer
Retries and re-runs are normal after failures, and they must not create duplicates. Networks fail midway — if a re-run is safe, recovery is simply "run it again".
Done here? The practice set for Part 3 — 4 exercises with solutions — is on the part page.