Design an Idempotent Payment API
Design an Idempotent Payment API
MediumBackend0% accepted
A user taps 'Pay' twice (or the network retries) and gets charged twice. Design an idempotent POST /charge endpoint so retries are safe. Cover: the idempotency-key header, how to store and look up keys, what to return on a duplicate, race conditions between two concurrent identical requests, and key expiry.
Approach
Client sends a unique Idempotency-Key. Server stores key→result; on duplicate, return the stored result instead of re-charging. Use a unique constraint + transaction to handle concurrent duplicates.
More Backend problems
Browse all coding problems →Scenarios & Expected Answers
No example scenarios for this problem.
Sign in to continue
Create a free account to view answers and mark progress.
- Free forever — no credit card
- Theory Q&A with detailed answers
- Points, streaks & leaderboards