Data5 min read10 interview questions
Caching: keep answers close
Why storing a copy of an answer makes things fast, and the one big risk.
In one sentenceA cache keeps the result of slow work nearby so the next request can reuse it.
Think of it like thisIt is writing a phone number on a sticky note instead of searching the phone book each time.
The big picture
Tap any step to see where to use it, how to use it and an example result.
Where caches live
In the browser, on a CDN, inside your app's memory, or in a fast store such as Redis. The closer to the user, the faster, but the harder it is to update everyone's copy.
The hard part: stale data
A cached copy can become out of date. You choose how long copies live (time to live), or you delete them when the original changes. Phil Karlton joked that naming things and cache invalidation are the two hard problems in computing.
What to cache
Things that are slow to produce and read far more often than they change: popular pages, results of expensive queries, data from other services. Do not cache private data in a place others share.
Common mistakes
- Caching without an expiry time.
- Forgetting to clear the copy when data changes.
- Caching personal data in a shared cache.
Quick recap
- A cache stores answers close by
- A hit is fast, a miss does the slow work
- Choose how long a copy lives
- Update or remove copies when data changes
Try it yourself
Add a simple dictionary cache to a function that calls a slow web API, and print whether each call was a hit or a miss.
Top 10 interview questions
Ordered from basic to advanced. Try answering out loud first, then open the answer.
What is a cache?Basic
A fast, temporary store of results that are slow to produce, so repeated requests can be answered quickly.
What are a cache hit and a cache miss?Basic
A hit means the data was found in the cache. A miss means it was not, so the system does the slow work and usually stores the result.
Where can caching happen?Basic
In the browser, at a CDN, in the application's memory, in a store like Redis or Memcached, and inside the database itself.
What is TTL?Basic
Time to live: how long an entry may stay in the cache before it expires and must be fetched again.
What is cache invalidation and why is it hard?Intermediate
Removing or updating cached data when the original changes. It is hard because many copies may exist in many places, and getting it wrong shows users stale data.
What are cache-aside, write-through and write-back strategies?Intermediate
Cache-aside: the app checks the cache and fills it on a miss. Write-through: writes go to the cache and database together. Write-back: writes go to the cache first and reach the database later, which is faster but riskier.
What eviction policies are common?Intermediate
LRU removes the least recently used item, LFU the least frequently used, and FIFO the oldest. They decide what to drop when the cache is full.
What is a cache stampede?Intermediate
When a popular entry expires and many requests all miss at once and hit the database together. Fixes include locks so one request refreshes it, staggered expiry and refreshing before expiry.
How do you decide what to cache?Advanced
Cache data that is read often, expensive to produce and tolerant of being a little stale. Avoid caching data that must always be exact, or that changes on almost every request. Measure the hit rate.
What problems can caching hide or cause?Advanced
Stale or inconsistent data, memory pressure, cold-start slowness after a restart, and hiding a slow query that should be fixed at the source. Also leaking one user's data to another if the cache key is wrong.
Guides are simplified on purpose. Tool names and details change, so check the official documentation before relying on a detail.