Inferensys

Glossary

Cache Eviction Policy

The algorithm that determines which items to remove from a full cache to make space for new entries, such as LRU or TTL-based expiration.
Knowledge engineer constructing knowledge base on laptop, document hierarchy visible, casual office setup.
CACHE MANAGEMENT

What is Cache Eviction Policy?

The deterministic algorithm governing which stored objects are purged from a saturated cache to accommodate new data, directly impacting hit ratios and retrieval latency.

A cache eviction policy is the algorithmic logic that selects which entry to remove when a cache reaches its maximum allocated capacity and a new item must be inserted. The policy's objective is to maximize the cache hit ratio by retaining data most likely to be requested again. Common strategies include LRU (Least Recently Used), which discards the stalest access, and TTL (Time-To-Live) expiration, which removes entries based on absolute age rather than access frequency.

The choice of policy critically shapes P99 tail latency in retrieval pipelines. An optimal policy exploits temporal locality, keeping hot embeddings or query results in fast memory while evicting cold data. In semantic caching systems, eviction may also consider the computational cost of regeneration; a complex summarization result might be retained longer than a cheaply computed embedding, prioritizing the prevention of expensive recomputation over simple data volume.

CACHE EVICTION POLICY

Core Eviction Strategies

The algorithmic logic that governs which data is purged when a cache reaches capacity. Selecting the right eviction policy directly impacts cache hit ratio, tail latency, and the overall effectiveness of your retrieval pipeline.

01

Least Recently Used (LRU)

The foundational temporal eviction algorithm that discards the item with the oldest access timestamp first. LRU operates on the principle of temporal locality: data accessed recently is highly likely to be accessed again soon.

  • Mechanism: Maintains a doubly-linked list and hash map. Every GET or SET moves the item to the head. Eviction removes from the tail.
  • Complexity: O(1) for both access and eviction.
  • Best For: General-purpose workloads with strong recency bias, such as user session data or recent document queries.
  • Weakness: Vulnerable to cache pollution from a full scan of infrequently accessed data, which flushes out the genuinely hot working set.
O(1)
Access/Eviction Time
02

Time-to-Live (TTL) Expiration

A time-bound eviction policy where each cache entry is assigned an absolute or sliding expiration timestamp upon insertion. The entry is automatically invalidated and removed once its lifespan elapses, regardless of access frequency.

  • Absolute TTL: Expires at a fixed wall-clock time (e.g., '2025-01-01 00:00:00 UTC'). Ideal for daily financial snapshots.
  • Sliding TTL: Resets the timer on every access. Ideal for user sessions where idle timeout is the concern.
  • Best For: Data with known staleness windows, such as embedding caches for semi-static documents or API rate-limit counters.
  • Weakness: Does not react to memory pressure. A sudden write spike can fill the cache with unexpired entries, causing an out-of-memory error before the timer fires.
Deterministic
Invalidation Trigger
03

Least Frequently Used (LFU)

An access-count-based policy that evicts the item with the lowest hit frequency. LFU tracks a counter for every key and removes the statistically least popular item when capacity is reached.

  • Mechanism: Often implemented with a min-heap or a frequency list. A GET increments the counter and re-orders the item.
  • Best For: Stable, long-lived datasets where historical popularity is a strong predictor of future value, such as a semantic cache storing frequently asked enterprise questions.
  • Weakness: Cache pollution from historical data. An item that was extremely popular last week but is now irrelevant retains a high count and refuses to be evicted, occupying space that new, relevant data needs. Modern variants like Window-TinyLFU add recency weighting to solve this.
O(log n)
Typical Eviction Cost
04

First-In, First-Out (FIFO)

The simplest eviction policy, operating as a standard queue. The first item written into the cache is the first item evicted, regardless of how many times it has been accessed since insertion.

  • Mechanism: A simple queue data structure. No re-ordering on access.
  • Complexity: O(1) for all operations. Extremely low CPU overhead.
  • Best For: Write-heavy, transient data streams where no single item is more valuable than another, such as a KV-Cache for a continuous batching inference server where sequences are processed in strict generation order.
  • Weakness: Completely ignores access patterns. A frequently accessed 'hot' item will be evicted simply because it was the first to arrive, drastically reducing hit ratio for read-heavy workloads.
Minimal
Computational Overhead
05

Random Replacement (RR)

A probabilistic eviction strategy that selects a random candidate for removal when the cache is full. Despite its simplicity, it performs surprisingly well in specific adversarial scenarios.

  • Mechanism: A random number generator selects the victim index. No metadata tracking is required.
  • Best For: Extremely high-throughput systems where the overhead of tracking access order or frequency is prohibitive, or as a baseline comparison for more complex policies.
  • Advantage: Immune to cache stampede pathologies caused by synchronized expiry timers. It naturally distributes eviction pressure.
  • Weakness: Non-deterministic performance. It can occasionally evict the single most valuable item in the cache, causing a temporary latency spike for that specific query.
O(1)
Eviction Complexity
06

Segmented LRU (SLRU)

A hybrid policy that splits the cache into a probationary segment and a protected segment. New entries enter probation; if accessed again, they are promoted to the protected segment. Eviction occurs from the probationary end.

  • Mechanism: Two LRU lists. A hit in probation promotes the item to the protected segment's head. When the protected segment is full, its tail item is demoted back to probation.
  • Best For: Workloads susceptible to cache pollution from sequential scans. A single linear scan of a database table will cycle through the probationary segment without flushing the protected 'hot' set.
  • Real-World Use: Forms the basis of many high-performance in-memory databases and OS page replacement algorithms. It provides a strong balance between recency and frequency without the historical baggage of pure LFU.
High
Scan Resistance
CACHE EVICTION POLICY

Frequently Asked Questions

A cache eviction policy is the algorithmic logic that determines which data to purge when a cache reaches its capacity limit. Understanding these policies is critical for infrastructure engineers optimizing retrieval latency and maintaining high cache hit ratios in answer engine architectures.

A cache eviction policy is a deterministic algorithm that selects which entries to remove from a full cache to accommodate new data. When a cache—such as Redis, Memcached, or an in-memory hash table—reaches its allocated memory threshold, the eviction policy triggers to free space. The policy evaluates all cached entries against a specific criterion: recency of access, frequency of use, explicit time-to-live (TTL) expiration, or a cost function. The selected entry is then invalidated and removed. The choice of policy directly impacts the cache hit ratio, which is the percentage of requests served from cache rather than the slower backend. Common implementations include LRU (Least Recently Used), LFU (Least Frequently Used), FIFO (First-In, First-Out), and TTL-based expiration. In distributed answer engines, eviction policies must also account for cache coherence across nodes to prevent serving stale data.

CACHE REPLACEMENT STRATEGIES

Comparison of Common Eviction Policies

A technical comparison of the primary algorithms used to determine which entries to discard when a cache reaches capacity, evaluated against key operational metrics for retrieval pipelines.

FeatureLRU (Least Recently Used)LFU (Least Frequently Used)TTL (Time-to-Live)FIFO (First-In, First-Out)

Core Mechanism

Tracks last access timestamp; evicts the stalest entry

Tracks access frequency counter; evicts the least popular entry

Assigns an absolute expiration timestamp per entry; evicts on expiry

Maintains a queue ordered by insertion time; evicts the oldest entry

Optimal Workload

Temporal locality (recently accessed items likely re-accessed)

Popularity-based workloads with stable hot-item sets

Data with known, deterministic freshness requirements

Streaming or batch-processing workloads with no re-access patterns

Memory Overhead

Low (doubly-linked list + hash map)

Medium (heap or sorted structure for frequency counts)

Low (per-entry timestamp only)

Minimal (single queue pointer per entry)

Resistance to Cache Pollution

Moderate (a single scan can flush working set)

High (requires sustained frequency to remain; protects hot items)

High (pollution limited to entry's lifespan)

Low (scan-resistant; sequential reads flush entire cache)

Implementation Complexity

Low

Medium

Low

Very Low

Adapts to Access Pattern Shifts

Suitable for Semantic Cache

Typical P99 Latency Impact

< 1 ms (in-memory)

< 2 ms (heap maintenance)

< 0.5 ms (passive expiry check)

< 0.5 ms (pointer shift)

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.