Redis - Max Memory & Key Eviction Policy

Foreword

To ensure that Redis does not exceed the maximum physical working memory, several so-called Key Eviction Policies are available, which make it possible to automatically remove the stored keys based on the criteria defined in the policy until the memory limit is reached.


Requirements

  • Redis server (with root access)



Customize configuration

Connect to your Redis server via SSH, for example, and log in as the SSH root user.

Open the Redis configuration file with an editor of your choice, we use nano in this case:

nano /etc/redis/redis.conf


The Redis service must be restarted after every change:

systemctl restart redis-server.service



Max Memory

The Max Memory setting allows you to limit the amount of memory used by Redis. Search for maxmemory in the configuration file and define the desired value.


maxmemory 4GB # sets the limit to 4 GiB


The following table shows the possible sizes of the maximum memory size:

value definition
500MB 500 MiB
1GB 1 GiB
4GB 4 GiB
8GB 8 GiB
12GB 12 GiB
16GB 16 GiB 16 GiB


The suffixes mb, gb are interpreted as binary prefixes (IEC prefixes). More about memory sizes →



Difference LRU & LFU

**What is LRU (Least Recently Used)?

LRU is a memory and cache optimization algorithm that always removes the data that has not been used for the longest time. This means that when the memory is full, the system first deletes the entries that have been used the least recently.\nLRU is particularly suitable for applications where current data is significantly more important than older data, as it realistically reflects typical user behavior.



**What is LFU (Least Frequently Used)?

LFU is an algorithm for efficient cache management that removes the entries that have been used the least frequently. Instead of looking at the time of last use, LFU counts how often a key has been accessed.\nLFU is ideal if frequently used data is to remain in the cache and rarely used data is to be systematically displaced.



Short comparison LRU vs. LFU

LRU prioritized currency LFU prioritized frequency of use**


Both methods improve cache performance, but pursue different strategies for memory optimization.



Max Memory Policy

The Max Memory Policy setting defines the deletion behavior of Redis if the addition of data records results in the Max Memory Limit being exceeded.


Search for maxmemory-policy in the configuration file and set the desired key eviction policy.

maxmemory-policy volatile-ttl # default setting


Key Eviction Policies

The following table contains a summary of all key eviction policies supported by Redis and their description.

Policy Description Consideration of Expire TTL
noeviction No keys are removed. An error is output for new write operations if there is no more free memory. ❌ No
allkeys-lru Removes the least recently used (LRU) keys. ❌ No (affects all keys)
allkeys-lfu Removes the least recently used (LFU) keys. ❌ No (affects all keys)
allkeys-random Removes randomly selected keys. ❌ No (affects all keys)
volatile-lru Removes LRU keys, but only those with a set expiration date. ✔️ Yes (only keys with expire)
volatile-lfu Removes LFU keys, but only those with a set expiration date. ✔️ Yes (only keys with expire)
volatile-random Removes keys randomly, but only those with a set expiration date. ✔️ Yes (only keys with expire)
volatile-ttl Removes keys with expiration date, starting with the shortest remaining TTL. ✔️ Yes (only keys with expire)