Coalesce Concurrent Cache Misses With a Single-Flight Loader
Last updated: November 29, 2026
Quick Overview
Write a thread-safe wrapper around a slow loader so that when many threads miss the cache for the same key at once, only one of them calls the backend and the rest wait for its result. Errors must reach every waiter and must not be cached.
Meta
November 29, 20260
0
0 solved
Write a thread-safe wrapper around a slow loader so that when many threads miss the cache for the same key at once, only one of them calls the backend and the rest wait for its result. Errors must reach every waiter and must not be cached.
Meta's web tier leans on memcache in front of TAO and MySQL, and a popular profile or post going cold can send thousands of identical misses to the database at once (the thundering herd that memcache leases were built to stop). This shows up in the coding or fundamentals portion of a Meta onsite, and the interviewer is checking whether you can reason about who holds which lock, when waiters wake up, and what happens on failure.
What the Interviewer Expects
- Keeps the backend call outside the lock so other keys are not serialized behind one slow load
- Uses a per-key in-flight record so followers block on an event instead of polling
- Propagates the leader's exception to every follower and leaves nothing cached on failure
- Removes the in-flight entry in a finally block so a crash cannot wedge the key forever
- Can explain how this maps to memcache leases or request coalescing at a proxy
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- 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 fall back to their own load if the leader hangs?
- How does this change if the coalescing must work across many web servers instead of threads in one process?
- What happens if an invalidation arrives while a load is in flight, and how do you avoid caching a stale value?
- Would you ever want followers to retry after the leader fails, and how do you avoid a retry storm?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
get(key)returns the cached value, or loads it vialoader(key).- At most one in-flight load per key at any moment; other callers for that key wait.
- Different keys load in parallel.
- If the l...
High-Level Design
Two shared maps behind one mutex: cache (key to value) and inflight (key to a call record holding an Event, a value and an error). Under the lock a caller checks the cache, then the in-flight ma...