Load/Bearing
Fundamentals · 03 of 22

Caching

Keep the answers to popular questions in memory so the database only sees the questions nobody asked recently.

Most systems are read-heavy, and most reads ask for the same small set of things: the front page, a popular product, a celebrity's profile. A cache keeps those answers somewhere faster than the database, usually in memory, so a repeat question costs about a millisecond and puts no load on the database.

Where caches live

Caching happens at every layer, from the user's device to the disk:

APPserverCACHE~1 ms · hitDB~10 ms · miss
Cache-aside: the app asks the cache first and only queries the database on a miss, then stores the result.

Read and write patterns

PatternHow it worksGood for
Cache-aside (lazy loading)The app reads the cache. On a miss it reads the DB and writes the result into the cache.The default. Only caches what's actually requested.
Read-throughThe cache library loads from the DB itself on a miss.Same as cache-aside, with less app code.
Write-throughEvery write goes to the cache and the DB together.Data that's read right after it's written.
Write-behind (write-back)Writes go to the cache and are flushed to the DB later.Very high write rates, if you accept losing data when the cache dies.
Write-aroundWrites go straight to the DB. The cache fills on the next read.Data written once and rarely read.

Hit rate is everything

If h is the hit rate, the share of requests reaching the database is 1 − h. That makes the last few percent worth far more than they look:

hit rate 80% → DB sees 20% of reads hit rate 90% → DB sees 10% (half the load of 80%) hit rate 99% → DB sees 1% (a tenth of the load of 90%) average latency = h × t_cache + (1 − h) × t_db

Hit rate rises with cache size, longer TTLs and skewed popularity. Real workloads usually follow a power law, so a cache holding the top 1% of keys can serve most of the traffic.

Eviction

Memory runs out, so something has to go. LRU (least recently used) is the common default. LFU (least frequently used) protects items that are always popular from a burst of one-off reads. A TTL on each key bounds how stale data can get, even if you forget to invalidate it.

Invalidation, the hard part

A cache is a copy, and copies go stale. Common strategies:

Failure modes to plan for

Finding and fixing hot keys

Detect them by looking for skew, not totals:

Fix them by spreading or absorbing the load:

In the game: each level sets a hit rate (85% in Database Meltdown). Adding the cache multiplies the reads reaching your database by 1 − hit rate. One cache costs less than one read replica and removes far more load, which is why it's the cheaper way through that level. In Thundering Herd, the hot keys expire twice mid-rush; request coalescing, TTL jitter or serving stale each stop the stampede for free.

Interview checklist

Practise it

Level 4: Thundering Herd

Put this into practice against live traffic. Your progress in the game is saved in your browser.

Play level 4
Workshop drill

Do it by hand

Interactive drills for the parts the simulator can't show. Scored, with your best result saved in your browser.

Cache write policies

Further reading