Skip to the workspace
Pebellslearn by building
Probabilistic & System-Scale Structures  ›  Lesson

LRU, LFU and What Actually Hits

Cache Eviction 26 minA doubly-linked list plus a hash map gives O(1) LRU; LFU evicts by frequency instead, and neither policy is automatically better

dsa / LRU, LFU and What Actually Hits

Explore each scene at your own pace.

01 · LRU can lose to a policy that doesn't even look

LRU can lose to a policy that doesn't even look

LRU, LFU and What Actually Hits

Definition
A cache eviction policy decides which stored item to discard when the cache is full. LRU removes the least recently used item and LFU removes the least frequently used one. A linked list plus a hash map make LRU run in constant time.
In plain words
A cache is a small desk drawer for things you reach for often. When it is full, LRU throws out whatever you have not touched for longest, while LFU throws out whatever you have used the fewest times.

In short: an LRU (least-recently-used) cache keeps a doubly-linked list in recency order, plus a hash map from key to its node, so every operation is O(1): a hit splices its node to the front (MRU end), a miss evicts the tail (LRU end) and inserts the new key at the front. LFU (least-frequently-used) evicts by hit count instead of recency, breaking ties by recency. Neither is free: LRU can be worse than plain FIFO on some access patterns, because promoting a hit to the front pushes some other key closer to eviction — a key that FIFO, which never reorders on a hit, would have kept.

A doubly-linked chain: hot at one end, doomed at the other — a hit splices its node to the front in 3 pointer rewires, a miss evicts the tail

step through it
1class Node:
2 def __init__(self, key=None, value=None):
3 self.key, self.value = key, value
4 self.prev = self.next = None
5 
6def order_after_each_touch(capacity, keys):
7 head, tail = Node(), Node()
8 head.next, tail.prev = tail, head
9 nodes = {}
10 
11 def remove(n):
12 n.prev.next, n.next.prev = n.next, n.prev
13 
14 def insert_front(n):
15 n.next, n.prev = head.next, head
16 head.next.prev, head.next = n, n
17 
18 for k in keys:
19 if k in nodes:
20 remove(nodes[k])
21 insert_front(nodes[k])
22 else:
23 if len(nodes) >= capacity:
24 victim = tail.prev
25 remove(victim)
26 del nodes[victim.key]
27 n = Node(k, k)
28 nodes[k] = n
29 insert_front(n)
30 cur, seen = head.next, []
31 while cur is not tail:
32 seen.append(cur.key)
33 cur = cur.next
34 print(f"touch {k}: {seen}")
35 
36order_after_each_touch(2, [2, 3, 2, 1, 3])

LRU can lose to a policy that doesn't even look

Quarry compares two eviction policies on the exact same trace: LRU, and plain FIFO (evict whatever arrived first — no reordering on a hit at all):

python

FIFO gets 2 hits; LRU gets only 1. Trace it: after 2, 3, both policies hold {2, 3}. The third touch, 2, is a hit for both — but LRU promotes 2 to the front, which makes 3 the new least-recently-used. When 1 arrives next (a miss for both), LRU evicts 3 — the exact key 1's successor, 3 again, needs a step later. FIFO never reordered on the hit, so it evicts 2 (the oldest insertion) instead, and 3 survives to be a hit. Reacting to a hit is not automatically the safer choice.

Scene 1 of 5
Project context and further resources
You're building a piece ofQuarry — the storage engine
This piece — lru_vs_fifo(): Measures LRU against FIFO on the same access trace before picking the read cache's policy.
Scenario Quarry's engineers are debating whether to switch the read cache from FIFO to LRU, and want to see both policies measured on the exact same access trace before deciding.
lru_vs_fifo.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
lru_vs_fifo(capacity, keys) → dictHow many hits LRU and FIFO each get over the same access trace.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.