Inferensys

Glossary

LRU-K Algorithm

The LRU-K algorithm is an advanced cache eviction policy that tracks the times of the last K references to an item to better predict future access patterns than standard LRU.
Stylish WeWork-like workspace with hot desks and document wall, professional searching through enterprise knowledge base on a mounted ultrawide display, warm industrial pendants overhead.
ADVANCED CACHE POLICY

What is the LRU-K Algorithm?

The LRU-K algorithm is an advanced cache eviction policy designed to predict future access patterns more accurately than standard Least Recently Used (LRU) by tracking the times of the last K references to each item.

The LRU-K algorithm is a cache eviction policy that generalizes the standard Least Recently Used (LRU) approach by considering the times of the last K accesses to an item, rather than just the single most recent access. It maintains a history list for all items, recording their last K access timestamps. When the cache is full and a new item must be inserted, the algorithm evicts the item with the largest backward K-distance, which is the time since its K-th-to-last reference. This provides a more nuanced prediction of future use, as it distinguishes between items accessed once recently and those accessed frequently over a longer period.

In practice, LRU-K significantly improves the cache hit ratio for workloads with complex, non-stationary access patterns, such as database buffer pools or agent-side API response caches, where simple recency is a poor predictor. The parameter K (typically 2) controls the depth of historical tracking, balancing prediction accuracy against the metadata overhead. A key operational detail is that items with fewer than K recorded accesses are treated as having an infinite backward K-distance, making them prime candidates for eviction and preventing one-time accesses from polluting the cache.

PERFORMANCE COMPARISON

LRU-K vs. Other Eviction Algorithms

A technical comparison of the LRU-K algorithm against other common cache eviction policies, focusing on their mechanisms, performance characteristics, and suitability for different access patterns.

Algorithm / FeatureLRU-KStandard LRULFUARC (Adaptive Replacement Cache)

Core Eviction Metric

Time of K-th-to-last access

Time of single last access

Total historical access frequency

Dynamic blend of recency (LRU) and frequency (LFU)

Predictive Power for Future Access

High (models access patterns over K references)

Low (only considers most recent access)

Medium (considers long-term popularity)

Very High (self-tuning based on workload)

Resistance to One-Time Scans

High (K > 1 prevents pollution)

Very Low (easily polluted by sequential scans)

High (scans don't accumulate high frequency)

High (dynamically adjusts)

Memory Overhead Per Item

Moderate (stores K access timestamps)

Low (stores single timestamp or position)

Moderate (stores frequency counter)

High (maintains separate LRU and LFU lists)

Implementation Complexity

High

Low

Medium

Very High

Optimal For

Workloads with stable, recurring patterns (e.g., database query caching, agent-side API caching)

Simple, recency-biased workloads

Workloads with stable, highly popular items (e.g., static content CDN)

Variable, unknown, or shifting access patterns

Handles Shifting Access Patterns

Moderate (requires tuning of K)

Poor

Poor (suffers from cache pollution by old popular items)

Excellent (primary design goal)

Typical Hit Ratio Improvement over LRU

15-40%

Baseline

Varies widely; can be worse than LRU on shifting patterns

Can exceed LRU by 20-50% on mixed workloads

LRU-K ALGORITHM

Frequently Asked Questions

The LRU-K algorithm is an advanced cache eviction policy designed to improve upon standard LRU by considering the frequency of recent accesses. These questions address its core mechanics, use cases, and implementation details.

The LRU-K algorithm is a cache eviction policy that tracks the times of the last K references to each cached item to make more informed predictions about future access patterns than a standard Least Recently Used (LRU) policy. It works by maintaining a history list for items not currently in the cache and a main cache for active items. For each access, the algorithm records a timestamp. When the cache is full and a new item needs to be inserted, the algorithm evicts the item with the oldest K-th-to-last access time, effectively penalizing items that have not been accessed frequently in the recent past. This approach better distinguishes between one-hit wonders (items accessed once) and genuinely popular items, leading to a higher cache hit ratio.

Key Mechanism:

  • History List: Stores access history for items evicted from the main cache.
  • K-th Access Time: The core metric for eviction decisions.
  • Parameter K: Typically set to 2, balancing recency and frequency.
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.