Mastering Redis Memory Eviction: Policies, Algorithms, and Configuration
This article explains how Redis handles memory exhaustion by distinguishing expiration and eviction policies, detailing the maxmemory setting, enumerating all eviction strategies—including LRU and LFU algorithms—and showing how to view and modify these policies for optimal cache performance.
Introduction
Redis cache uses memory to store data, and as the amount of cached data grows, the limited cache space will inevitably become full. This article discusses what to do when the cache is full and explains Redis's memory eviction mechanisms.
Note that in Redis, the expiration policy and the memory eviction policy are two completely different concepts. The expiration policy determines how expired key‑value pairs are removed, while the memory eviction mechanism decides which keys to delete when the used memory exceeds the configured maximum, ensuring Redis runs efficiently.
Redis Maximum Memory
Memory eviction is triggered only when Redis's used memory reaches a threshold defined by the maxmemory configuration item, which can be found in the Redis configuration file.
You can view the configured maximum memory with the command:
127.0.0.1:6379> config get maxmemory
1) "maxmemory"
2) "0"A value of 0 means there is no memory limit on a 64‑bit operating system; on a 32‑bit system the default limit is 3 GB.
Memory Eviction Policies
Viewing the Current Policy
Use the command config get maxmemory-policy to see the current eviction policy:
127.0.0.1:6379> config get maxmemory-policy
1) "maxmemory-policy"
2) "noeviction"The noeviction policy means Redis will not evict any data when memory is exceeded; instead, write operations will fail.
Policy Types
Early Redis versions offered six eviction policies:
noeviction : never evicts; writes fail when memory is insufficient (default).
allkeys-lru : evicts the least recently used key among all keys.
allkeys-random : evicts a random key.
volatile-lru : evicts the least recently used key among keys that have an expiration time.
volatile-random : evicts a random key among keys with an expiration time.
volatile-ttl : evicts the key with the nearest expiration time.
Redis 4.0 added two more policies:
volatile-lfu : evicts the least frequently used key among keys with an expiration time.
allkeys-lfu : evicts the least frequently used key among all keys.
Policies prefixed with allkeys- consider all keys for eviction, while those prefixed with volatile- consider only keys that have an expiration set.
Changing the Policy
There are two ways to set the eviction policy, each with its own trade‑offs:
Use config set maxmemory-policy <policy>. This takes effect immediately without restarting Redis, but the setting is lost after a restart.
Edit the Redis configuration file to set maxmemory-policy <policy>. The change persists across restarts, but Redis must be restarted for the new policy to apply.
Eviction Algorithms
Beyond the policy names, Redis primarily relies on two eviction algorithms: LRU (Least Recently Used) and LFU (Least Frequently Used).
LRU Algorithm
LRU evicts the key that has not been accessed for the longest time. It is typically implemented with a linked list where the most recently accessed key moves to the head, and eviction removes the tail element.
Redis uses an approximate LRU algorithm: each object stores an extra field with the last access time, and when eviction is needed Redis samples a configurable number of keys (default 5) and removes the least recently used among the sample.
Drawback: a key that was accessed once recently will not be evicted even if its overall usage frequency is low, which motivated the introduction of LFU in later versions.
LFU Algorithm
LFU evicts keys with the lowest access frequency, based on the principle that frequently accessed data is likely to be accessed again.
In Redis each object header stores LFU information: an 8‑bit counter ( logc) for frequency and a 16‑bit timestamp ( ldt) for the last decrement time.
typedef struct redisObject {
unsigned type:4;
unsigned encoding:4;
unsigned lru:LRU_BITS; /* LRU time or LFU data (freq and access time) */
int refcount;
void *ptr;
} robj;Conclusion
In summary, Redis's memory eviction policies are distinct from its expiration policies. Eviction is triggered when memory usage exceeds maxmemory, and the chosen maxmemory-policy determines which keys are removed. Since Redis 4.0 there are eight eviction policies, with noeviction as the default, which prevents any data removal and causes write errors when memory is full.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
