Idempotent request handling with idempotency keys
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Microsoft's coding round, last asked Feb 2026. It tests idempotency, apis and concurrency.
Clients retry requests when they time out, so the server may receive the same request more than once. Implement a request handler that uses a client-supplied idempotency key: the first request with a key does the work; any retry with the same key and body gets the original result back without doing the work again. Keep it in memory, safe under concurrent calls, with stored keys expiring after a time to live.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
IdempotentHandler(process: Callable[[dict], tuple[int, dict]], ttl_seconds: int)—processdoes the real work and returns(status, body). -
handle(key: str, body: dict, now: float) -> tuple[int, dict]:- New key: run
process(body), store the result with a fingerprint of the body, return it. - Known key, same body, finished: return the stored result;
processis not called. - Known key, different body: return
(422, {"error": "key_reused"}). - Known key still being processed by another call: return
(409, {"error": "in_progress"}), or wait for it (state which). -
processraises: store nothing final, so a retry can run it again. - Keys older than
ttl_secondsare forgotten.
- New key: run
Example
h = IdempotentHandler(charge, ttl_seconds=3600)
h.handle("k1", {"amount": 50}, now=0) -> (201, {"charge_id": "c1"}) # charge runs
h.handle("k1", {"amount": 50}, now=5) -> (201, {"charge_id": "c1"}) # replayed
h.handle("k1", {"amount": 70}, now=6) -> (422, {"error": "key_reused"})
h.handle("k1", {"amount": 50}, now=4000) -> (201, {"charge_id": "c2"}) # key expired, runs again
Constraints
- Many threads call
handleat once, often with the same key. - Up to 10^6 live keys.
Clarifying questions
- Should a concurrent duplicate wait for the first call's result, or fail fast with 409?
- Should failures be cached (a 400 is a final answer) or released (a 500 may succeed on retry)?
- Is the key scoped per client or global?
- What happens if the process crashes midway after a side effect?
What a strong answer covers
- A record per key: state (in progress or done), body fingerprint (hash of a canonical form), result, created time.
- Atomic claim of a new key: insert-if-absent under a lock (or striped locks), so two simultaneous requests cannot both run
process. - Waiting duplicates via a per-key event or future, with a timeout, if you choose to wait.
- Failure semantics: which errors become the stored answer and which release the key; side effects made idempotent downstream when a crash happens midway.
- Expiry by a sweep or a heap of creation times, and bounded memory.
- Tests: sequential replay, concurrent duplicates (exactly one
processcall), body mismatch, failure then retry, expiry.
Follow-ups
- Move the store to Redis or a database shared by many servers. What replaces the lock?
- The server crashes after charging but before saving the result. How do you avoid charging twice?
- Clients forget to send keys. Can the server derive one safely?
- How long should keys be kept, and what does that cost?
Where it's asked
- Microsoft · Coding · Last asked Feb 202660 min · Ten to fifteen minutes of behavioral questions, then coding in HackerRank where you write a main and make the tests pass; usually three rounds · No AI unless the interviewer invites it; some 2026 loops add a round where you bring your own AI agent
What it tests
idempotency · apis · concurrency
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
