Caching strategies, or what goes in, what stays and when it leaves
Updated Nov 8, 2026 · 3 min read
A cache is a small, fast copy of data that lives somewhere slower. Putting one in is easy. The hard parts are deciding how writes reach it, what to throw out when it fills up, and which layer each kind of data belongs in.
Four ways to handle writes
| Pattern | On a write | Trade-off |
|---|---|---|
| Cache-aside | Write the database, then delete the cache entry | Simple and safe. The next read pays for a miss |
| Write-through | Write the cache and the database together | Reads are always fresh. Every write pays twice |
| Write-back | Write the cache now, the database later | Fastest writes. A dead cache node loses what it had not saved |
| Write-around | Write the database only; the cache fills on read | Keeps one-off writes out of the cache |
Eviction, or who leaves when it is full
- LRU (least recently used) evicts whatever nobody has read for the longest time. It follows what is popular right now, which is why it is the default in Redis and most libraries.
- LFU (least frequently used) evicts whatever has been read the fewest times. It protects long-term favourites, but old counts can keep last month’s hit in memory long after interest moved on.
- FIFO evicts the oldest entry, even if it is still being read constantly.
- Random eviction is cheap and surprisingly decent, but it will sometimes throw out a hot key.
How big should it be?
Most traffic is skewed, with a small share of items gets most of the reads. A common rule of thumb is 80/20, where 20% of items take 80% of reads. Size the cache for that hot set, not the whole catalog. A million items at 10 KB each is 10 GB; the hot fifth is 2 GB, which fits comfortably in one cache node.
What a miss really costs
A miss checks the cache first and then goes to the database, so it costs both. With a 1 ms cache and a 10 ms database, the average read is hit rate × 1 + miss rate × 11 ms. At a 95% hit rate that is 1.5 ms; at 80% it is 3 ms. Every point of hit rate counts, and it counts twice, because misses are also what the database has to carry.
Layers, nearest first
| Layer | Where it lives | Good for |
|---|---|---|
| Browser cache | On the reader’s device | Files that never change, like versioned scripts and images |
| CDN | Servers near the reader | Anything that is the same for everyone |
| In-process cache | Inside each app server | Tiny, very hot data like config |
| Shared cache (Redis) | One network hop from the app | Per-item data every server needs |
| Database | The source of truth | Everything, durably |
Each layer answers what it can and passes the rest inward. To decide where data belongs, ask who it is the same for. If it is the same for everyone, it can go to the edge. If it is different per user, it has to stay behind your servers.
Keeping it fresh
- A TTL caps how stale an entry can get, even if an invalidation is missed.
- Deleting on write makes the next read rebuild the entry from the database.
- Jittered TTLs stop keys loaded together from expiring together.
- Request coalescing lets one request rebuild a hot key while the others wait, instead of all of them hitting the database at once.