Loading…
A short voice session: one question, one follow-up, then instant feedback — up to 5 minutes.
The client sends a unique idempotency key with the operation, and the server stores the key together with the result of the first successful processing. Retries with the same key return the original result instead of executing the business operation again.
Idempotency is important when clients may retry requests because of timeouts, network failures, or connection interruptions. Without idempotency, a retry of a payment or order request could create two charges or two orders.
A common design uses an idempotency key supplied by the client:
POST /payments
Idempotency-Key: 8b7f2a1c-...The server stores the key with enough information to identify the operation and the resulting response. A simplified table could contain key, user_id, request_hash, status, and the response or resource identifier.
The server should enforce uniqueness at the database level. When the same key is received again, the service checks the stored record. If the original operation completed, it returns the same result. If processing is still in progress, the service needs an explicit policy, such as returning a conflict or waiting for the original operation.
The request payload should normally be associated with the idempotency key. If the same key is reused with a materially different payload, the server should reject it rather than silently treating it as the original operation.
For distributed systems, the important part is atomicity. Creating the idempotency record and transitioning the business operation must be designed so concurrent requests cannot both perform the operation. Database uniqueness constraints, transactions, and carefully designed state transitions are commonly used.
Idempotency does not mean every HTTP method is automatically safe. It is a business-level guarantee that repeated processing of the same logical operation produces the same intended effect.