1. This read-heavy service sits on a single database and is getting slow. Where would you add caching, and what exactly would you cache?
Mid-levelcachingcache-asidehit-ratio
What the interviewer is really testing
Whether you decide from the workload (read to write ratio, skew, tolerance for staleness) instead of adding Redis by reflex, and whether you know the different layers a cache can sit at and what each is good for.
Show the model answer, follow-ups and checklist
Model answer
Before placing a cache I want three facts: what is slow or expensive, how skewed the reads are, and how stale each piece of data may be. A cache pays off when the same answer is requested many times between changes.
Then I work from the user inwards, because the best cache is the one closest to the user that can still serve a correct answer.
- Browser and CDN. Static assets and public pages that are the same for everyone can be served at the edge. Those requests never reach us at all.
- Shared application cache. For data that is per-entity but reused across requests, such as a product or a user profile, I would use cache-aside in Redis. The key is the entity id, the value is the assembled object the page needs, and the TTL is minutes with some jitter. On a write, the service updates the database and deletes the key.
- In-process cache. A small set of very hot, rarely changing values can sit in the application’s memory with a TTL of seconds.
- The database itself. It has its own buffer pool, so I would first check that the slow query is not simply missing an index.
Next I would estimate the benefit. If 90% of reads are for 10% of entities and each entity changes a few times a day, a hit ratio of about 90% is realistic from caching the popular tenth alone, and the database is left with about a tenth of the read load. If reads were spread evenly over a huge key space, I would expect a poor hit ratio and would not add the cache.
Finally I would say what I am not caching: anything used to make a decision that must be correct, such as stock at checkout. And I would note that the database must survive a cold or failed cache, so it needs either headroom or protection.
Likely follow-ups
- How would you know the cache is doing its job?
- Track the hit ratio, the eviction rate, cache latency and the database's read load before and after. A healthy cache shows a high and stable hit ratio and a database load that follows the miss ratio. A hit ratio that looks fine while the database is still busy usually means the expensive queries are not the ones being cached.
- When would you add an in-process cache as well as Redis?
- For a small amount of very hot, slowly changing data such as configuration, feature flags or a few hundred popular items. It removes the network hop and the deserialisation. The costs are memory on every instance and copies that disagree between instances, so I would give it a TTL of seconds or broadcast invalidations.
- Would you cache query results or objects?
- Objects keyed by id where I can, because it is obvious which key a write affects. Cached query results, such as a filtered list, are hard to invalidate because many different writes can change them. For those I rely on a short TTL or a version in the key.
- What would you refuse to cache?
- Anything where the decision needs the current value, such as a balance or stock check at the moment of purchase. Also data written more often than it is read, and requests so varied that entries would be evicted before they are reused.
Common mistakes
- Saying "add Redis" without naming the key, the value, the TTL or the expected hit ratio.
- Ignoring the layers in front of the application, when a CDN or browser cache would remove the request entirely.
- Caching data that changes more often than it is read.
- Not saying what happens to the database when the cache is empty or unavailable.
- Treating the cache as a fix for a missing index or an N+1 query pattern.
A strong answer includes
- Asks about read to write ratio, access skew and how stale each kind of data may be
- Walks the layers from browser and CDN through the application cache to the database
- Names the cache key, the cached value and a TTL for at least one concrete item
- Estimates or reasons about the hit ratio and what load remains on the database
- States the read and write pattern (for example cache-aside with delete on write)
- Says what is deliberately not cached and why
Revise: Caching patterns