Caching Interview Questions
Frequently asked caching interview questions on patterns, invalidation, eviction policies, Redis and CDNs, with clear answers.
Caching interview questions
Caching comes up in backend, system design and coding interviews alike. Expect conceptual questions plus "design a cache" or "implement LRU".
1. What is a cache hit ratio and why does it matter?
Hits divided by total lookups. A higher ratio means fewer requests reach the slow source, lowering latency and database load. Monitoring it tells you whether a cache is actually useful.
2. Explain cache-aside.
The application checks the cache first; on a miss it reads the database and stores the result. On writes it updates the database and deletes the cache key. It's simple, resilient to cache failures and the most common pattern.
3. Write-through vs write-behind?
Write-through writes to the cache and database synchronously — fresh cache, slower writes. Write-behind writes to the cache and flushes to the database later — fastest writes, but data can be lost if the cache fails.
4. Why delete the cache key on update instead of setting the new value?
Concurrent updates can set values out of order and leave stale data cached. Deleting forces the next read to load the latest value from the source of truth.
5. What is a cache stampede and how do you prevent it?
Many requests miss the same expired hot key simultaneously and all hit the database. Prevent it with single-flight locking (one rebuilder), jittered TTLs, background refresh-ahead and serving stale data while revalidating.
6. What is cache penetration?
Repeated requests for keys that don't exist always miss and hit the database. Cache negative results briefly or use a Bloom filter to reject impossible keys.
7. Name common eviction policies.
LRU (least recently used), LFU (least frequently used), FIFO, random, and TTL-based expiry. LRU is the most common default.
8. How would you implement an LRU cache with O(1) operations?
A hash map from key to node plus a doubly linked list ordered by recency. get moves the node to the front; put inserts at the front and evicts from the tail when over capacity.
9. Redis vs Memcached?
Both are in-memory key-value stores. Redis offers rich data types (lists, sets, sorted sets, streams), persistence, replication, Lua scripting and pub/sub. Memcached is a simpler, multi-threaded cache for plain strings. Redis is the more common default today.
10. What does Cache-Control: no-cache mean?
The response may be stored, but caches must revalidate with the server before using it. no-store is what forbids storing it at all.
11. How do ETags work?
The server sends a fingerprint of the response. The client sends it back in If-None-Match; if the content hasn't changed, the server returns 304 Not Modified with no body.
12. How do you cache static assets on a CDN and still deploy updates instantly?
Fingerprint file names with a content hash (app.3f9a1c.js) and cache them for a year as immutable. A new deploy produces new file names, so no purge is needed; keep HTML short-lived.
13. How do you keep a cache consistent with the database?
Combine invalidation on writes (direct or event-driven via change data capture or a message queue) with TTLs as a safety net. For data that must be exact, read from the database or use versioned values.
14. When should you NOT use a cache?
When data must always be exactly current, is rarely re-read, is highly personalised with low reuse, or when the real fix is a missing index or an inefficient query.
15. Local in-process cache vs distributed cache?
Local caches are fastest but not shared between servers and lost on restart. Distributed caches like Redis are shared and consistent across servers at the cost of a network hop. Many systems layer both.
Practice tip
Implement the LRU cache from memory, then add a TTL per entry and a lock for thread safety. Being able to extend your own solution under follow-up questions is what interviewers look for.