What does it mean for an operation to be idempotent?
Sketch your own answer first, then compare it with the one below.
A strong answer
Doing it more than once has the same effect as doing it once. Setting a field to 5 is idempotent. Adding 5 to it is not.
It matters because networks lose replies. A client that did not hear back cannot tell whether the request ran, so it retries. If the operation is idempotent, the retry is harmless. If not, the retry may charge a card twice.
HTTP defines some methods as idempotent, such as PUT and DELETE. A POST is not, so APIs that create things need an extra mechanism.
Sources
- Roy Fielding, Mark Nottingham, Julian Reschke (2022). RFC 9110: HTTP Semantics, section 9.2.2 Idempotent Methods.
- Jayadeba Jena, Sanjay Dalal (2024). The Idempotency-Key HTTP Header Field.