Eviction Policies

A cache is a small space pretending to be a big one, and the pretense only works if it's clever about what to keep. When a bounded cache fills up, every new item forces out an old one — and which one you evict determines your hit rate, which determines whether the cache is worth having at all. Eviction policies are the algorithms that make this choice, and understanding them (especially the workhorse, LRU) is essential to building caches that actually stay effective.

Because caches are bounded (from the fundamentals post), a full cache must evict something to make room for new data — and the eviction policy deciding what to evict directly determines the hit rate. This post covers why eviction is necessary, the main policies (LRU, LFU, FIFO, and others), TTL-based expiration, and how to choose. Eviction is where a bounded cache decides what’s worth keeping, making it central to cache effectiveness.

Why eviction is necessary

Eviction — removing items from the cache to make room — is necessary because caches are bounded, and the policy for what to evict matters enormously:

Eviction is necessary because bounded caches must remove items to make room, and the eviction policy (what to evict) directly determines hit rate — so a good policy keeps likely-to-be-reused data and evicts unlikely-to-be-reused data, approximating the impossible ideal (evict what won’t be needed longest) using past access as a predictor. The main policies embody different heuristics.

The main eviction policies

Several standard eviction policies use different heuristics for what to evict — the main ones every engineer should know:

LRU (evict least-recently-used — the common, effective default exploiting temporal locality), LFU (evict least-frequently-used — keeps popular data, slower to adapt), FIFO (evict oldest — simple but ignores access, usually worse), and Random (simple, low-overhead) are the main eviction policies. LRU is the general-purpose workhorse. Alongside these access-based policies, time-based expiration (TTL) is also common.

TTL: time-based expiration

Separate from (and often combined with) eviction policies is TTL (Time To Live) — expiring cached items after a set time, regardless of access:

TTL (time-based expiration — items expire after a set duration) manages freshness (bounding staleness), complementing access-based eviction policies (which manage space) — with the TTL length trading freshness against hit rate. TTL is a simple, widely-used tool, often combined with eviction policies. Choosing among all these depends on the workload.

Choosing an eviction policy

Selecting the right eviction (and expiration) approach depends on the workload — some practical guidance:

Eviction policies — LRU (the effective default, exploiting temporal locality), LFU (keeps popular data), FIFO/Random (simple), plus TTL (time-based freshness expiration) — decide what a bounded cache keeps, directly determining hit rate. Choose LRU by default, match to the access pattern, combine eviction (space) with TTL (freshness), and mind the overhead. Next: cache invalidation — the genuinely hard problem of keeping cached data consistent.

Key takeaways

Further reading

Sources & References

LRU, LFU, FIFO and more