Designing Idempotency: Safe Retries and Duplicate Requests
An idempotent operation produces the same final state when the same logical request is processed more than once. In distributed systems, a timeout can leave the caller unsure whether the server handled a request, so it may retry. The goal is not to eliminate every duplicate delivery; it is to prevent duplicate side effects with a stable operation key and atomic persistence.
An idempotent operation produces the same final state when the same logical request is processed more than once. In distributed systems, a timeout can leave the caller unsure whether the server handled a request, so it may retry. The goal is not to eliminate every duplicate delivery; it is to prevent duplicate side effects with a stable operation key and atomic persistence.
Why idempotency matters
Suppose a client loses its connection before receiving a payment response. The server may already have approved the payment, or it may never have received the request. Sending it again could charge the same order twice. A message broker can also redeliver a message if a consumer stops after processing but before acknowledging it.
Retries help recover from transient network errors and throttling, but repeated calls can cause duplicate charges or partial updates if each call changes state again. AWS guidance recommends making retryable operations idempotent and using bounded backoff for transient failures. Idempotency is the correctness rule that makes retries and duplicate delivery safe.
Choose a stable operation key
Assign each logical operation a key that remains unchanged across retries and redelivery. For an HTTP API, a client can send an idempotency key for each order-creation or payment attempt; the server can store it within the relevant user or account scope. A message consumer can use a producer-assigned message ID or another stable event identifier. Do not use an ID generated anew for every delivery attempt, a timestamp, or a correlation ID shared by several messages.
Store a request hash or important input fields with the key so you can detect accidental reuse with different content. For example, if the first request under order-741 creates a 25,000-won order and a later request reuses that key for 40,000 won, reject it as a conflict or send it for review instead of silently returning the earlier success. This is an illustrative design, not a provider-specific guarantee.
Make the check and operation atomic
A two-step “check whether the key exists, then process if it does not” is insufficient. Two requests arriving together can both see no key and both perform the operation. Use a database uniqueness constraint, atomic conditional insert, or transaction so only one request can claim the key.
When possible, commit the idempotency record and business change in the same transaction. For example, save the order row and operation key together so both commit or both roll back. If the key is saved first and the process stops before the business change, a retry might skip work forever. If the change commits but the key does not, a retry might apply it twice. Verify the storage system’s atomicity boundary and transaction scope.
When a completed key arrives again, return the previously stored outcome or a duplicate-success response without applying another change. For a key still in progress, decide whether to wait, return its current state, or ask the caller to retry. Do not mark every failed key as complete; distinguish retryable failures from cases where a side effect may already have occurred.
Handle side effects outside the transaction
A local database transaction cannot by itself prevent duplicate effects from an external payment service or email provider. If the external service accepts idempotency keys, pass the same logical operation key and confirm the provider’s scope and retention period. Otherwise, store states such as in progress, completed, and needs reconciliation. After a timeout, first check or reconcile the external result before deciding whether to call again. Do not treat an uncertain outcome as a definite failure and charge again.
For message consumers, an Inbox pattern can store the processed key and business data in one transaction. If the key and business data live in different systems or processing combines multiple external operations, a transactional outbox, compensating action, or reconciliation job may be needed. The choice depends on which writes can share an atomic boundary.
Set retention and verify behavior
If an idempotency key is deleted too early, a late redelivery or client retry can run the operation again. Set retention by considering the broker’s maximum redelivery period, message expiry, manual replay window, and API client retry period. Document what happens when a request with the same key arrives after retention expires.
Track duplicate detections, key conflicts, stale in-progress records, retries, and reconciliation outcomes. Check that:
Repeating the same key and request leaves the business state and side effects as if it ran once.
Reusing a key with different content is clearly rejected.
If two instances process one key concurrently, only one commits the business change.
Redelivery after a process stops before replying can recover the stored result.
A timed-out external call is not repeated without checking its outcome.
Idempotency does not limit the number of calls, nor does it mean every error should be retried automatically. It is a design rule: identify work consistently, make the business change and duplicate marker atomic where possible, and verify or reconcile results outside that atomic boundary.
References
[Microsoft Learn: Idempotent Consumer pattern](https://learn.microsoft.com/en-us/azure/architecture/patterns/idempotent-consumer)
[AWS Prescriptive Guidance: Retry with backoff pattern](https://docs.aws.amazon.com/prescriptive-guidance/latest/cloud-design-patterns/retry-backoff.html)