Try
↑ ↓ move↵ openesc close

Interview prep / Caching

Senior · Caching · System design interview question

Explain how TinyLFU decides whether a new key may enter a full cache, and what the sketch is for.

Sketch your own answer first, then compare it with the one below.

A strong answer

TinyLFU is an admission policy. When a missed key arrives at a full cache, the cache identifies a victim, the key its eviction policy would remove. TinyLFU admits the newcomer only if its estimated access frequency is higher than the victim's. Otherwise the newcomer is dropped and the victim stays.

Frequencies come from a count-min sketch: a small table of counters indexed by several hash functions. An estimate is the minimum over the counters for a key. It can overestimate because of collisions but never underestimate, and it uses a small fixed amount of memory no matter how many keys exist. That is what makes it practical to track the frequency of keys that are not in the cache, which exact counting could not afford.

To keep frequencies fresh, the counters are periodically halved. This aging lets new popularity overtake old and addresses the staleness of plain LFU.

Typical deployments pair the admission filter with an LRU-style main area, often segmented, and sometimes a small window in front so that a burst of new items gets a fair chance.

Sources

Learn it by running it

The caching and eviction lesson explains the mechanism behind this question, and its lab lets you change the parameters and watch the effect.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms