An idempotent operation is one that will have no effect if it’s received a second time.
Simply put, an operation is idempotent if it produces the same result when called over and over.
An identical request should return an identical result when done twice, two thousand, or two million times.
In computer science, the term idempotence may have a different meaning depending on the context in which it is applied:
- in imperative programming, a subroutine with side effects is idempotent if the system state remains the same after one or several calls.
- in functional programming, a pure function (Deterministic?) is idempotent if it is idempotent in the mathematical sense given in the definition.
It means operation can be repeated or retried as often as necessary without causing unintended effects.
- A function looking up a customer’s name and address in a database is typically idempotent, since this will not cause the database to change.
- Changing a customer’s address to XYZ is typically idempotent, because the final address will be the same no matter how many times XYZ is submitted.
- NOT IDEMPOTENT: However, placing an order for a cart for the customer is typically not idempotent, since running the call several times will lead to several orders being placed.
- Canceling an order is idempotent, because the order remains canceled no matter how many requests are made.