← All articles

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

PatternOn a writeTrade-off
Cache-asideWrite the database, then delete the cache entrySimple and safe. The next read pays for a miss
Write-throughWrite the cache and the database togetherReads are always fresh. Every write pays twice
Write-backWrite the cache now, the database laterFastest writes. A dead cache node loses what it had not saved
Write-aroundWrite the database only; the cache fills on readKeeps 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.

80% hit rate3.0 ms
90% hit rate2.0 ms
95% hit rate1.5 ms
99% hit rate1.1 ms
Average read time with a 1 ms cache and a 10 ms database.

Layers, nearest first

LayerWhere it livesGood for
Browser cacheOn the reader’s deviceFiles that never change, like versioned scripts and images
CDNServers near the readerAnything that is the same for everyone
In-process cacheInside each app serverTiny, very hot data like config
Shared cache (Redis)One network hop from the appPer-item data every server needs
DatabaseThe source of truthEverything, 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.

missmissmissmissBrowser cacheon the deviceCDNnear the readerApp memoryinside the appRedisone hop awayDatabasethe truth
Each layer answers what it can and passes the rest inward.

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.