Caching & Content DeliveryIntermediate17 min read3 questions

Caching Strategies and Eviction Policies

The single highest-leverage component in most systems, and the one with the most ways to go subtly wrong. Patterns, eviction, invalidation and the failure modes that cause outages.

Covers: Cache-aside, read-through, write-through, write-back; LRU, LFU, TTL; invalidation; stampedes; hot keys

Caching is usually the largest performance win available: a Redis read is ~0.5 ms against a database read of 5–50 ms, and it removes load from the component that is hardest to scale. It is also where the two hardest problems in computer science live - cache invalidation and naming things - and an interviewer will absolutely push on invalidation.

Filter
0/3 mastered
IntermediatecachingpatternsconsistencyAsked at Meta, Netflix

30-second answer

Cache-aside puts the application in charge: check the cache, on a miss read the database and populate. Read-through hides that behind the cache client. Write-through writes to cache and database together, keeping them consistent at the cost of write latency and caching data nobody reads. Write-back writes only to the cache and flushes later, which is the fastest and the only one that can lose data. Cache-aside is the default for almost every system because it is simple, resilient to cache outages, and only caches what is actually read.

AdvancedcachingreliabilityfailureAsked at Meta, Alibaba

30-second answer

A stampede happens when a hot key expires and thousands of concurrent requests all miss and hit the database at once - prevent it with a lock so one request recomputes while the others wait or serve stale. Penetration is repeated requests for keys that do not exist, so the cache never helps - prevent it by caching negative results and using a Bloom filter. Avalanche is many keys expiring simultaneously, usually because they were populated together with identical TTLs - prevent it by adding random jitter to every TTL.

IntermediateevictionlrulfuAsked at Netflix, Cloudflare

30-second answer

LRU evicts what was used least recently - cheap, and it adapts fast to changing access patterns, but a single large scan can wipe the whole working set. LFU evicts what is used least often - it protects genuinely hot keys from scans, but adapts slowly and needs frequency decay or yesterday's popular items never leave. TTL is not really eviction, it is a freshness bound, and you almost always want it alongside a size-based policy. Size the cache by measuring the working set: plot hit rate against memory and find the knee, because past that point additional memory buys almost nothing.

Showing 3 of 3 questions for caching-strategies-and-eviction-policies.

Check your understanding

4 questions · no sign-up, nothing stored

0/4 answered
Question 1

1.After writing to the database, what should you do to the cache?

Question 2

2.100,000 keys are warmed at deploy with identical 1-hour TTLs. What happens an hour later?

Question 3

3.A nightly analytics job scans a million rows and your morning hit rate collapses. Which policy would have prevented it?

Question 4

4.Which data is the worst candidate for caching?

Hands-on challenge

Build it - this is what you talk about in a deep-dive round.

Add caching to a slow endpoint and prove it

Take a real endpoint backed by a database and add a cache layer that survives all three failure modes in this lesson.

Requirements

  • Implement cache-aside with versioned keys, jittered TTLs and negative caching.
  • Add single-flight or probabilistic early recompute and demonstrate it prevents a stampede under load.
  • Measure hit rate, p50/p99 latency and origin QPS before and after.
  • Plot hit rate against cache size at four sizes and identify the knee.
  • Simulate a total cache outage under load and record what happens to the database.

Stretch goals

  • Add a small in-process L1 cache in front of Redis and measure the additional gain.
  • Implement a Bloom filter for existence checks and measure its effect on penetration.