Skip to content

What is LRU Cache

Key idea:

LRU (Least Recently Used) Cache — eviction policy that removes least-recently-accessed items on overflow. Classic implementation: HashMap + doubly linked list for O(1) get/put. Redis default: `maxmemory-policy allkeys-lru`. Alternatives: LFU (Least Frequently Used), FIFO, random. LRU is universally good for web caching (recent content is hot).

Below: details, example, related terms, FAQ.

Check your site →

Details

  • O(1) operations: get + put
  • Structure: HashMap (key → node) + DoublyLinkedList (insertion order)
  • On get: move node to head
  • On put: add at head; if capacity exceeded → remove tail
  • Variants: LFU (frequency), TinyLFU (Google, Caffeine Java cache), W-TinyLFU

Example

// Python @lru_cache decorator
from functools import lru_cache
@lru_cache(maxsize=128)
def expensive(n): return n * n

// Redis LRU policy
maxmemory 1gb
maxmemory-policy allkeys-lru

Related Terms

Understanding LRU Cache Eviction Policy

The LRU (Least Recently Used) Cache is an effective eviction policy utilized in various caching mechanisms. The primary purpose of this policy is to optimize the use of limited memory resources by removing the least recently accessed items when the cache reaches its maximum capacity.

When implementing an LRU Cache, the algorithm maintains a record of the order in which items are accessed. This is typically achieved through a combination of a HashMap and a doubly linked list. The HashMap stores the keys and a reference to the corresponding nodes in the linked list, while the linked list maintains the order of access. When an item is accessed, it is moved to the front of the list. If the cache reaches its limit, the item at the end of the list (the least recently used) is removed.

LRU caching is particularly beneficial for applications that exhibit temporal locality, where recently accessed data is likely to be accessed again soon. This makes LRU cache an ideal choice for scenarios such as web applications, database queries, and file system caches.

In summary, the LRU Cache eviction policy is a strategic method for managing memory efficiently by prioritizing access patterns. Implementing this policy can lead to improved performance and resource utilization in systems that require rapid access to frequently used data.

Practical Implementation of LRU Cache in Redis

Redis, a popular in-memory data structure store, supports LRU caching through its configuration settings. To implement an LRU Cache in Redis, you can adjust the maxmemory-policy setting to allkeys-lru or volatile-lru, depending on your needs. Here’s how you can do it:

  • Set Max Memory: Before enabling LRU eviction, you need to define the maximum memory limit for Redis. This can be done using the following command:
  • CONFIG SET maxmemory 100mb
  • Enable LRU Eviction Policy: To set the eviction policy to LRU, execute:
  • CONFIG SET maxmemory-policy allkeys-lru
  • Testing LRU Behavior: You can test the LRU functionality by adding and accessing keys. For example:
  • SET key1 value1 SET key2 value2 GET key1 SET key3 value3 SET key4 value4 SET key5 value5 GET key2 GET key1
  • This sequence of commands demonstrates key access. When the memory limit is reached, Redis will evict the least recently used key based on the access pattern.

By configuring Redis to use an LRU eviction policy, you can effectively manage memory and ensure that the most relevant data remains accessible, optimizing performance in high-demand scenarios.

Comparing LRU Cache with Other Caching Strategies

While LRU (Least Recently Used) is a widely adopted caching strategy, it is essential to compare it with other caching strategies to understand its advantages and limitations. Below are some common alternatives:

  • LFU (Least Frequently Used): This strategy evicts items that are accessed the least number of times. LFU is advantageous in scenarios where certain data is consistently accessed more frequently over time, but it may suffer from 'cache thrashing' if access patterns fluctuate.
  • FIFO (First In, First Out): FIFO evicts the oldest items in the cache without considering their access frequency. This can lead to suboptimal performance if older items are still relevant. FIFO is simple to implement but may not effectively utilize memory in dynamic access patterns.
  • Random Replacement: This strategy randomly selects an item to evict when the cache is full. While it may work well in some scenarios, it lacks the efficiency of LRU in maintaining frequently accessed items.

In conclusion, while LRU is a robust caching strategy for many applications, understanding the context and characteristics of your data access patterns is crucial in choosing the right caching mechanism. Each strategy has its trade-offs, and selecting the most suitable one can lead to significant performance improvements in your application.

Learn more

Frequently Asked Questions

LRU vs LFU — which?

LRU — recency matters. LFU — frequency matters. For web: LRU usually wins (recency correlates with future access). LFU better for static catalog queries.

Is Redis LRU really accurate?

Redis uses approximate LRU for efficiency. Sample 5 keys, evict oldest. Accurate enough for most use cases.

TTL vs LRU?

Different. TTL — absolute expiry (delete after N sec). LRU — eviction on memory pressure. Often combined: TTL on data + LRU for overflow.

Try the live tool that powered this guide

Free plan — 10 monitors, checks every 5 min, no card required. Upgrade for 1-minute interval and multi-region monitoring.