Learn
Lesson · Caching and Hit Rates · Skew, size and policy

Replacement Policies on a Trace

A replacement policy is judged by the hits it gets on a reference string, not by how sensible its rule sounds. Farthest-in-future is the offline optimum and therefore the bound; the three runnable policies fall short of it by different amounts on different traces. And FIFO can miss more often with a larger cache, which is Belady’s anomaly and is measured here rather than asserted.

trace 1 2 3 4 1 2 5 1 2 3 4 5 12 references, 5 distinct keys hits OPT FIFO LRU LFU k = 3 5/12 1/4 1/6 1/6 k = 4 1/2 1/6 1/3 1/3 FIFO misses 9 at k = 3 and 10 at k = 4 Belady's anomaly OPT = evict the key whose next use is farthest in the future
Four policies, one reference string, two cache sizes

Core ideas

What this lesson establishes

A policy is a rule for choosing a victim. Three of these can be run by a cache; the fourth cannot, and that is what makes it useful.

1

A policy is measured on a trace, not argued about

Give a policy a reference string and a number of slots and it produces a definite number of hits. FIFO evicts the oldest arrival, LRU the least recently used, LFU the least frequently used. On 1 2 3 4 1 2 5 1 2 3 4 5 with three slots those give 1/4, 1/6 and 1/6 — and FIFO, the policy nobody defends, wins.

2

Farthest-in-future is the bound, and it cannot be run

The offline optimum evicts the resident key whose next use is furthest ahead in the trace. It needs the rest of the trace, so no cache can execute it, and that is precisely why it is useful: no online policy beats it, so the gap between it and LRU is the most any policy change could possibly buy. On this trace with three slots it gets 5/12 against LRU’s 1/6.

3

A bigger cache is not a guarantee

LRU and farthest-in-future are stack algorithms: a cache of k+1 slots always contains everything a cache of k slots would, so their miss counts cannot rise when the cache grows. FIFO has no such property, and on this trace it misses 9 times with three slots and 10 times with four.

The material

Four policies, the bound, and the anomaly

What each rule does, who proves the optimum is optimal, and the trace on which more memory costs hits.

Definition · Reference string and replacement policy

A 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 slots

On 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 not

A 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.

Interactive

Replacement policies on a trace

FIFO, LRU, LFU and the offline bound, at two cache sizes

Four policies on one reference stringthree you can run, and one that reads the future
hitmissOPT, the boundBelady's anomaly

Method

Running a policy comparison honestly

Two cache sizes, always. One size tells you which policy won a coin toss.

1

Write the trace and count the distinct keys

The distinct count is the compulsory misses, the floor every policy shares. A comparison that does not subtract them exaggerates how close the policies are: on twelve references with five distinct keys, five of the misses were never anyone’s fault.

2

Run the optimum first

Farthest-in-future gives you the ceiling before you have any attachment to a policy. If the gap between the bound and your current policy is small, no replacement change will help and the answer is a bigger cache or a different workload.

3

Run the runnable policies at two sizes

FIFO, LRU and LFU at k and at k+1. Two sizes, because a single size is one sample and because the second size is where an anomaly becomes visible. Compare hit counts, not hit rates, if the traces differ in length.

4

Check whether any miss count rose

If a policy misses more with more slots, you have found Belady’s anomaly and you have learned something about the policy rather than about the trace. LRU and the optimum cannot do this; FIFO and LFU carry no such guarantee.

Policies, bounds and anomalies

Score 0 / 0

1 2 3 4 1 2 5 1 2 3 4 5 at three slots and at four

Twelve references over five keys. The four-slot run is the one that matters, and it is the one most people would not bother with.

trace: 1 2 3 4 1 2 5 1 2 3 4 5 12 references, 5 distinct keys compulsory misses: 5, for any policy at any size k = 3 slots hits rate misses OPT 5 5/12 7 the bound FIFO 3 1/4 9 LRU 2 1/6 10 LFU 2 1/6 10 k = 4 slots hits rate misses OPT 6 1/2 6 FIFO 2 1/6 10 ← MORE misses than at k = 3 LRU 4 1/3 8 LFU 4 1/3 8 FIFO: 9 misses at k = 3 → 10 misses at k = 4 gap at k = 3: OPT 5/12 − LRU 1/6 = 1/4 of all references

Two results, and they point in different directions. At three slots FIFO beats LRU on this trace, which is a fact about this trace. At four slots FIFO gets worse than it was at three, which is a fact about FIFO: it is not a stack algorithm and has no guarantee to break.

For a faded rehearsal, run A B C A B D A B C D at k = 3 and k = 4. The supplied first move is that the optimum gets 5 hits at three slots; find LRU and FIFO at both sizes, and then say which policy’s ranking changed between the two cache sizes and whether any miss count rose. Report what that trace does and does not show about the anomaly.

The lab replays your own trace through all four policies, steps through the cache contents reference by reference so you can watch a policy make the eviction it is about to regret, and scans every cache size from one to six looking for a rise. It reports which policies it found one for and which it did not.

Common mistakes

1
Believing a bigger cache can never hurtIt can, and the trace 1 2 3 4 1 2 5 1 2 3 4 5 is where: FIFO misses 9 times at three slots and 10 at four. The guarantee people are reaching for is real but belongs to stack algorithms — LRU and farthest-in-future — and FIFO is not one. Before adding memory to a cache, know which policy it runs.
2
Comparing policies on one trace at one sizeFIFO beats LRU here at three slots and loses to it at four. A comparison that reports the first number and stops has measured the trace, not the policies. Two sizes at minimum, and a trace from the system you are actually asking about.
3
Reading the bound as a targetFarthest-in-future is what an oracle would achieve, and the gap between it and your policy is the most any online change could recover — not the amount one will. If the bound is 5/12 and you have 1/6, a better policy might close part of that; nothing closes all of it, because nothing online reads the future.

Check your understanding

✓
Finish when you would not accept a policy comparison run at one cache size.

You should be able to replay a short reference string through FIFO, LRU, LFU and farthest-in-future, count hits at two cache sizes, identify the compulsory misses, quote the bound as a bound, and recognise a rising miss count as Belady’s anomaly rather than as a mistake.

Note: That is the last of the three things which decide a hit rate: the skew, the size and the policy. The rest of the course is about the two things a cache does wrong even when its hit rate is excellent. The first is staleness, and “TTL and Staleness” prices it — along with what a shorter TTL costs in misses, which is the part that is usually left out.

Kept in this browser only, for you. Nothing is sent anywhere and nothing checks it. Carry it to another device.

Feedback and recommendations

Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.

No recommendations yet for this lesson.