Idempotency in One Sentence
Concepts covered: Idempotency
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.
| Operation | State after run 1 | State after run 2 | Idempotent? |
|---|---|---|---|
| Set stock of book 77 to 12 | stock = 12 | stock = 12 | Yes |
| Add 12 to stock of book 77 | stock = 12 | stock = 24 | No |
| Append the 4 orders for 2026-03-14 | 4 rows, $150 | 8 rows, $300 | No |
| Replace the rows for 2026-03-14 with its 4 orders | 4 rows, $150 | 4 rows, $150 | Yes |
| Delete every row dated 2026-03-14 | 0 rows for that day | 0 rows for that day | Yes |
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.
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- Not idempotent: stock is 11 after run 1 and 10 after run 2, so the end states differ.
- Idempotent: the WHERE clause limits the change to one book, so both runs leave stock at 11.
- Idempotent: run 2 finds the stock already lowered to 11 and has nothing left to change.
- Not idempotent: run 2 fails with an error, because run 1 already updated that row.
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.
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.