Idempotency key
Ключ идемпотентности
A client sends this identifier on the first attempt and reuses it on retries of the same operation. It lets the server recognize a retry without creating another order or charging the customer again.
Why retries need a key
Suppose a checkout app asks a payment API to charge a customer, but the request times out. The app can't tell whether the server missed the request or completed the charge and lost the response. Sending another ordinary POST risks a second charge. An idempotency key tells the API that both attempts belong to the same payment. Two separate purchases, even for the same amount, need different keys.
What the client and server do
The client generates a value, often a random UUID, before the first attempt and keeps it for retries. In HTTP APIs that support the feature, it commonly sends the value in an Idempotency-Key header.
The server registers the key within a documented scope, such as the merchant account and the operation. It associates the key with the request parameters and tracks the outcome. If a matching request arrives after completion, the API can replay the original response or return a reference to the existing resource, depending on its contract. If the same key arrives with a different amount or currency, the server should reject it rather than silently use either set of parameters. If the first attempt is still running, the API might tell the client to retry later.
Example: retrying a payment
POST /payments HTTP/1.1
Host: api.example.com
Idempotency-Key: 1f8cf2f2-817c-46f5-9e7a-17026147e28e
Content-Type: application/json
{"amount":2500,"currency":"USD"}
Here amount is in cents. If the response times out after the API creates payment pay_123, retrying with the same key and parameters can recover that payment ID without charging the customer again. Sending a fresh UUID on the retry defeats the mechanism. So can retrying after the API's key retention window: once the record expires, the server may treat the request as new.
Where it helps and what it doesn't guarantee
The same mechanism is useful for order creation and job submission. The key identifies the caller's intent, not just the request contents: two independent purchases can have identical bodies. A fingerprint of relevant parameters can detect mismatches under a reused key, but it isn't a substitute for the key. Nor does adding a header make an arbitrary API idempotent; the server must implement and document the behavior.
How it comes up in interviews
Expect to explain what happens when two workers receive the same key at once. A separate lookup followed by a write can race: both workers see no record and both proceed. When all writes are local, a database uniqueness constraint and a transaction can tie the key, the business change, and the recorded outcome together. Then consider a crash after an external payment provider accepts a charge but before the local database records the result. A local transaction cannot undo that charge. Discuss reusing a stable key with the provider and reconciling uncertain outcomes before initiating another payment.