Implement an Idempotency Key Store Safe Under Concurrent Retries

Last updated: November 29, 2026

Quick Overview

Build an in-process idempotency layer for a create-charge endpoint: the first request with a given key runs the handler, and any retry with the same key (including one arriving while the first is still running) gets the same response instead of charging twice.

Stripe
Software Engineering Fundamentals
Software Engineer
Stripe
November 29, 2026
Software Engineer
Onsite
Software Engineering Fundamentals
Medium

0

0

0 solved


Build an in-process idempotency layer for a create-charge endpoint: the first request with a given key runs the handler, and any retry with the same key (including one arriving while the first is still running) gets the same response instead of charging twice.

Stripe's API lets clients send an Idempotency-Key header so network retries never double-charge, and the integration and pair-programming rounds often turn this into a hands-on concurrency exercise. The interviewer is checking whether you notice the in-flight race, scope keys per account, and reject a reused key with different parameters.

What the Interviewer Expects
  • Scopes keys per account so two merchants can reuse the same key string
  • Handles a retry that arrives while the original request is still executing without running the handler twice
  • Detects key reuse with different request parameters and returns an error
  • Holds the global lock only for bookkeeping, never while the handler runs
  • Decides deliberately what happens to the key when the handler fails
  • Explains how the design maps to a shared store such as Postgres or Redis across many API servers
Key Topics to Cover
idempotency keys
race conditions
locks and events
request fingerprinting
exactly-once semantics
How to Approach This
  1. Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
  2. Choose data structures based on access patterns, not familiarity.
  3. Prefer immutable data and message passing over shared mutable state for concurrency.
  4. Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
  • How would you make this work across 500 API servers with a database instead of a dict?
  • What should happen if the server crashes after the charge succeeds but before the response is stored?
  • Should a 500 from the card network be cached under the key or should the client be allowed to retry?
  • How do you expire keys without letting a late retry slip through as a new request?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice System Design Problems
Sample Answer
Requirements
  • execute(account, key, params, handler) runs handler at most once per (account, key) inside a TTL (Stripe uses 24 hours).
  • Concurrent duplicates block until the first finishes, then get its re...
High-Level Design

A dict maps (account, key) to an entry holding a parameter fingerprint, a threading.Event, and the stored response or error. A single lock guards the dict, and only the thread that creates the e...


Submit Your Answer
Markdown supported

Related Questions