Concurrency in LLD · senior
Idempotency: a retry must not charge twice
A timeout does not mean failure. Give every charge an idempotency key stored under a unique constraint, so a retry returns the stored result and never creates a second charge.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Cause of a double charge: the first charge succeeded after our 3-second timeout and the retry created a second charge. A timeout is an unknown result.
- Every charge carries an idempotency key, per order and purpose, created once. A PaymentAttempt record has a unique key and a status: IN_PROGRESS, SUCCEEDED, FAILED, UNKNOWN.
- The first request inserts the record. A repeat returns the stored result. For concurrent duplicates the unique insert (or putIfAbsent) lets exactly one proceed; the others wait or get in progress.
- Unknown outcome: ask the gateway for the status by our reference before any retry. Pass our key or order id to the gateway when it supports idempotency.
- Store a hash of the request with the key. The same key with a different body is rejected.
- Keep keys at least as long as clients can retry.
- Right now: list orders with two successful charges, refund the extras, tell support, and turn off auto-retry on charge until fixed.
- Generate the key once, not on every retry. Checking for an existing payment without a unique constraint has the same race again.
The red pen
Common traps: mistake → fix
Mistake: Just do not retry
Fix: users and networks retry anyway; make the operation safe to repeat.
Mistake: A timeout means the charge failed
Fix: it may have succeeded.
Mistake: A new idempotency key on every retry is fine
Fix: it defeats the purpose.
Easy to hard
Questions interviewers ask, easy to hard
- A retry charged the customer twice. How do you fix it now and in the design?
- What is an idempotency key?
- How do you handle two identical requests at the same instant?
- What do you do when the gateway outcome is unknown?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.