Caching Strategies and Eviction
medium 25 minPlace fast memory between applications and persistent storage to slash read latency and protect databases.
When it comes up
The moments this idea belongs in a design interview, and the moments it does not.
Signals
When to bring it up
| You hear | Say | Because |
|---|---|---|
| Read-heavy, 100 to 1 | Cache-aside with a TTL | Most reads repeat |
| The database is at its limit for reads | Count repeated reads first, then cache them | A cache only helps with repeats |
| Users everywhere, public pages | A CDN in front | Near copies cut the distance |
| Must be exact | Read the database for that decision | A copy can be old |
| Millions of writes | A cache for reads will not help | Talk about batching or partitioning |
Options
Compare your moves
Product pages are slow and the database is busy. What do you do?
BreaksCache everything forever
- How
- Every query result is stored with no expiry.
- Limit
- Old prices stay, memory fills, and nobody knows which keys to delete.
- Choose when
- Only for data that truly never changes.
WorksCache pages for five minutes
- How
- A TTL and nothing else.
- Limit
- A price can be five minutes old, and hot pages expire for everyone at once.
- Choose when
- The business accepts five minutes.
StrongFix the query, then cache-aside with delete and TTL
- How
- Add the missing index, cache product rows, delete on change, keep a TTL, one refresher for hot keys.
- Limit
- More moving parts, and a rare stale set remains until the TTL.
- Choose when
- Reads repeat and a short delay is acceptable.