Part 6 of 12 · Data Structures

Which cache eviction policy keeps the most hits?

On skewed traffic, LFU and W-TinyLFU kept 49% hits in a cache holding 1% of keys, against LRU's 40%. A one-off scan cut LRU to 26% for a while. When popularity moved to new keys, plain LFU collapsed from 51% to 13% and stayed there; W-TinyLFU recovered to 47%. Measured, with the code.

A closed wooden drawer with a brass handle and keyhole, a picture of what a cache keeps

The verdict, which the experiments below will earn: for general-purpose caches, W-TinyLFU (the policy in Java’s Caffeine; Go’s Ristretto uses its close cousin, TinyLFU admission). It matched LFU’s hit rate on steady traffic, shrugged off a one-off scan that hurt LRU, and recovered quickly when popularity shifted, which plain LFU never did. LRU is the simple, decent default you get from most libraries and from Redis’s approximation of it. Avoid plain LFU without decay: it remembers yesterday’s popular items forever.

The mystery at the heart of every cache is a small act of prophecy: when the cache is full and something must go, which item will nobody want next? You get to guess using only what you’ve seen so far. Eviction is a tiny decision made millions of times, so a smarter guess pays off every single time.

The code is in systems-data-structures (Go, standard library only). make demo reruns every number.

Case file

The case When the cache is full, which item will nobody want next?
Suspects LRU, LFU, W-TinyLFU
Culprit LRU loses to scans, plain LFU never forgets
The fix W-TinyLFU: admit only what’s seen more often, and let counts fade
Evidence Go, standard library only, make demo, seeded Zipf traffic

The three suspects #

  • LRU (least recently used): on a miss, evict the item touched longest ago. One linked list plus a hash map; O(1). It adapts instantly to change, but a burst of one-off keys pushes useful items out.
  • LFU (least frequently used): evict the item with the lowest hit count (ties: least recent). O(1) with a list per count. Great when popularity is stable; terrible when it changes, because old counts never fade.
  • W-TinyLFU: new items enter a small LRU window (1% of the cache). When the window overflows, its oldest item competes with the main cache’s eviction candidate, and only the one seen more often stays. “Seen more often” comes from a tiny count-min sketch (the top-k post explains them) of 4-bit counters that halves itself periodically, so frequency fades. The main cache is a segmented LRU (probation and protected). This design comes from Gil Einziger, Roy Friedman and Ben Manes, and its clever turn is the admission test: instead of only asking what to evict, it asks whether the newcomer deserves a place at all.
Flowchart: New item, Small LRU window (1% of the cache), Oldest window item seen more often than the main cache's victim?, Admit it, evict the victim, Drop the newcomer, Sketch counters halve periodicallyFlowchart: New item, Small LRU window (1% of the cache), Oldest window item seen more often than the main cache's victim?, Admit it, evict the victim, Drop the newcomer, Sketch counters halve periodically
Diagram source
flowchart TD
  A["New item"] --> B["Small LRU window (1% of the cache)"]
  B -->|window overflows| C{"Oldest window item seen more often than the main cache's victim?"}
  C -->|yes| D["Admit it, evict the victim"]
  C -->|no| E["Drop the newcomer"]
  F["Sketch counters halve periodically"] -.-> C

Clue one: 1,000,000 keys, Zipf-distributed traffic (s = 0.9) #

Web traffic usually follows a Zipf-like distribution: a few keys are very popular and there’s a long tail. Each policy was warmed up, then measured over 4 million accesses:

Cache size LRU LFU W-TinyLFU
1,000 (0.1% of keys) 22.3% 32.4% 32.7%
10,000 (1% of keys) 39.5% 48.8% 49.4%
100,000 (10% of keys) 63.7% 69.0% 69.4%

With a small cache, frequency-aware policies hit about 10 percentage points more than LRU. Seen as misses, that’s 51% instead of 61%, or about 16% less load on whatever sits behind the cache. On this evidence alone, LFU and W-TinyLFU look equally guilty of being good. The next two clues split them.

Clue two: after a one-off scan #

Steady traffic is the easy case. Here’s where the policies start to show their character.

A report, a crawler or a backfill reads 50,000 keys once. Cache of 10,000:

Policy Hit rate before Next 5,000 accesses Next 100,000 accesses
LRU 39.4% 26.1% 39.1%
LFU 48.6% 48.9% 48.5%
W-TinyLFU 49.3% 49.6% 49.1%

The scan flushed LRU’s hot set: the next requests missed until it refilled. The other two refused to admit keys seen once, which is exactly what you want from a cache.

Clue three: when popularity shifts #

The same traffic shape, but over new keys (a new product launch, a new news cycle). Cache of 10,000, after a long history of the old popularity:

Policy Before the shift First 200,000 after Next 1,000,000 after
LRU 39.4% 39.2% 39.5%
LFU 51.1% 12.9% 15.5%
W-TinyLFU 49.5% 38.4% 47.1%

Here plain LFU gives itself away. It stayed full of items with huge historical counts that nobody requested any more. W-TinyLFU’s sketch halves its counters every 10 × capacity accesses, so the old favourites lost their advantage within a few hundred thousand requests. That one small trick, letting memory fade, is the reveal: it’s what keeps the policy both frequency-aware and adaptable, at almost no cost.

Which suspect for which case #

Cache What I’d use
In-process application cache (Java, Go, Rust) W-TinyLFU through Caffeine, or a TinyLFU-based library such as Ristretto or Moka; it’s the library default for good reason
Redis as a cache maxmemory-policy allkeys-lfu (Redis’s LFU decays counts over time, unlike the plain LFU here) or allkeys-lru; measure with your own traffic
CDN or reverse proxy Whatever the product offers (usually LRU-like); protect it from scans by not caching one-off responses (Cache-Control: no-store on exports and reports)
Database buffer pool Already handled: PostgreSQL uses a clock-sweep with usage counts, and large sequential scans use a small ring buffer instead of the shared cache, precisely to avoid the scan problem above

The fine print #

  • Hit rate isn’t everything. The cost of a miss varies per key; some caches weight eviction by size or by how expensive the item is to recompute.
  • Your distribution is not mine. Log a sample of real keys and replay them through candidate policies; it takes an afternoon, as this demo shows.
  • TTLs interact with eviction. Items that expire anyway don’t need to be evicted; data that must not be stale needs invalidation, not a better policy. That’s a different mystery, solved in why a cache made our data wrong.

Remember this

  • W-TinyLFU: frequency-aware, scan-proof, and it forgets old favourites.
  • LRU is a decent default, but a one-off scan flushes its hot set.
  • Never plain LFU without decay.

What to do on Monday #

  1. W-TinyLFU for in-process caches, from a maintained library.
  2. LRU when that’s what the platform offers, with scans kept away from the cache.
  3. Never plain LFU without decay.
  4. Replay real traffic before tuning size or policy.

It still pleases me that a few kilobytes of 4-bit counters can make a cache this much wiser.

Measured Nov 2026 with Go 1.27 on a laptop (Intel i7-8550U). Hit rates follow from the algorithms and the seeded traffic, and repeat exactly. Code and full results: systems-data-structures.

Like (sign in first) 0 likes 💬 0

Written by

Vishal Kumar

Associate Director at CapGrid, running product and engineering in a CPTO capacity. 10+ years building distributed, multi-tenant SaaS and AI platforms. AWS Certified Solutions Architect - Professional. Based in Gurugram.

Comments

No comments yet.

Sign in to comment. We'll email you a link; no password needed.