What is idempotency and why is it needed?
Idempotency is a property of an operation in which repeating it produces the same result as the first execution.
In other words, if the same request runs twice (or more), the system stays in the same state, without duplication or errors.
Examples of idempotency
- Money transfer: a repeated request should not deduct the amount again.
- HTTP PUT: updating the resource
/user/1with the same data always produces the same result. - Sending an event: redelivery should not create a duplicate order or notification.
Why idempotency is needed
- Reliability in distributed systems: during failures or retries, the system does not lose consistency.
- Safe repeated calls: especially important for microservices and message brokers, where the same event can arrive several times.
- Resilience to network errors: on timeouts, an operation can be retried without the risk of a "repeated action".
How it is achieved
- Using unique operation identifiers.
- Checking the state before executing ("if already processed, skip").
- Logging all processed events or transactions.
Summary:
Idempotency guarantees that a repeated call does not change the result. It is a key principle of reliable microservices, where retries can happen due to network errors or message redelivery.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.