Programming Terms: Idempotence
Based on Corey Schafer's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.
Idempotence means repeating an operation does not change the result or server state after the first application.
Briefing
Idempotence means an operation can be repeated multiple times without changing the outcome after the first application. In practical programming terms, it’s about whether “doing it again” builds on the previous result or leaves the system in the same state. That distinction matters most when designing APIs and choosing HTTP methods, because clients may retry requests and the server should ideally avoid unintended side effects.
A simple way to see the concept is to compare a function applied once versus applied repeatedly to its own output. With a Python example, a function that adds 10 to an input behaves differently depending on how many times it’s run: starting from 10, one application produces 20, but feeding 20 back into the same function produces 30. Because the result changes each time, the operation is not idempotent.
The contrast comes from operations that “stabilize” after the first run. Python’s built-in abs function illustrates this well: abs(-10) returns 10, and abs(10) returns 10 again. Reapplying abs keeps producing the same value, so the operation is idempotent. The idea isn’t limited to functions—an assignment like a = 10 is also idempotent in the sense that executing it repeatedly leaves the variable at the same value each time.
Where idempotence becomes especially relevant is HTTP. The GET method is treated as idempotent because it retrieves a resource without changing server state; reloading the same URL repeatedly returns the same representation. PUT is also considered idempotent in typical usage because it updates a resource to a specified value. If a client repeatedly sends “set user to Cori,” the resource ends up in the same state after the first request, and subsequent identical PUTs don’t keep modifying it.
POST, by contrast, is not idempotent because it’s commonly used for actions that change data in the background. A voting endpoint is a classic example: each POST request can record a new vote, so repeating the request can produce a different outcome (for instance, casting another vote up or down).
DELETE is often described as idempotent even though it may return different responses like 404 after the resource is gone. The key is server state: once the user is deleted, further DELETE calls don’t change anything beyond the first successful deletion. After the first call, the system remains in the same state, which is why DELETE is categorized as idempotent.
A useful rule of thumb from the examples: the first application may change the value (like turning -10 into 10), but repeated applications should not keep moving the result forward. When operations are idempotent, retries and repeated calls become safe and predictable—especially in web APIs.
Cornell Notes
Idempotence is the property of an operation where repeating it does not change the result or server state after the first application. In the transcript’s examples, a function that adds 10 is not idempotent because applying it repeatedly keeps increasing the value (10 → 20 → 30). Python’s abs is idempotent because once the number is non-negative, applying abs again leaves it unchanged (abs(-10) = 10, abs(10) = 10). In HTTP, GET and PUT are treated as idempotent because they retrieve data or set a resource to a specific value without further changes. DELETE is also idempotent because after the resource is removed, additional deletes don’t change the server state further.
How can you test whether a function is idempotent using the “apply it twice” idea?
Why is abs considered idempotent while “add 10” is not?
Can idempotence apply to non-function code like assignments?
Why is GET idempotent in HTTP?
Why is PUT idempotent but POST is not?
How can DELETE be idempotent even if it might return 404 on later calls?
Review Questions
- Give an example of an operation that is not idempotent and show the difference between f(X) and f(f(X)).
- Explain why PUT can be idempotent even though it changes data, while POST is generally not idempotent.
- In your own words, distinguish “idempotent result” from “idempotent server state,” using DELETE as the example.
Key Points
- 1
Idempotence means repeating an operation does not change the result or server state after the first application.
- 2
A quick test is to compare f(X) with f(f(X)); if they match, the operation is idempotent.
- 3
abs is idempotent because once a value is non-negative, applying abs again leaves it unchanged.
- 4
HTTP GET is idempotent because it retrieves data without modifying server state.
- 5
HTTP PUT is idempotent when it sets a resource to a specific value repeatedly.
- 6
HTTP POST is typically not idempotent because repeated requests can create additional changes (e.g., repeated votes).
- 7
DELETE is idempotent because after the first deletion, further deletes don’t change server state even if responses differ (like 404).