Build an Idempotency Layer for a Create Payment API
Last updated: November 29, 2026
Quick Overview
Implement a component that wraps a payment-creation handler so that requests repeated with the same merchant-scoped idempotency key return the original response instead of creating a second payment. Reusing a key with a different request body must be rejected, and keys expire after a TTL.
Razorpay
November 29, 20260
0
0 solved
Implement a component that wraps a payment-creation handler so that requests repeated with the same merchant-scoped idempotency key return the original response instead of creating a second payment. Reusing a key with a different request body must be rejected, and keys expire after a TTL.
Merchants integrating Razorpay's APIs retry on network timeouts, and without idempotency a retry can create a duplicate payout or order. Interviewers use this to see whether you think about concurrent duplicates, body fingerprinting and what happens when the handler fails midway.
What the Interviewer Expects
- Scope keys per merchant
- Fingerprint the request body and reject mismatched reuse
- Handle a concurrent duplicate while the first request is in flight
- Release the key if processing raises so the client can retry
- Expire entries with a TTL using an injectable clock for testing
- Explain how this maps to Redis or a DB unique constraint in production
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you implement this across many API pods with Redis, and what race does SET NX solve?
- Should a 5xx response be cached under the key? What about a 4xx validation error?
- What if the process crashes after charging the bank but before storing the response?
- How long should the TTL be for payouts versus payments?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
handle(merchant, key, body, process) returns the handler's (status, body) response. Same key and same body returns the cached response without calling process. Same key with a different body ret...
High-Level Design
A map from (merchant, key) to (fingerprint, response_or_IN_FLIGHT, expires_at). Reserve the key under a lock before calling the handler, so only one caller processes. Fingerprint is SHA-256 of can...