Design a Multi-Level Cache
🗄️ What is a multi-level cache? It is a stack of caches in front of a slow data source. The top level (L1) is tiny and very fast, like a map inside your own process. The next level (L2) is bigger and slower, like a shared cache server. Below them sits the database, which is slow but has everything. A read tries the fast levels first and only falls through to the database when it has to.
You will hear this problem under several names: "design an L1/L2 cache", "design a cache with write-through and write-back", or "design a distributed cache library". They are one design. The tiers and the write policies are the core of this post. The distributed library is the same cache with its L2 spread over many machines, and we build it in the Extensibility section with consistent hashing and quorum reads and writes.
This is a hard problem because the classes are few but the rules interact. A read changes several levels at once, an eviction can trigger a database write, and a sloppy write leaves an old value hiding in a lower level. Interviewers use it to see whether you can keep a small system correct while every operation touches more than one place.
Requirements
"Design a cache with several levels in front of a slow database. Reads should be fast, and it should support different write policies."
Short prompt, many hidden decisions. Each question below settles one of them.
Clarifying Questions
You: How many levels are there, and how big is each one?
Interviewer: Make it configurable. The caller passes a list of capacities, one per level, smallest first. Count capacity in keys, not bytes.
You: When a read finds a key in L2 but not in L1, what happens?
Interviewer: Return it, and copy it into L1 so the next read is fast. In general, copy it into every level above the one that had it. If no level has it, read the database and fill every level.
You: Which write policies do we need?
Interviewer: Write-through and write-back. The caller picks one when it creates the cache.
You: Where does a write go? Into every level, or only into L1?
Interviewer: Your call, but a read must never return an old value after a newer write.
You: How does each level evict when it is full?
Interviewer: Least recently used, per level. Each level keeps its own order.
You: With write-back, a value can sit in the cache before the database has it. Is it OK to lose it if the process crashes?
Interviewer: Yes, that is the deal you make with write-back. But give callers a flush() that pushes everything pending to the database.
You: Do several threads use the cache at once?
Interviewer: Start with one thread. We will come back to it.
The fourth answer is the one to slow down on. "Your call" means the interviewer wants to see you reason about it. We will compare three answers in the Implementation section, and the one we pick shapes the rest of the design.
💡 Interview tip: When the interviewer says "your call", name the options out loud and pick one with a reason. Silence followed by code looks like you did not notice there was a choice.
Final Requirements
- The cache has one or more levels with given capacities (in keys). Level 0 is L1, the fastest.
- Every level evicts its least recently used key when it holds more keys than its capacity.
- A read checks the levels from the top. If level
ihas the key, the value is copied into levels0toi - 1. If no level has it, the value comes from the backing store and is copied into every level. If the store does not have it either, the read returns nothing. - A write never leaves an older value where a later read could find it.
- The write policy is chosen at construction: write-through (the store is updated during the write) or write-back (the store is updated later).
- With write-back, a value the store does not have yet must reach the store when it is evicted, or when the caller calls
flush(). - For testing, callers can ask which level holds a key and what the store holds.
Out of scope
- Expiry times (TTL) and size limits in bytes (we sketch TTL in Extensibility).
- Caching "not found" answers.
- Several threads (Extensibility).
- Spreading a level over many machines (Extensibility).