Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path →
Idempotency Key Handler
Networks retry. A customer’s webhook fires twice; a payment request is re-sent after a timeout. Without idempotency you double-charge or double-write. The fix: dedupe by a client-supplied key and always return the first result. Solve it in Python or TypeScript, with hidden tests.
The problem
Implement process(requests). Each request is [key, value]. The first time you see a key, record its value as the result. Any later request with the same key must return that first recorded result — the new value is ignored (the operation already happened). Return the list of results, one per request, in order.
Your turn — write it
Edit the stub, hit Run (or ⌘/Ctrl + Enter), and watch the hidden tests. Stuck? open the hints under the problem, or Reveal solution is one click away.
Implement process(requests): keep a map of key → first result. For each request, if the key is new, store its value; then append the stored result for that key.
Examples and constraints 4 examples
requests = [["k1","a"],["k1","b"],["k2","c"],["k1","d"]]["a", "a", "c", "a"]requests = [["a","1"],["b","2"],["a","9"],["b","8"]]["1", "2", "1", "2"]requests = [["k",""],["k","x"]]["", ""]requests = [][]- The result for a key is fixed by its first occurrence; later values for that key are discarded.
- Return one result per request, preserving order.
- This is exactly how an idempotency-key store makes a retried POST safe.
Hints 3 ideas
- Use a dict/Map from key to the first value seen.
- On each request: if the key isn’t stored yet, store the value.
- Always append the stored value for the key (which is the first one) to the output.
Approach, complexity & discussion — open after you solve
Start from brute force
The obvious first version answers each request by looking back: for request i, scan requests[0..i] from the start and return the value of the first request with the same key. It works because the first occurrence is, by definition, the result for that key, and an empty-string first value is handled correctly because nothing is tested for truthiness. It costs O(n²) time and O(1) extra space beyond the output. With all-distinct keys every scan runs all the way to i, so 100,000 requests take about 5 billion comparisons, and a real server would be rescanning its whole request history on every call.
The approach
The insight is that “first write wins” needs only one fact per key: the value its first request carried. Remember that fact in a hash map and every later request becomes a constant-time lookup instead of a scan. The invariant: after request i, store maps every key seen so far to the value from its first occurrence, and nothing else.
The reference keeps store = {} and out = []. For each key, value it tests membership with if key not in store and only then writes store[key] = value, so a retry can never overwrite the original. It then appends store[key], not value, to out: for a new key the two are the same, and for a replay it is the first result. Testing membership rather than truthiness matters, because a first value of "" must still win. One pass, one dict.
"b" and "d" never appear in it even though they arrived. In a real service the store entry would also hold the full response, status code and body, so that a replay is byte-for-byte the answer the client missed the first time.Walk through an example
The first example, one row per request: only a new key writes to store, and every row appends the stored value.
| step | request | key in store? | store after | out after |
|---|---|---|---|---|
| 1 | ["k1", "a"] | no: store "a" | {k1: "a"} | ["a"] |
| 2 | ["k1", "b"] | yes: ignore "b" | {k1: "a"} | ["a", "a"] |
| 3 | ["k2", "c"] | no: store "c" | {k1: "a", k2: "c"} | ["a", "a", "c"] |
| 4 | ["k1", "d"] | yes: ignore "d" | {k1: "a", k2: "c"} | ["a", "a", "c", "a"] |
Complexity
Hash map: O(n) time for n requests, since each does one average O(1) membership test and at most one insert (hashing a key costs its length), and O(u) space for u distinct keys plus O(n) for out. Brute force: O(n²) time, because each request rescans everything before it, and O(1) extra space.
Edge cases to test
- An empty request list returns
[]. - A falsy first value,
[["k", ""], ["k", "x"]], returns["", ""]. A truthiness check such asif not store.get(key)treats""as missing, overwrites it, and returns["", "x"]. - Interleaved keys,
[["a", "1"], ["b", "2"], ["a", "9"], ["b", "8"]], return["1", "2", "1", "2"]: each key keeps its own first result. - Keys are compared exactly, so
[["k1", "a"], ["K1", "b"]]returns["a", "b"]: two keys that differ only in case are two different operations. - An immediate replay with the same value,
[["k", "v"], ["k", "v"]], returns["v", "v"], and the second request must still be treated as a replay, not a new operation.
Common mistakes
- Writing
store[key] = valueon every request. Last write wins, and the first example returns["a", "b", "c", "d"]. - Checking truthiness instead of membership (
if not store.get(key)): an empty-string first value counts as missing and gets overwritten. - Storing the response only after executing, with no atomicity — a concurrent retry then double-executes.
- No expiry on stored keys, so the store grows without bound.
- Keying on the wrong thing (the whole payload, or a value that changes between retries).
Follow-ups an interviewer asks next
- Two copies of the same request arrive at the same moment on two servers.
- The check and the write must be one atomic step, or both copies see “not stored” and both execute. Use an insert-if-absent on a shared store, such as a unique constraint on the key column or Redis
SET key value NX, and record the key as “in progress” before doing the work. The loser of the race then waits for the stored result or gets a conflict error instead of executing. - The same key arrives with a different payload.
- This handler silently returns the old result, which hides a client bug: the key was reused for a different operation. Store a hash of the request body next to the key and compare it on every replay. A match returns the stored response; a mismatch returns an error instead of a response that belongs to another request.
- The store grows without bound. How do you expire keys?
- Give each entry a time-to-live longer than the longest window in which a client might retry, and delete entries older than that, with a TTL in Redis or a periodic cleanup job in a database. Do not evict by memory pressure alone, as an LRU cache would: a key evicted early lets a late retry execute a second time.
- The server crashes after charging the card but before storing the result.
- Then the retry finds no key and charges again. When the side effect is local, write the key, the effect and the stored response in one database transaction. When it is a call to another service, record the key as started before the call and pass the same idempotency key downstream, so that service deduplicates the retry too.
Where this shows up
Idempotency keys are what make payment and order APIs safe to retry: the network will make a client resend after a timeout, and this handler guarantees “charge once” even when the request arrives twice. Stripe’s API, for example, accepts an Idempotency-Key header on POST requests for exactly this reason, and an IETF draft specifies the header for HTTP APIs in general.
Exactly once is built, not delivered. A network can offer at-most-once delivery (send once, maybe lose it) or at-least-once delivery (retry until acknowledged, maybe duplicate), but not exactly-once over an unreliable link: the sender can never tell a lost request from a lost reply. Systems that advertise exactly-once processing combine at-least-once delivery with deduplication on the receiving side, which is this handler. Kafka’s idempotent producer is one example: the broker tracks a producer ID and a per-partition sequence number and discards a retried write it has already appended.
Webhooks and queues. The same problem runs in the other direction when you receive events. Webhook senders and message queues with at-least-once delivery will sometimes deliver the same event twice, so a consumer records the IDs of events it has processed and skips a repeat. The record of processed IDs must be written in the same transaction as the effect, or a crash between the two reintroduces the duplicate.
Design operations to be naturally idempotent. The cheapest key is the one you do not need. “Set the shipping address to X” or an upsert keyed on an order ID can be applied any number of times with the same result, while “add 10 to the balance” cannot. Where the business operation allows it, phrase writes as setting a state rather than applying a delta, and reserve idempotency keys for operations that are inherently one-shot, such as moving money or sending an email.
Check yourself
Predict process([["a","1"], ["b","2"], ["b","3"], ["a","4"], ["c","5"]]).
["1", "2", "2", "1", "5"]. a and b each lock in their first value, so the replays at positions 3 and 4 return "2" and "1"; c is new and returns its own value.
A refactor keeps the if key not in store write but appends value instead of store[key]. What does the first example return?
["a", "b", "c", "d"], every request’s own value. The store still holds the right first results, but nothing reads it, so each retry reports a result that never happened. Deduplication has to change the response, not just the bookkeeping.
A client generates a fresh UUID for every attempt, retries included. What does the handler do with a retried charge?
Treats it as a new operation: process([["u1","charge"], ["u2","charge"]]) returns ["charge", "charge"], and both run. The key must identify the logical operation, so it is generated once, before the first attempt, and reused for every retry of it. A client that can restart mid-operation should persist the key with the pending operation, so the retry after a restart reuses it too.
10,000 requests arrive: 1,000 distinct operations, each sent 10 times. How many entries does store hold at the end, and how does the work compare with the brute-force scan?
1,000 entries, since space is O(u) for u distinct keys, not O(n). The map does 10,000 average-O(1) lookups whatever the order. The brute-force scan stops at a key’s first occurrence, so its cost depends on how far back that is: the first copy of each of the 1,000 operations scans the whole prefix without a match, and only all-distinct keys push it to its O(n²) worst case of about n²/2 comparisons.
Reference solution
Reference solution — Python
def process(requests):
store = {}
out = []
for key, value in requests:
if key not in store:
store[key] = value # first write wins
out.append(store[key]) # replays return the first result
return out
print(process([["k1","a"],["k1","b"],["k2","c"],["k1","d"]])) # -> ['a', 'a', 'c', 'a']Reference solution — TypeScript
function process(requests: string[][]): string[] {
const store = new Map<string, string>();
const out: string[] = [];
for (const [key, value] of requests) {
if (!store.has(key)) store.set(key, value); // first write wins
out.push(store.get(key)!); // replays return the first result
}
return out;
}
console.log(process([["k1","a"],["k1","b"],["k2","c"],["k1","d"]])); // -> ["a","a","c","a"]Questions about Idempotency Key Handler
What is an idempotency key?
An idempotency key is a unique value, often a random UUID, that the client generates once per logical operation and sends with every attempt of that operation, usually in an Idempotency-Key HTTP header. The server stores the result of the first attempt under that key. When the same key arrives again, because the client retried after a timeout or a dropped connection, the server returns the stored result instead of performing the operation a second time. The key names the operation, so a retry must reuse it and a genuinely new operation must get a new one.
Why does the first result win instead of the latest one?
Because the first request is the one that actually ran. A retry is a copy of an operation that already happened, sent only because the client never saw the answer. If the payment was charged on the first attempt, the truthful response to every retry is the result of that charge. Letting a later request overwrite the stored result would either run the operation again or report something that never happened. In this challenge that means a later value for a known key is discarded, and every request with that key returns the first value.
What happens when two requests with the same idempotency key arrive at the same time?
With a plain lookup followed by a write, both requests can see that the key is missing and both execute, which is exactly the double charge the key was meant to prevent. A production handler makes the check and the claim one atomic operation, such as an insert that fails if the key already exists, and marks the key as in progress before doing the work. The second request then either waits for the first to finish and returns its result, or receives a conflict error and retries later.
Are PUT and DELETE already idempotent without a key?
By HTTP semantics, yes: GET, PUT and DELETE are among the methods defined as idempotent, meaning that repeating the request has the same effect on the server as sending it once. POST and PATCH are not, which is why idempotency keys are mostly used on POST endpoints that create payments or orders. Idempotent describes the effect, not the response: a second DELETE of the same resource can return 404 even though nothing more was deleted. An API can still accept keys on PUT and DELETE to return identical responses on retries.
Explore the topic
See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.
More Challenges
- Retry with Exponential BackoffTools time out and APIs rate-limit — a production agent retries without hammering. Exponential backoff waits longer after each failure (1s, 2s, 4s, 8s…) up to a cap. Compute the delay schedule: delay[i] = min(cap, base·2ⁱ). Solve it in Python or TypeScript.Read →
- Verify a Webhook SignatureEvery real integration sends signed webhooks — and every FDE has to verify them. Recompute the signature over the payload, compare in constant time, and reject stale events to stop replay attacks. Solve it in Python or TypeScript, with hidden tests.Read →
- CSV → Schema MapperEvery customer hands you a messy export. Before it flows into your system, each row must be coerced to a schema — strings to ints, "true" to booleans — and the rows that don’t fit quarantined, not silently dropped. Solve it in Python or TypeScript, with hidden tests.Read →
- Data Reconciliation DiffThe customer swears the migration worked. You reconcile: compare source records against what landed in the target, keyed by id, and report exactly what was added, removed, or changed. Solve it in Python or TypeScript, with hidden tests.Read →
- PII RedactorBefore customer data touches a log line — or a third-party LLM — the personal bits have to go. Mask emails, SSNs, credit cards, and phone numbers with pattern matching, leaving the rest readable. Solve it in Python or TypeScript, with hidden tests.Read →
- Config ValidatorHalf of "it doesn’t work at the customer" is a bad config — a missing key, a string where an int belongs, an env that isn’t allowed. Validate it up front and fail loudly with a clear list instead of crashing three layers deep. Solve it in Python or TypeScript, with hidden tests.Read →