Definition · Reference string and replacement policyA reference string is the sequence of keys a cache is asked for. A replacement policy is the rule that chooses which resident key to evict when a miss arrives at a full cache of k slots. Its hit rate on that string is hits divided by references.
The first reference to each key must miss whatever the policy and whatever k: those are the compulsory misses, and on a trace of five distinct keys there are five of them.
- FIFO evicts the key that entered the cache earliest, regardless of use since.
- LRU evicts the key that was used least recently.
- LFU evicts the key with the smallest use count, ties broken by insertion order; a count dies with the entry it belongs to.
- Farthest-in-future evicts the resident key whose next use is furthest ahead, or one that is never used again.
The bound, and whose proof it is
Farthest-in-future — also called Belady’s algorithm, or OPT — minimises misses over every possible policy on a given trace. It is offline: it reads the rest of the request sequence to choose a victim, which no cache can do. So it is not a policy anyone runs; it is a ceiling against which the policies people do run are measured.
That it is optimal is a theorem, proved by an exchange argument, and the proof is not this course’s. It is Algorithms, in “Greedy Algorithms and Matroids”, in the lesson “Optimal Offline Caching” — algorithms/greedy-algorithms-and-matroids/optimal-caching — which also draws the offline/online distinction properly. What this lesson does with the theorem is use it: the bound turns “our hit rate is 1/6” into “the best any policy could have done on this trace is 5/12, and we are getting 1/6”.
Example · Twelve references, five keys, three slotsOn 1 2 3 4 1 2 5 1 2 3 4 5 with k = 3: farthest-in-future gets 5 hits (5/12), FIFO gets 3 (1/4), LRU gets 2 (1/6) and LFU gets 2 (1/6). The gap between the bound and LRU is 1/4 of all references, which is the most that a better online policy could have recovered — and no online policy can recover all of it, because the bound reads the future.
Belady's anomaly: more memory, more misses
Now give the same trace four slots instead of three. LRU improves from 2 hits to 4, LFU from 2 to 4, the optimum from 5 to 6. FIFO goes from 3 hits to 2: its miss count rises from 9 to 10. A larger cache made it worse.
Theorem · Stack algorithms, and what FIFO is notA policy is a stack algorithm if, at every point of every trace, the contents of a k-slot cache are a subset of the contents of a (k+1)-slot cache. For such a policy every hit at k is a hit at k+1, so the miss count is non-increasing in the cache size.
LRU and farthest-in-future are stack algorithms; FIFO is not, because its eviction order depends on insertion times that change when the cache size changes. Having no such guarantee is not the same as always failing — FIFO is well behaved on most traces — but it means no trace is safe.
This is the misconception the lesson exists to kill, and it is a belief people genuinely hold: that adding memory to a cache can be neutral at worst. It cannot, unless you know which policy you are running and that it is a stack algorithm. The lab scans every cache size from 1 to 6 for all four policies on your own trace and reports where any of their miss counts rises, so the anomaly arrives as a measurement rather than as a claim.
Two limits on what a trace can tell you. A hit count on twelve references is a fact about twelve references and not about your system; policies are separated by workloads, and a trace with a hot key ranks them differently from a round robin. And nothing here is about cost: LRU needs bookkeeping that FIFO does not, and whether that is worth two per cent of hit rate is an implementation question this course leaves to Algorithms.