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
Coding & Algorithms
Software Engineer
PayPal
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

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
idempotency
LRU cache
TTL
state machines
payments APIs
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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...


Submit Your Answer
Markdown supported