liveBook · Manning

Eviction policy

As explained in Advanced Algorithms and Data Structures

An eviction policy is a rule for selecting an element to remove from a full cache when space is needed for a new entry.

An eviction policy decides which entry to remove from a full cache when a new entry must be stored. It turns a fixed capacity into a rule for retaining or purging data. section 7.2.1 section 7.9

Start with the cache’s contract: it has a maximum size, supports lookup and insertion, always accepts new entries, and overwrites an existing value when keys match. The policy determines which existing entry yields space. section 7.2.1

Common policy choices use different priorities:

  • LRU removes the least recently used entry, treating recency as the retention metric. section 7.4
  • LFU prioritizes access frequency; new entries start with a counter of 1, and hits increase their priority. section 7.5.2
  • Custom policies can rank entries by business importance, data age, or a time-weighted access metric. section 7.5.4

The policy shapes the supporting data structure and performance. An LRU can maintain order with constant-time operations, while a list-based LFU may require linear-time updates when many entries share a frequency. A priority queue, such as a heap, keeps dynamically changing priorities more efficiently. section 7.5.2 section 7.4

LFU insertion and eviction

When an LFU cache is full, evictOneEntry removes the priority queue’s next node and deletes its key from the hash table. A new entry then enters with priority based on frequency 1 and insertion time. This separates constant-time key lookup from the structure that orders candidates for eviction.

Abstract data structure: Cache

API

class Cache {
  init(maxSize);
  get(key);
  set(key, value);
  getSize();
}

Contract with client

A cache stores a certain number of entries (the maxSize argument for the constructor, in the API), always allows you to set new entries, and retains elements based on the concrete cache’s implementation of the eviction policy.a

When set is called, if an entry with the same key already exists on the cache, the new value will overwrite the old one.

Use the cache API and contract to trace how capacity, insertion, lookup, overwriting, and eviction interact. section 7.2.1

How Advanced Algorithms and Data Structures covers it

How deep it goes
Defines the rule, compares LRU and LFU, and traces priority-queue implementation trade-offs without covering concurrency design in detail.
Examples
Uses least-recently-used removal, frequency counters, timestamps, priority queues, heaps, and custom age- or importance-weighted metrics.
What it assumes
Assumes familiarity with cache operations, hash tables, linked lists, and priority queues while defining eviction from the cache contract.
What you can do after
Build a cache policy by selecting a retention metric and matching it with lookup and ordering data structures.
How it connects
Connects cache design with LRU cache, LFU cache, hash tables, priority queues, and cache concurrency.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Advanced Algorithms and Data Structures. The section links point to the parts of the book it draws on.