liveBook · Manning

Eviction

As explained in Acing the Frontend Interview

Cache eviction is the removal of cached entries to enforce capacity limits or other retention policies.

Cache eviction removes entries when a cache exceeds capacity or applies a retention policy. Least Recently Used (LRU) eviction removes the least recently accessed entry so frequently used data remains available. section 6.4

An interview-ready LRU explanation covers:

  • Use a Map for lookup and insertion-order tracking. section 6.4
  • Move accessed or inserted keys to the end as most recently used. section 6.4
  • Evict the first key when the cache exceeds its maximum capacity. section 6.4

LRU cache interaction

With a capacity limit, inserting a new entry can remove a stale entry such as 1 → 1. Accessing key 4 returns 4 and makes it the most recently used entry. section 6.4

Listing 6.6 LRU cache implementation in TypeScript
class LRUCache<K, V> {
  private cache: Map<K, V>;
  private capacity: number;
   constructor(capacity: number) {
    if (capacity <= 0) {
      throw new Error('Capacity must be greater than 0');
    }
    this.capacity = capacity; #A
    this.cache = new Map(); #B
  }
  get(key: K): V | -1 { #C
    if (!this.cache.has(key)) { #D
      return -1;
    }
    const value = this.cache.get(key)!; #E
    this.cache.delete(key);
    this.cache.set(key, value);
    return value;
  }
  put(key: K, value: V): void { #F
    if (this.cache.has(key)) {
      this.cache.delete(key); #G
    }
    this.cache.set(key, value); #H
    if (this.cache.size > this.capacity) {
      const lruKey = this.cache.keys().next().value!; #I
      this.cache.delete(lruKey); #J
    }
  }
  entries(): [K, V][] { #K
    return Array.from(this.cache.entries());
  }
}

Inspect the TypeScript implementation showing capacity validation, Map-based ordering, get and put operations, and least-recently-used deletion. section 6.4

How Acing the Frontend Interview covers it

How deep it goes
Explains LRU behavior, ordering, and constant-time implementation, while deferring broader policy tuning and stampede handling.
Examples
Demonstrates eviction through a TypeScript LRU cache, a React demo, and stale-entry removal during cache operations.
What it assumes
Assumes caching and data-structure fundamentals while emphasizing the algorithmic reasoning behind LRU behavior.
What you can do after
Builds from scratch an LRU cache with constant-time access and insertion using a Map and capacity-based deletion.
How it connects
Connects eviction with caching strategies, in-memory caches, Map, Redis, database query caching, and operating-system page replacement.

Also explained in

Compare how 3 books explain eviction →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Acing the Frontend Interview. The section links point to the parts of the book it draws on.