Implement Idempotency Keys for a Create Payment Endpoint
Last updated: November 29, 2026
Quick Overview
Write the server-side logic that makes a create payment call safe to retry: the same idempotency key with the same request returns the original result, a reused key with a different body is rejected, and concurrent or unknown-outcome requests never double charge.
Square
November 29, 20260
0
0 solved
Write the server-side logic that makes a create payment call safe to retry: the same idempotency key with the same request returns the original result, a reused key with a different body is rejected, and concurrent or unknown-outcome requests never double charge.
Square's public Payments API requires an idempotency key on create calls, and its docs use the case of a dropped connection after a successful charge to explain why. This round checks whether you understand the real failure modes (timeouts, concurrent retries, mismatched bodies) and can encode them in clean code.
What the Interviewer Expects
- Scope keys per merchant or application
- Fingerprint the request body and reject mismatches
- Handle a concurrent in-flight duplicate
- Never release a key when the processor outcome is unknown
- Store and replay the exact original response
- Give keys a retention window
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
- How would you implement this on a sharded SQL database instead of an in-memory dict?
- Should a card decline be cached and replayed, or retryable with the same key?
- Who resolves records stuck IN_PROGRESS after a timeout, and how?
- What retention period would you choose and why?
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
States per (merchant, key): absent, IN_PROGRESS, DONE. Same key plus same fingerprint replays the stored response. Same key plus different fingerprint is an error. A duplicate while the first is in fl...
High-Level Design
Reserve the key atomically before calling the processor (in SQL, an insert into a table with a unique index on (merchant_id, key)). Call the processor outside the lock. On success store the response. ...