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

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.

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.

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.