Skip to content

The idempotency key that hid a changed order

Failure story 02, in requests, retries and side effects.

A customer orders 1 unit. The request times out. They change the quantity to 3 and press Place order again, and the client reuses the original idempotency key. The server replies Order confirmed. What did the customer get?

Assumption Value
Client Creates one idempotency key per checkout and reuses it for every submission from that screen
Server Looks up the key. If it has a stored response, returns it; otherwise processes the order and stores the response for 24 hours
What the server compares The key only

The previous story, the reservation that succeeded twice, was caused by changing the key too often. This one is caused by not changing it when it should change.

Step What happens
1 The customer submits quantity 1 with key k-44.
2 The server reserves 1 unit, stores the response Confirmed: 1 unit against k-44, and the response is lost.
3 The customer sees a timeout, decides to buy for two friends as well, and changes the quantity to 3.
4 The client still has k-44, so it submits quantity 3 with k-44.
5 The server finds k-44 and returns the stored response, Confirmed: 1 unit, with status 200.
6 The client, which only checks for 200, shows Order confirmed next to the quantity the customer last typed: 3.

The customer believes they bought 3 units. The warehouse ships 1.

An idempotency key is a promise: this is the same request as before. The client broke the promise when the request changed, and the server had no way to notice, because it only compared keys. The cached response was correct for the request it answered; it was simply answering a different request from the one the customer made.

Aspect Design A: key-only lookup Design B: key bound to the request
What the server stores Key → response Key → fingerprint of the meaningful fields, and the response
Same key, same request Replays the response Replays the response
Same key, changed request Replays the old response as if it answered the new one Rejects with 409 Conflict (or 422): This key was used for a different request
What the client must do Nothing Create a new key when the customer changes the order, and handle a conflict by asking the customer

The fingerprint covers what the customer means, not every byte: product, quantity, price and delivery address, for example, but not a timestamp or a tracing header that legitimately differs between attempts. Choosing those fields is a design decision worth writing down.

fingerprint = hash(customerId, productId, quantity, unitPrice, deliveryAddressId)
on request(key, body):
stored = lookup(key)
if stored is none: process, then save(key, fingerprint(body), response)
elif stored.fingerprint == fingerprint(body): return stored.response
else: return 409 "key reused for a different request"
  1. A changed request can’t borrow an old answer. Submit quantity 1 with key k-1, drop the response, then submit quantity 3 with k-1. Expected: the server doesn’t return Confirmed: 1 unit as a success for this request, and the screen can’t show 3 units ordered while only 1 is reserved.
  2. A true retry still replays. Submit quantity 1 with k-2 twice. Expected: one order, and the second response matches the first.
  3. The UI reads the response, not its own form. Whatever the server returns, the confirmation screen shows the quantity from the response.

This check is in the checks catalog as test templates in Java and Python.

  • The client can never change the request between attempts: the retry is automatic and the form is locked while it’s in flight.
  • Keys are generated per request body by construction, for example as a hash of the body, so a changed body always gets a new key. This has its own trap: two deliberately identical orders get the same key.

Discussion question. Payment providers that support idempotency keys typically reject a reused key with different parameters. Why might a provider choose to reject, rather than quietly process the new request as a new order?

Exercise. Your fingerprint includes the unit price. Between the first attempt and the retry, a price change takes effect, so the server computes a different price for the same product and quantity. The customer changed nothing. What happens under Design B, and how would you fix it?

Worked answer

The retry gets a conflict even though it’s a true retry, because the fingerprint used a value the server derived at processing time, not a value the customer sent. The fix is to fingerprint only what the client submitted: product, quantity and the price the customer was shown and agreed to, sent in the request. The server then decides separately whether it will honor that price. The rule: fingerprint the caller’s intent, never server-derived state that can change between attempts.

Glossary: idempotency

Written and edited by Abhinav Taduka, enterprise software architect.