LRU Cache

Caching

Least Recently Used eviction policy. When the cache is full, the item that hasn't been accessed for the longest time gets evicted first. Simple, effective, and the default eviction strategy for most CDN caching layers.

Updated Mar 9, 2026

Full Explanation

Every cache has finite storage. When it fills up, something has to go. LRU keeps track of when each item was last accessed and evicts the stalest one. The logic is straightforward: if nobody has asked for it recently, it's probably not important.

LRU works well for CDN traffic patterns where a small percentage of content (the "head") gets most of the requests. Popular content stays in cache because it's constantly being accessed, while long-tail content naturally ages out. This matches the Zipf distribution that most web traffic follows.

The main weakness of LRU is scan resistance. If a crawler or bot requests thousands of unique URLs once each, it can flush the entire cache of popular content. Some CDN implementations use LRU variants like SLRU (Segmented LRU) or W-TinyLFU that combine recency with frequency to resist this problem.

Try the interactive Cache Hierarchy animation in the course to see how LRU eviction works across multiple cache tiers.

Examples

Varnish cache size and LRU behavior:

# Start Varnish with 2GB cache (LRU is default eviction)
varnishd -a :80 -s malloc,2G

# Monitor evictions (nuke = LRU eviction)
varnishstat -f MAIN.n_lru_nuked

# If nuked count climbs fast, your cache is too small
watch -n 1 'varnishstat -1 | grep lru_nuked'

Nginx proxy cache with size limit:

# 10GB cache zone, LRU eviction when full
proxy_cache_path /var/cache/nginx
    levels=1:2
    keys_zone=cdn_cache:10m
    max_size=10g
    inactive=24h
    use_temp_path=off;

# inactive=24h removes items not accessed in 24h
# max_size=10g triggers LRU eviction at 10GB

Video Explanation

Frequently Asked Questions

Least Recently Used eviction policy. When the cache is full, the item that hasn't been accessed for the longest time gets evicted first. Simple, effective, and the default eviction strategy for most CDN caching layers.

Varnish cache size and LRU behavior:

# Start Varnish with 2GB cache (LRU is default eviction)
varnishd -a :80 -s malloc,2G

# Monitor evictions (nuke = LRU eviction)
varnishstat -f MAIN.n_lru_nuked

# If nuked count climbs fast, your cache is too small
watch -n 1 'varnishstat -1 | grep lru_nuked'

Nginx proxy cache with size limit:

# 10GB cache zone, LRU eviction when full
proxy_cache_path /var/cache/nginx
    levels=1:2
    keys_zone=cdn_cache:10m
    max_size=10g
    inactive=24h
    use_temp_path=off;

# inactive=24h removes items not accessed in 24h
# max_size=10g triggers LRU eviction at 10GB

Related CDN concepts include:

  • Cache Key — A unique identifier generated from request attributes that determines whether requests can share a cached …
  • Cache Miss Types — Cold miss: First ever request (unavoidable). Capacity miss: Evicted due to full cache. Invalidation miss: …