Caching the read path
Serve repeated reads from memory and protect your database from load.
15 min readWhy caches work
Most systems read far more than they write, and reads are skewed: a small set of popular items gets most of the traffic. Keeping those items in memory makes reads around ten times faster and removes most of the load from the database.
Where to cache
Caches can sit at several layers, and they stack.
- CDN at the edge: static files and public, cacheable responses close to users
- In-memory cache (e.g. Redis) next to your services: hot rows, sessions, precomputed results
- Local in-process cache: tiny, ultra-hot data such as configuration
Cache-aside and invalidation
In the cache-aside pattern the service reads from the cache first, falls back to the database on a miss, and writes the result into the cache with a time-to-live. The hard part is staleness: decide how old data may be, and either expire it with a TTL or delete the key when the source changes.
When the cache fails
A cache is a performance layer, so the system should keep working without it, just slower. But if the database was sized assuming a 90% hit rate, losing the cache can send ten times the traffic to it at once. Replicate important caches and size the database to survive a cold cache, or shed load while the cache warms.
Key takeaways
- Cache what is read often and changes rarely
- A cache hit rate of 85% cuts database load by almost seven times
- Plan for a cold or failed cache: the database must survive it
Sign in to track lessons and earn badges.