Inferensys

Glossary

Least Recently Used (LRU)

Least Recently Used (LRU) is a cache eviction algorithm that removes the item that has not been accessed for the longest period of time when the cache reaches its capacity limit.
Strategy consultant facilitating AI use case discovery workshop, sticky notes on glass wall, casual corporate meeting.
CACHE EVICTION POLICY

What is Least Recently Used (LRU)?

A foundational algorithm for managing finite memory in computing systems, from CPU caches to agentic memory stores.

Least Recently Used (LRU) is a cache eviction policy that, when a cache is full and a new entry must be added, removes the item that has not been accessed for the longest period of time. It operates on the principle of temporal locality, assuming that data accessed recently is likely to be needed again soon. Implementation typically uses a combination of a hash map for O(1) lookups and a doubly linked list to track access order, moving items to the front (most recent) on each access and evicting from the back (least recent).

In agentic memory and context management, LRU is critical for managing finite context windows and vector cache capacity. It ensures the most relevant recent interactions or retrieved knowledge remain immediately accessible, while older, less-used data is evicted. This policy directly combats performance issues like thrashing. Common variations include LRU-K, which considers the K-th-to-last access time for better resistance to scan patterns. Its efficiency makes it a default choice in many database buffer pools and in-memory key-value stores like Redis.

CACHE EVICTION ALGORITHM

Key Characteristics of LRU

Least Recently Used (LRU) is a foundational cache eviction policy that prioritizes recency of access. Its design and implementation have direct implications for the performance of agentic memory systems, database buffers, and CPU caches.

01

Core Eviction Principle

LRU operates on a simple, temporal heuristic: the item that has not been accessed for the longest time is evicted first. This assumes that recently used data is more likely to be needed again in the near future, a principle known as temporal locality. When the cache reaches capacity, the algorithm identifies and removes the 'coldest' entry to make space for a new item.

  • Example: In a chat agent's conversation history cache, the opening message of a long session would be evicted before the user's most recent query.
02

Implementation Data Structures

Efficient LRU requires data structures that support fast access, update, and ordering. The canonical implementation combines a hash map (or dictionary) for O(1) key lookups with a doubly linked list to maintain access order.

  • Hash Map: Maps keys to nodes in the linked list.
  • Doubly Linked List: The head represents the Most Recently Used (MRU) item. The tail represents the Least Recently Used (LRU) item, ready for eviction.
  • On Access: A retrieved item is moved to the head of the list (an O(1) operation).
  • On Insertion: A new item is added to the head. If at capacity, the tail node is evicted.
03

Performance Profile & Complexity

A properly implemented LRU cache aims for O(1) time complexity for both get (read) and put (insert/update) operations. This efficiency is critical for high-throughput systems like API gateways or vector database query caches.

  • Advantage: Excellent performance under stable, locality-heavy workloads.
  • Overhead: Requires maintaining two data structures, leading to higher memory overhead per entry compared to simpler policies like FIFO.
  • Latency: The constant-time operations ensure predictable performance, which is essential for real-time agentic systems.
04

Weakness: Scan-Resistant Workloads

LRU performs poorly under scan or one-time access patterns. If a workload involves sequentially reading a large dataset that exceeds cache size, LRU will evict the entire existing 'working set' to accommodate the scan, causing a cache pollution effect.

  • Example: An agent performing a full-table database scan for analytics will flush the cache of frequently accessed user profiles.
  • Contrast with LFU: The Least Frequently Used (LFU) algorithm is more resilient to scans, as one-time scan entries have a low frequency count and are evicted quickly.
05

Variants and Adaptations

Several algorithms extend or modify classic LRU to address its limitations:

  • LRU-K: Tracks the times of the last K references to an item, evicting based on the K-th-to-last access time. This better distinguishes between frequently and infrequently accessed items.
  • 2Q (Two Queues): Uses two queues: an A1 queue (FIFO) for items seen once and an Am queue (LRU) for items seen multiple times. This effectively filters out one-time scans.
  • Adaptive Replacement Cache (ARC): Dynamically balances between LRU and LFU by maintaining both recent and frequent entry lists, adapting to the workload.
06

Application in Agentic Systems

In autonomous agents, LRU is a key policy for managing finite context windows and episodic memory buffers.

  • Context Window Management: An LLM-powered agent with a limited token context can use LRU to retain the most recent user instructions and system outputs, eviding older interactions to stay within limits.
  • Tool Call Result Cache: Results from expensive API calls (e.g., database queries, weather APIs) can be cached with an LRU policy, ensuring the most recently fetched data is readily available for subsequent reasoning steps.
  • Session State: For multi-turn conversations, LRU can manage which pieces of historical state are kept in fast-access memory versus persisted to slower vector stores.
IMPLEMENTATION

How LRU Works: Implementation Mechanism

The Least Recently Used (LRU) algorithm is implemented by tracking access order to efficiently identify the item that has gone unused the longest when eviction is required.

The canonical implementation uses a doubly linked list and a hash map (dictionary). The list orders items by recency, with the most recently used (MRU) at the head and the LRU at the tail. The hash map provides O(1) access from a cache key to the corresponding node in the list. On a cache hit, the accessed node is detached and moved to the head. On a cache miss requiring insertion, a new node is created at the head.

When the cache reaches capacity, the eviction process removes the node at the tail (the LRU item) and deletes its entry from the hash map. This combination ensures all core operations—access, insertion, and eviction—execute in constant O(1) time. Alternative implementations for high-concurrency environments may use approximations like an LRU clock or segmented lists to reduce locking overhead while maintaining the core eviction heuristic.

LEAST RECENTLY USED (LRU)

Frequently Asked Questions

A core algorithm for managing finite memory in computing systems. These FAQs address its mechanics, trade-offs, and practical applications in agentic systems and modern infrastructure.

Least Recently Used (LRU) is a cache eviction policy that removes the item that has not been accessed for the longest period when the cache reaches its capacity limit. It operates on the principle of temporal locality, assuming that recently used data is likely to be used again soon.

How it works:

  • The cache is typically implemented with a hash map for O(1) lookups and a doubly linked list to track access order.
  • On a cache hit (data found), the accessed item is moved to the front (most-recent) position of the list.
  • On a cache miss (data not found), a new item is fetched and placed at the front.
  • When the cache is full and a new item must be inserted, the algorithm evicts the item at the tail (least-recent) end of the list.

This simple, ordered list ensures the eviction decision is deterministic and based solely on recency of access.

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.