Skip to content
Caching

Lesson 4 of 14 · 22 min

x
4/14

Lesson position in the course — not completion. Use Mark Complete to track finished lessons (saved in this browser).

Eviction Policies: LRU, LFU, FIFO & TTL

Cache memory is finite. When the cache is full and a new entry arrives, something must be removed — that decision is the eviction policy. The right policy depends on your access patterns.

LRU (Least Recently Used) evicts the entry that has not been accessed for the longest time. It works well for most workloads because recently used data tends to be used again — temporal locality. LFU (Least Frequently Used) evicts the entry with the fewest accesses over its lifetime, better when some items are consistently popular. FIFO evicts the oldest inserted entry regardless of usage — simple but often suboptimal. Random eviction picks any entry arbitrarily; surprisingly effective and zero bookkeeping overhead. TTL (time-to-live) expiry removes entries after a fixed duration regardless of usage — essential for data that becomes stale on a schedule.

Before
No eviction — cache grows until OOM
1const cache = new Map();2 3function set(key, value) {4  cache.set(key, value);  // never removes anything5}6// Memory usage grows forever until the process crashes
After
LRU with max capacity
1class LRUCache {2  constructor(capacity) {3    this.capacity = capacity;4    this.cache = new Map(); // Map preserves insertion order5  }6 7  get(key) {8    if (!this.cache.has(key)) return null;9    const val = this.cache.get(key);10    this.cache.delete(key);11    this.cache.set(key, val); // move to end (most recent)12    return val;13  }14 15  set(key, value) {16    if (this.cache.has(key)) this.cache.delete(key);17    else if (this.cache.size >= this.capacity) {18      const oldest = this.cache.keys().next().value;19      this.cache.delete(oldest); // evict LRU20    }21    this.cache.set(key, value);22  }23}

Check your understanding

  • What does LRU evict?Show answer

    Answer

    The entry unused for the longest time — good when temporal locality holds.
  • When might LFU beat LRU?Show answer

    Answer

    When a few keys stay popular over long periods while recent one-offs should not stick.
  • Why combine LRU with TTL?Show answer

    Answer

    Capacity eviction handles memory; TTL bounds staleness even for frequently hit keys.
Previous

Progress is saved in this browser.

Next Lesson