Mastering Redis maxmemory‑policy: How LRU, LFU and Other Eviction Strategies Work
This article explains how Redis handles memory exhaustion by configuring maxmemory and maxmemory-policy, detailing each eviction strategy (noeviction, volatile‑lru, volatile‑lfu, volatile‑ttl, volatile‑random, allkeys‑lru, allkeys‑lfu, allkeys‑random), their implementation details, and the underlying approximate LRU algorithm.
Default maxmemory and configuration
On 64‑bit systems the default maxmemory is 0 (no limit); on 32‑bit systems it defaults to 3 GB. Setting a non‑zero value (e.g., maxmemory 6gb) enables eviction according to maxmemory-policy.
maxmemory 6gb
maxmemory-policy allkeys-lru # or volatile-lruEviction policies
noeviction : when the limit is reached, write commands (except DEL, HDEL, UNLINK) are rejected; reads continue.
volatile-lru : approximate LRU applied only to keys with an expiration time.
volatile-lfu : approximate LFU applied only to keys with an expiration time.
volatile-ttl : evicts the key with the shortest remaining TTL among expiring keys.
volatile-random : randomly evicts an expiring key.
allkeys-lru : approximate LRU applied to all keys.
allkeys-lfu : approximate LFU applied to all keys.
allkeys-random : randomly evicts any key.
Approximate LRU algorithm
Redis stores a 24‑bit “LRU clock” value for each key. The eviction process is triggered by a client request that needs memory.
When a key is accessed, its LRU clock is updated to the current server clock.
Redis selects maxmemory-samples keys at random (default 5).
For each sampled key it computes idle time = current clock – key’s LRU clock.
The key with the largest idle time is inserted into an eviction pool (default size 16). If the pool is full, the entry with the smallest idle time is discarded.
If the freed memory is still insufficient, steps 2‑4 repeat until the target memory usage is reached.
Increasing maxmemory-samples improves eviction accuracy but raises CPU usage.
LRU vs. LFU
LRU (Least Recently Used) evicts keys that have not been accessed for the longest time. LFU (Least Frequently Used) evicts keys with the lowest access frequency over a recent window, which can be more precise for workloads with occasional accesses.
Why Redis uses an approximate LRU
Redis stores data only in hash tables; maintaining a separate doubly‑linked list for true LRU would increase memory consumption.
Synchronising a linked list on every request would add noticeable CPU overhead.
The approximate algorithm provides a good trade‑off between memory overhead, CPU cost, and eviction accuracy.
Key parameters
maxmemory– maximum memory Redis may use before eviction starts. maxmemory-policy – selects one of the eight policies described above. maxmemory-samples – number of random keys examined per eviction cycle (default 5).
Eviction pool size – number of candidate keys kept in memory (default 16).
Senior Tony
Former senior tech manager at Meituan, ex‑tech director at New Oriental, with experience at JD.com and Qunar; specializes in Java interview coaching and regularly shares hardcore technical content. Runs a video channel of the same name.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
