Inferensys

Glossary

LRU Cache

A Least Recently Used cache eviction policy that discards the data that has not been accessed for the longest period, optimizing for temporal locality of reference.
Data scientist building training data pipeline on laptop, data preprocessing visible, technical workspace.
CACHE EVICTION POLICY

What is an LRU Cache?

A Least Recently Used (LRU) cache is a data structure that automatically discards the least recently accessed items first when its storage capacity is full, optimizing for temporal locality of reference.

An LRU Cache implements a specific cache eviction policy that assumes data accessed recently will likely be accessed again soon. When the cache reaches its predefined memory limit and a new entry must be stored, the algorithm identifies and removes the item with the oldest access timestamp. This mechanism directly optimizes the cache hit ratio by retaining hot data in fast storage layers like RAM, preventing expensive trips to slower disk-based or remote primary data stores.

In high-throughput retrieval pipelines, an LRU cache is a critical tool for latency budgeting, directly reducing P99 latency and tail latency for frequently queried embeddings or documents. Unlike a simple TTL-based eviction, LRU adapts dynamically to shifting access patterns without manual tuning. However, engineers must guard against cache stampede scenarios where a popular entry's expiration coincides with a flood of requests, potentially overwhelming the backend database before the cache is repopulated.

EVICTION POLICY MECHANICS

Key Characteristics of LRU Caching

The Least Recently Used (LRU) algorithm is a foundational cache eviction policy that optimizes for temporal locality. By discarding the least recently accessed item first, it assumes data accessed recently will likely be accessed again soon.

01

Temporal Locality Exploitation

LRU caching is built on the principle of temporal locality of reference. This heuristic assumes that if a data block is accessed once, it is highly probable it will be accessed again in the near future. By evicting the item with the oldest access timestamp, the cache retains 'hot' data that reflects the current working set of the application. This makes it highly effective for workloads with recency bias, such as user session data or trending content feeds.

02

Doubly Linked List + Hash Map Implementation

A canonical high-performance LRU cache achieves O(1) time complexity for both get and put operations by combining two data structures:

  • Hash Map: Provides constant-time key lookup to directly access cache entries.
  • Doubly Linked List: Maintains access order. When an item is accessed, it is detached and moved to the head of the list in O(1) time. The tail of the list always represents the least recently used item, ready for immediate eviction when capacity is exceeded.
03

Cache Hit Ratio Dynamics

The effectiveness of an LRU policy is measured by its cache hit ratio. A high hit ratio indicates the cache is successfully intercepting requests before they reach slower backend storage. LRU excels when the working set size is smaller than the cache capacity. However, a sudden burst of one-time sequential reads—often called a scanning pollution event—can flush the entire cache of frequently used data, causing a sharp drop in hit ratio until the working set is re-established.

04

LRU vs. TTL-Based Eviction

LRU eviction is purely access-frequency and recency-based, contrasting with Time-To-Live (TTL) expiration. TTL enforces an absolute expiry timestamp regardless of access patterns, which is essential for stale data prevention. In practice, high-performance retrieval systems often combine both strategies: LRU manages capacity pressure by evicting cold data, while TTL ensures data freshness by purging entries that have exceeded their valid lifespan, even if they are still frequently accessed.

05

Approximations for High Concurrency

Strict LRU requires locking the entire data structure on every access to move nodes, creating a concurrency bottleneck in multi-threaded environments. To solve this, databases and operating systems often implement approximations like CLOCK (Second Chance). This algorithm uses a circular buffer and a reference bit; a sweeper hand checks entries, clearing the bit if set, and evicting the entry if the bit is already clear. This avoids the overhead of linked-list pointer manipulation under high contention.

EVICTION POLICY COMPARISON

LRU vs. Other Cache Eviction Policies

A technical comparison of the Least Recently Used (LRU) eviction policy against other common algorithms used in retrieval-augmented generation and answer engine caches.

FeatureLRULFUTTLFIFO

Eviction Trigger

Access recency

Access frequency

Absolute time elapsed

Insertion order

Temporal Locality Optimization

Resistant to Scan Pollution

Memory Overhead

O(1) per entry (doubly-linked list + hash map)

O(log N) per entry (priority heap)

O(1) per entry (timer wheel)

O(1) per entry (simple queue)

Handles Bursty Traffic

Requires Tuning

Best Use Case

Session data, recent query cache

Static asset cache, CDN origins

Session tokens, DNS records

Write-through buffers

CACHE EVICTION MECHANICS

Frequently Asked Questions About LRU Caches

Explore the mechanics of the Least Recently Used (LRU) eviction policy, a foundational algorithm for optimizing retrieval latency by exploiting temporal locality. These answers dissect its implementation, trade-offs, and role in high-performance answer engine architectures.

An LRU (Least Recently Used) Cache is a data structure that automatically discards the item that has not been accessed for the longest period when its storage capacity is full. It operates on the principle of temporal locality of reference, which posits that data accessed recently is highly likely to be requested again soon. The mechanism relies on tracking access order: every time an item is read or written, it is marked as the 'most recently used' and moved to the front of the eviction queue. When the cache reaches its predefined limit and a new item must be inserted, the algorithm identifies and evicts the 'least recently used' item at the back of the queue. This policy is distinct from TTL-based expiration, which removes items based on absolute age, or LFU (Least Frequently Used), which tracks access counts. In the context of latency budgeting for retrieval, an LRU cache is critical for maintaining low P99 latency by ensuring that frequently accessed embeddings or documents are served from fast memory rather than slow disk or recomputation.

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.