LRU EVICTION
Discard whoever you haven't talked to in the longest time
A 4-slot LRU cache holds hot keys A and B. A scan reads S1 to S6 once each, then the app asks for A. What happens?
An LRU cache keeps the keys you used most recently. A hash map finds a key; a linked list ordered by recency moves it to the front on every hit and drops the back when the cache is full. That works because recent keys tend to be reused. A scan breaks the assumption: each key is used once, yet each lands at the front and pushes a hot key toward the back. Segmented LRU makes a key earn its place. New keys wait in a small probation segment, and only a second hit moves them to the protected one.
LRU: hash map + doubly linked list (head = MRU, tail = LRU)
get(k): if k in map: move node to head; HIT O(1)
else: insert at head; if full, drop tail; MISS
Scan pollution: one pass over N keys you will never read again
pushes N entries through the head. Once N > capacity - |hot| the scan
starts evicting hot keys; with N >= capacity every hot key is gone.
Plain LRU cannot tell "used
once, just now" from "used constantly, until just now".
Segmented LRU (SLRU): same slots, two LRU lists
PROBATION new keys enter here; eviction happens only here
PROTECTED a hit in PROBATION promotes the key here
overflow demotes PROTECTED's tail to PROBATION's head
A key must be seen twice to be protected, so a scan of one-off
keys only churns PROBATION. The protection holds while the hot set
fits in PROTECTED; a hot key demoted to PROBATION can still be
evicted by one-off keys.
Relatives: 2Q and LRU-K (admission after K uses), ARC (adapts the
split), TinyLFU / W-TinyLFU in Caffeine (frequency-based admission),
midpoint insertion in the InnoDB and Linux page caches. LRU WALK is the textbook case and keeps the invariant. In SCAN POLLUTION the invariant breaks at S3, when a key used once evicts A. SEGMENTED LRU uses the same 4 slots and the same requests, and A and B both hit afterwards.
A and B are hot. Then a report scans six keys once each. Raise CAPACITY until plain LRU still has A and B after the scan. Note the number, then compare it with the SEGMENTED LRU scenario at the default of 4.
GOAL: no hot key is evicted by the scan and GET A, GET B hit afterwards
The starter is plain LRU and loses the hot keys to a scan. Rewrite access as segmented LRU. Lists are MRU-first. A miss inserts into probation and evicts its tail when it exceeds probCap. A hit in probation promotes the key to the head of protected; if that overflows protCap, demote the tail of protected to the head of probation. A hit in protected moves the key to its head. Return true on a hit.
The InnoDB buffer pool and the Linux page cache insert new pages at a midpoint (an old sublist or an inactive list) so one table scan cannot flush the hot set. Caffeine (Java) uses W-TinyLFU, which admits a key only if its estimated frequency beats the victim's. Memcached's segmented LRU has hot, warm and cold queues.
“Implement an LRU cache” (hash map plus doubly linked list, O(1) both ways), often followed by “what workload makes it bad?” Name scan pollution and a fix: SLRU or 2Q (admission after a second hit), LRU-K, or frequency-based admission such as TinyLFU.