Course map

Topic 086 / Core

Cache eviction policies: LRU, LFU

Chapter 6.1 · Caching Strategies

What and why

A cache has finite memory; once full, an eviction policy decides which entry to remove to make room for a new one — a capacity decision, independent of invalidation (→ 85), which removes an entry because it is known stale, not because of memory pressure. LRU (least recently used) evicts whatever hasn't been touched in the longest time; LFU (least frequently used) evicts whatever has the lowest access count. Builds on: → 2 (data structures: hash maps, trees, tries, heaps, queues — the hash map + linked list pairing).

Diagram

Access sequence A, B, C, A, D at capacity 3:

LRU (recency-ordered)              LFU (count-based)
after A:  [A]                      counts: A:1
after B:  [B,A]                    counts: A:1, B:1
after C:  [C,B,A]                  counts: A:1, B:1, C:1  (cache full)
after A (hit, moves to front):     counts: A:2, B:1, C:1
          [A,C,B]
after D (miss, evict B —           D arrives, miss, capacity full: evict the
          least recently used):    lowest count — B and C tie at 1; tie-break
          [D,A,C]                  by recency evicts B: {A:2, C:1, D:1}

How it works

  1. LRU keeps entries ordered by recency of access; a hit moves the entry to the "most-recent" end, and eviction removes the "least-recent" end — built from a hash map (O(1) lookup) plus a doubly linked list (O(1) move-to-front), the exact structure traced in → 2.
  2. LFU keeps a count per entry; eviction removes the lowest count, with ties typically broken by recency.
  3. LRU favors recency: good whenever "used recently" predicts "will be used again soon" — most real workloads.
  4. LFU favors stable popularity: good when a few keys stay hot regardless of a recent quiet spell, but costs more to maintain (counts, decay) and adapts more slowly to new hot keys.
  5. Both are approximations of an unachievable optimal policy (evict whatever won't be needed soonest, which requires knowing the future); the choice is which approximation error is acceptable.

Practical example

(illustrative). On the 5-step trace above, LRU evicts B when D arrives — B hasn't been touched since step 2, while C was touched at step 3 and A at step 4. LFU, on the same trace, evicts B or C (both at count 1) depending on the tie-break — LRU and LFU disagree on this exact sequence, which is the point: the right policy depends on whether recency or frequency better predicts what's needed next.

When to use it

  • Use LRU as the default: simplest, cheapest, matches most real access patterns.
  • Use LFU when: measurement shows a stable popularity skew that recency-based eviction keeps discarding — verify, never assume.
  • Avoid building custom LFU before confirming, by measurement, that LRU's hit ratio is actually the problem.
  • Failure scenarios: a single large scan (a batch job reading every row once) poisoning an LRU cache by evicting every genuinely hot key for one-time reads ("cache pollution," mitigated with scan-resistant variants or a separate pool); LFU counts that never decay, so an item popular once stays "frequent" long after it stopped being read.
  • Simpler alternative: no eviction at all — size the cache to hold the whole hot set, if memory allows; eviction only matters once the cache is smaller than its working set.

Practice

(mechanism kind — trace by hand, then verify; 20 min, lab: Redis).

  1. By hand, trace LRU on A, B, C, A, D, E at capacity 3; write the cache contents after each step.
  2. Configure Redis with maxmemory and maxmemory-policy allkeys-lru on a small dataset; fill past capacity and confirm the evicted keys match your by-hand trace.
  3. Repeat with allkeys-lfu and a skewed access pattern (one key read 10× more than the rest); compare which keys survive eviction against the LRU run.

Done when your hand-traced eviction order matches Redis's observed eviction order under the same access sequence.

Shared lab setup

Key takeaway

Eviction answers "what goes when the cache is full," independent of staleness; LRU is the safe default, LFU trades bookkeeping cost for resistance to recency bias. Done when you can: trace LRU eviction by hand on any access sequence and state when LFU would evict differently. Used next in → 87 (Redis, where these policies are configured).

References

None — this topic makes no external claims.

Source directory

Search the guide