Design an Idempotency Key Store With Leases and TTL
Last updated: November 29, 2026
Quick Overview
Implement an in-memory store for payment API idempotency keys supporting begin (claim a key for a request payload), complete (save the response) and abort, with a lease on in-flight claims, a replay TTL on completed responses, and LRU eviction at a fixed capacity.
PayPal
November 29, 20260
0
0 solved
Implement an in-memory store for payment API idempotency keys supporting begin (claim a key for a request payload), complete (save the response) and abort, with a lease on in-flight claims, a replay TTL on completed responses, and LRU eviction at a fixed capacity.
PayPal's REST APIs accept an idempotency header so a merchant retrying a timed-out capture never charges a buyer twice. The interviewer checks O(1) operations, correct state transitions (new, in flight, done), conflict detection when a key is reused with a different payload, and honest discussion of what eviction does to safety.
What the Interviewer Expects
- Model three states: absent, in flight, completed
- Reject reuse of a key with a different request payload
- Expire in-flight leases so a crashed worker does not block a key forever
- Keep all operations O(1) using an ordered dict or hash map plus linked list
- Explain why eviction of a live key weakens the idempotency guarantee
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 make this work across many API servers?
- Should eviction ever remove an in-flight entry? What would you do instead?
- How do you choose the TTL for completed responses?
- What happens if complete() arrives after the lease expired and another worker took over?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
begin(key, payload_hash, now) returns PROCEED (caller should execute the payment), IN_PROGRESS (another attempt is running), REPLAY with the stored response, or CONFLICT when the key was used with a...
High-Level Design
One OrderedDict maps key to (state, payload_hash, value, expires_at). Order gives LRU in O(1) via move_to_end and popitem(last=False). Expiry is lazy: each access checks expires_at and delet...