Coalesce Concurrent Cache Misses Into a Single Pricing Backend Call

Last updated: November 29, 2026

Quick Overview

When a popular product's price expires from cache, hundreds of request threads miss at once and stampede the pricing service. Implement a single-flight helper so only one thread loads a key while the others wait for its result or its error.

Amazon
Software Engineering Fundamentals
Software Engineer
Amazon
November 29, 2026
Software Engineer
Onsite
Software Engineering Fundamentals
Hard

0

0

0 solved


When a popular product's price expires from cache, hundreds of request threads miss at once and stampede the pricing service. Implement a single-flight helper so only one thread loads a key while the others wait for its result or its error.

During Prime Day a few ASINs take a huge share of traffic, so one expiring cache entry can turn into a thundering herd against a downstream service. The interviewer is looking for a clean leader and follower protocol, correct error propagation, and cleanup so a failure does not poison the key.

What the Interviewer Expects
  • Elects exactly one leader per key using a short critical section
  • Runs the slow backend call outside the lock
  • Propagates the leader's exception to every follower
  • Removes the in-flight entry in finally so the next miss retries
  • Discusses complementary techniques such as early probabilistic refresh and stale-while-revalidate
Key Topics to Cover
thundering herd
single flight
cache stampede
events
error propagation
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 add a timeout so followers do not wait forever on a hung leader?
  • How would you extend this to coalesce across a fleet of hosts?
  • What is stale-while-revalidate and when would you prefer it?
  • How do you stop every key from expiring at the same second?
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
  • PriceCache.get(asin) returns a cached price or loads it from the backend.
  • For N concurrent misses on one key, the backend is called once.
  • If the load fails, all waiters see the failure, and a ...
High-Level Design

A map from key to an in-flight _Call object holds an Event, a value, and an error. Under a short lock, the first thread for a key creates the call and becomes leader; others grab the existing call...


Submit Your Answer
Markdown supported