Design a Loading Cache
🗄️ What is a loading cache? It is a cache that knows how to fill itself. You give it a loader function, such as "read this user from the database". When a key is missing or too old, the cache calls the loader, keeps the result for a while, and serves it from memory until it expires. Callers just ask for a key and never deal with misses themselves.
Guava's LoadingCache and Caffeine in Java, and the "single-flight" helper in Go, are versions of this design. Interviewers reach for it because the plain version is easy, and the concurrent version has one famous trap. When a popular key expires and a hundred threads miss it at the same moment, a naive cache sends a hundred identical queries to the database. That is the thundering herd (or cache stampede), and the fix, one load per key with everyone else waiting for it, is what this problem is really about. Expiry by TTL, and refreshing hot keys before they expire, come along with it.
Requirements
"Design an in-memory cache in front of a slow service. On a miss it should load the value itself. Lots of threads use it, and we must not overload the service."
The last sentence is the important one. The questions below turn it into rules.
Clarifying Questions
You: Where do values come from?
Interviewer: From a loader function you get in the constructor. It takes a key and returns the value. It is slow, tens of milliseconds, and sometimes it throws.
You: How long is a value good for?
Interviewer: A fixed time to live from the moment it was loaded. Pass the current time into each call, so we can test it.
You: If a hundred threads ask for the same missing key at the same time, how many loads should happen?
Interviewer: One. The other ninety-nine wait for it and get the same answer. That is the main thing I want to see.
You: And for different keys?
Interviewer: They are independent. A slow load for one key must not block anybody asking for another key, hit or miss.
You: What should callers see when the loader fails?
Interviewer: Return an empty string to everyone who was waiting on that load. Don't cache the failure: the next request tries again.
You: Is there a size limit, or eviction?
Interviewer: Not for now. Assume the key set fits in memory.
You: Should an expired value be served while it is being reloaded?
Interviewer: Not in the base version. Expired means load again. We can talk about refreshing later.
The third and fourth answers pull in opposite directions. "One load per key" sounds like it needs a lock around the load, and "different keys must not block each other" says that lock cannot be global. Most of the design is about satisfying both.
💡 Interview tip: When an interviewer says "we must not overload the backend", ask directly "what happens when many callers miss the same key at once?". Naming the thundering herd before they do is a strong signal.
Final Requirements
LoadingCache(ttl, loader)creates an empty cache.get(key, timestamp)returns the cached value if it is fresh. Otherwise it callsloader(key), caches the result with load timetimestamp, and returns it.- A value loaded at time
tis fresh whiletimestamp < t + ttl. After that it has expired. - While a load for a key is running, other
getcalls for that key do not start another load. They wait for it and return its result. - Loads and hits for different keys never wait on each other.
- If the loader throws, every caller waiting on that load gets
"", nothing is cached, and the nextgettries again. get_if_present(key, timestamp)returns the fresh value or"", and never loads.invalidate(key)drops the cached value.
Out of scope
- A maximum size and eviction (Extensibility).
- Serving stale values during a background refresh (Extensibility).
- A cache shared between machines. This is one process; see the system design posts for distributed caches.