Implement Idempotency Keys for a Money Transfer Endpoint
Last updated: November 29, 2026
Quick Overview
Implement a server-side idempotency layer for a POST /transfers endpoint so that a mobile client retrying with the same Idempotency-Key gets the original result, a reused key with a different body is rejected, and a concurrent duplicate does not move money twice.
Onepay
November 29, 20260
0
0 solved
Implement a server-side idempotency layer for a POST /transfers endpoint so that a mobile client retrying with the same Idempotency-Key gets the original result, a reused key with a different body is rejected, and a concurrent duplicate does not move money twice.
Users on flaky mobile networks tap Send in the OnePay app and the client retries, so every money-moving API needs idempotency. The interviewer checks that you separate in-flight from completed requests, fingerprint the payload, and know which failures are safe to forget versus which must be remembered.
What the Interviewer Expects
- Store key, request fingerprint, state, and the final response
- Return the stored response on replay instead of re-executing
- Reject key reuse with a different payload
- Block or signal concurrent duplicates while the first is in progress
- Distinguish retryable infrastructure failures from final business outcomes like declines
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- The process crashes after the bank call succeeds but before the response is saved. How do you recover?
- How would you implement this with Postgres instead of an in-memory dict?
- How long should keys live, and what happens after they expire?
- Should the key be scoped per user? What attack does that prevent?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
The client sends an Idempotency-Key header with each transfer. Same key and same body: return the first result without moving money again. Same key and different body: client bug, return 422. Same k...
High-Level Design
A table keyed by (user_id, key) with columns request_hash, state (IN_PROGRESS or DONE), status, response, expires_at. Claim the key atomically before calling the handler, then record the o...