Skip to content
Tech Intuition
Tech Intuition

Appearance

Theme

Signal · Dark with amber

Background

Slow stars and the odd comet

Connect

Simulation · about 6 min

One stream of requests, four ways to decide what to forget.

Every cache below receives exactly the same requests. Change the traffic and the cache size, and watch which eviction policy keeps the keys that matter.

FIFO

Evicts the oldest entry, however useful it is.

hit ratio
0%
last 300
–
average
–

LRU

Evicts the entry that has gone unused the longest.

hit ratio
0%
last 300
–
average
–

LFU

Evicts the entry that has been used the least often.

hit ratio
0%
last 300
–
average
–

Best possible

reference

Knows the future and evicts what is needed last. A ceiling, not a real policy.

hit ratio
0%
last 300
–
average
–

hot: in the hot set cold: not needed now empty slot

Each square is one cache slot holding one key. The line under each cache is its hit ratio over every 100 requests, so a dip means a scan, a new trend, or a cold start. A hit costs 1 ms; a miss costs what you set.

Traffic

A hot set of 50 keys, interrupted every 1,000 requests by a scan that reads 300 keys once each.

Out of 1,000 different keys. Changing it restarts the replay.

What a trip to the database costs on a miss.

Playback speed

Press Play to replay 12,000 requests through all four caches. They all receive the same stream, so a difference in hit ratio is the policy's doing.

Request 0 of 12,000

What you are looking at in the cache eviction lab

A cache is small, so when it is full and a new key arrives, something has to go. The eviction policy decides what. The four caches here use different rules, but they replay exactly the same requests in the same order, so any difference in hit ratio is the policy's doing.

FIFO evicts the oldest entry. LRU evicts the entry that has gone unused the longest. LFU evicts the entry used least often (its count starts again at 1 when an entry is evicted). Best possible is not a real policy: it reads the whole stream in advance and evicts the key that will be needed furthest in the future (Belady's rule). It shows how much room is left above the real ones.

The hit ratio is the share of requests found in the cache. It matters because a miss costs a trip to the database: with a 1 ms hit and a 20 ms miss, going from 90% to 99% hits cuts the average from 2.9 ms to 1.2 ms. Each square is a cache slot. Amber squares hold keys that matter right now, grey squares hold keys that do not, and the line under each cache is its hit ratio over every 100 requests.

The model is simple on purpose: one cache, entries of equal size, reads only, no expiry times, and every miss inserts the key. The traffic is synthetic, so use it to see why policies differ, not to forecast a real system.

Things to try in the cache eviction lab

  1. Leave Traffic on Scan and press Play. Watch the line under LRU and FIFO drop to zero during each scan and then climb back as they relearn the hot keys, while LFU stays high.
  2. Switch to Trending. Now LFU is in trouble: grey squares pile up in its cache, keys that were popular once and are never asked for again. LRU forgets quickly and wins.
  3. Switch to Loop and set the cache to 50 entries. FIFO, LRU and LFU all score about zero, because every key is evicted just before it comes round again. Drag the size to 60 and watch all of them jump to about 100%.
  4. Switch to Popular few and drag the miss penalty to 100 ms. A few points of hit ratio change the average latency by many milliseconds. Compare each policy with Best possible to see how much a smarter one could still win.
  5. Shrink the cache to 10 entries on any traffic. Every hit ratio falls, and the gaps between policies change. The best policy depends on the traffic and the size.

Why cache eviction matters in a system design interview

Interviewers expect you to choose an eviction policy for a reason. LRU is the strong default because recent use predicts future use for most traffic, and it is cheap to build with a hash map and a linked list. LFU is better when popularity is stable and worse when it shifts, which is why real LFU caches age their counts. Neither is safe from a scan or a loop, so say so: a nightly report that reads a whole table will flush an LRU cache.

Mention what real systems do. Redis lets you pick an approximate LRU or LFU that samples a few keys instead of tracking them all. Operating systems use cheaper approximations of LRU such as CLOCK. Caffeine, a Java cache library, adds an admission filter based on TinyLFU so one-off keys do not push out popular ones, and the 2Q and ARC policies split the cache between recent and frequent entries. The caching lesson covers the patterns around eviction (cache-aside, write-through, invalidation, stampedes and hot keys) and includes a tested C# LRU cache.

Send a message

It goes straight to my inbox. I usually reply within a few days.