Quick revision
The short answer
Each pattern breaks the rule that an idempotency key must identify the operation, not the attempt, or must outlive every retry. The app makes a new key per launch, so one purchase looks like several; the gateway invents its own key per retry, so the server never sees a repeat; and the 3-day-old replay arrives after the 24-hour table has forgotten the key. We fix the key's origin and lifetime in each case, and add a second defence based on business identity, a unique constraint or state machine per order, so one order can never be charged twice whatever the keys say.
What to remember
In this answer, we saw why a 24-hour idempotency table still let 40 duplicate charges a week through. The app created a key per attempt instead of per purchase, the gateway invented keys on retry, and a replay arrived after the key had expired. We fixed each by making the key identify the operation and travel with it for its whole life, added a partial unique index on order and charge type as a key-independent second defence, and monitored double charges per order until the count reached zero.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Each pattern breaks the rule that an idempotency key must identify the operation, not the attempt, or must outlive every retry. The app makes a new key per launch, so one purchase looks like several; the gateway invents its own key per retry, so the server never sees a repeat; and the 3-day-old replay arrives after the 24-hour table has forgotten the key. We fix the key's origin and lifetime in each case, and add a second defence based on business identity, a unique constraint or state machine per order, so one order can never be charged twice whatever the keys say.
Why this question is asked
An idempotency table looks like a complete solution to duplicate charges, and the team was surprised to find 40 a week getting through. The table itself is fine; the problem is where the keys come from and how long they live. Interviewers ask this to see whether we understand that idempotency is a contract that spans the client, every hop in between and the server. In this answer, we will recap how an idempotency table works, take each of the three patterns in turn, add a second line of defence that does not depend on keys at all, and finish with monitoring.
1A Quick Recap of Idempotency Keys
An idempotency key is a unique value the client attaches to an operation, usually in an Idempotency-Key header. The server records each key with the result of the operation. If the same key arrives again, the server returns the stored result instead of charging again. This makes it safe for a client to retry after a timeout, because the retry carries the same key.
The whole mechanism rests on one rule: every attempt of the same operation must carry the same key, and the server must still remember that key when the attempt arrives. Each of the three patterns breaks one half of that rule.
| Pattern | What breaks | Fix |
|---|---|---|
| App restarts make a new key | key identifies the attempt, not the purchase | create key at confirm, persist on device |
| Gateway generates its own key | each retry gets a different key | forward client key; no key, no retry |
| DLQ replay after 3 days | table forgot the key after 24 hours | replay with original key, check state first |
2Pattern 1: The App Makes a New Key When It Restarts
The mobile app generated the key in memory when it sent the request. A user taps Pay, the request times out on a weak network, the user closes the app and opens it again, and the app shows the pending purchase and sends it again with a fresh key. To the server, that is a brand-new operation, and it charges again.
The key must identify the purchase. The app should create it once, at the moment the user confirms, and store it with the pending order in the device's local storage before the first send. Every retry, including after a restart or a crash, then reads the same key. We can also make the key deterministic from the client's own order id, for example purchase:client-order-5521, which survives even a reinstall if the order id comes from the server.
3Pattern 2: The Gateway Retries With Its Own Key
The API gateway was configured to retry failed upstream calls, and because some clients send no key, it generated one for every attempt. The result is the worst of both worlds: a timeout between the gateway and the payments service leads to a retry with a new key, so the service charges twice.
6 messages in order. Play them, or step through them one at a time.
The payments service sees two different keys and treats them as two operations.
The fix has two parts. The gateway must forward the client's key unchanged and never replace it. And when a non-idempotent request, such as POST /charges, arrives without a key, the gateway must not retry it at all; it returns the error to the client, who knows whether to try again. Better still, the payments API rejects charge requests without a key, which forces every client to supply one.
4Pattern 3: The Dead-Letter Replay Three Days Later
A charge request failed in an asynchronous step, landed in a dead-letter queue, and after a fix three days later an operator replayed it. The original attempt had actually succeeded on a retry, but the table only keeps keys for 24 hours, so the replayed key looked new, and the customer was charged again.
There are three ways to close this, and they work well together. The replayed message must carry the original idempotency key, which means the key travels inside the message, not only in an HTTP header. The table's retention must cover the longest replay delay; if replays can happen up to 7 days later, keep keys for at least 30 days. And the replay tool should check the payment's current state before resending, skipping orders that already have a successful charge.
5A Second Line of Defence: Business Identity
All three patterns have one thing in common: they depend on keys being right. A second defence that does not depend on keys at all makes the system robust to the next mistake we have not thought of. The business rule is simple: one order is charged at most once for a given charge type. We let the database enforce it.
The partial unique index allows a new attempt after a failed one, since failed rows do not count, but it makes a second pending or successful charge for the same order impossible, whatever key it carries. A per-order state machine (CREATED, CHARGING, PAID) achieves the same with a conditional update. And the key sent to the payment provider should also derive from the order and charge type, so even a bug that bypasses our database is stopped by the provider.
6Monitoring Until the Rate Is Zero
Finally, we watch for the outcome we care about: two successful charges for one order. A query or stream job that counts orders with more than one SUCCEEDED capture per day gives a direct measure. We alert on any non-zero value and review every case. Each case points to a path we missed, a client version, a gateway route or a support tool, and we close it until the weekly count stays at zero.
7Follow-up questions
Should idempotency keys be stored with the request body hash?
Yes. Storing a hash of the request lets the server detect a client that reuses a key for a different operation, for example a different amount, and reject it with an error instead of returning a misleading stored result.
Why not simply keep idempotency keys forever?
Storage grows without bound and old keys are rarely useful. A retention that covers the longest realistic retry or replay, plus the business-identity constraint for anything older, gives the same protection at a fraction of the cost.
What should the API return when the original request is still in progress?
A conflict response such as 409, telling the client to retry later, rather than starting a second charge or waiting indefinitely. The client retries with the same key and eventually gets the stored result.
8Conclusion
In this answer, we saw why a 24-hour idempotency table still let 40 duplicate charges a week through. The app created a key per attempt instead of per purchase, the gateway invented keys on retry, and a replay arrived after the key had expired. We fixed each by making the key identify the operation and travel with it for its whole life, added a partial unique index on order and charge type as a key-independent second defence, and monitored double charges per order until the count reached zero.
Open with
All three patterns break the same rule: an idempotency key has to identify the operation, not the attempt, and the server has to remember it for as long as any retry can arrive.
Then, if they want more
The mobile app generated a key per send, so after a restart the same purchase arrived with a new key and looked like a new operation. The fix is to create the key once when the user confirms and persist it with the pending order on the device, so every retry reuses it.
The gateway invented its own key for each retry, so a timeout between gateway and payments service produced two different keys and two charges. The gateway must forward the client's key unchanged, and if there is no key, it must not retry a non-idempotent request at all; ideally the API rejects charges without a key.
The dead-letter replay came three days later, after the twenty-four-hour retention, so it looked new. Replays must carry the original key inside the message, retention must cover the longest replay delay, and the replay tool should check the payment state first.
Beyond keys, I'd add a second line of defence based on business identity: a partial unique index on order id and charge type for pending or succeeded payments, or a per-order payment state machine, so a second charge for one order is impossible whatever the key says.
Then I'd alert on any order with two successful charges and review each case until the rate is zero.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain why each pattern defeats the idempotency table and how you would fix them.": https://scrivio-interview-library.netlify.app/architecture/distributed-systems/1d8dafd852/