Definition: Property of an operation that produces the same result regardless of how many times it is executed, fundamental for reliability in distributed systems.
— Source: NERVICO, Product Development Consultancy
What Is Idempotency
Idempotency is a property of operations where executing the same action one or multiple times produces exactly the same result in the system. In the context of APIs and distributed systems, an idempotent operation guarantees that if a request is sent twice (for example, due to a timeout and a retry), the effect on the server is identical to having sent it once.
How It Works
Idempotency is typically implemented through idempotency keys: the client generates a unique identifier for each logical operation and includes it in the request. The server stores the key along with the result of the first execution. If it receives the same key again, it returns the stored result without executing the operation again. In HTTP, GET, PUT, and DELETE methods are idempotent by design, while POST is not by default and requires explicit implementation of idempotency keys.
Key Use Cases
- Safe payment processing where a retry must not generate a duplicate charge to the user
- Resource creation APIs handling automatic retries without creating duplicates
- Messaging systems where a message may be delivered more than once (at-least-once delivery)
- Infrastructure as code operations where executing the same script multiple times produces the same final state
Advantages and Considerations
Idempotency is fundamental for building reliable distributed systems, as network failures and retries are inevitable. It drastically simplifies client error recovery logic, which can safely retry any failed operation. The main consideration is the cost of storing idempotency keys and defining their time-to-live. It also requires care in composite operations where the idempotency of the overall operation does not follow from the idempotency of its parts.