Redis - Max Memory & Key Eviction Policy
Introduction
To ensure that Redis does not exceed the maximum amount of physical memory, several so-called key eviction policies are available. These policies allow stored keys to be automatically removed based on criteria defined by the policy until the memory limit is no longer exceeded.
Prerequisites
- Redis server (with root access)
Customize the Configuration
Connect to your Redis server, for example via SSH, and log in as the SSH root user.
Open the Redis configuration file with an editor of your choice; in this case, we’ll use nano:
nano /etc/redis/redis.conf After each change, the Redis service must be restarted:
systemctl restart redis-server.serviceMax Memory
The maxmemory setting allows you to limit the amount of memory used by Redis. Search the configuration file for maxmemory and set the desired value.
maxmemory 4GB # sets the limit to 4 GiB The following table shows the possible values for the maximum memory size:
| Value | Definition |
|---|---|
| 500MB | 500 MiB |
| 1GB | 1 GiB |
| 4GB | 4 GiB |
| 8GB | 8 GiB |
| 12GB | 12 GiB |
| 16GB | 16 GiB |
The suffixes mb and gb are interpreted as binary prefixes (IEC prefixes). More on memory sizes →
Difference Between LRU & LFU
What is LRU (Least Recently Used)?
LRU is an algorithm for memory and cache optimization that always removes the data that has not been used for the longest time. This means that when memory is full, the system first deletes the entries that were most recently used the least frequently.\nLRU is particularly well-suited 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 overall. Instead of focusing on the time of the last use, LFU counts how often a key has been accessed.\nLFU is ideal when frequently used data should remain in the cache and rarely used data should be systematically evicted.
Quick Comparison: LRU vs. LFU
- LRU prioritizes recency
- LFU prioritizes frequency of use
Both methods improve cache performance but follow different strategies for memory optimization.
Max Memory Policy
The Max Memory Policy setting defines Redis’s eviction behavior when adding records would cause the Max Memory Limit to be exceeded.
Search the configuration file for maxmemory-policy and set the desired key eviction policy.
maxmemory-policy volatile-ttl # default setting Key Eviction Policies
The following table summarizes all key eviction policies supported by Redis and their descriptions.
| Policy | Description | Expire TTL Consideration |
|---|---|---|
| noeviction | No keys are removed. New write operations result in an error if there is no free memory. | ❌ No |
| allkeys-lru | Removes the least recently used (LRU) keys. | ❌ No (affects all keys) |
| allkeys-lfu | Removes the least frequently 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 an expiration date set. | ✔️ Yes (only keys with an expiration date) |
| volatile-lfu | Removes LFU keys, but only those with an expiration date set. | ✔️ Yes (only keys with an expiration date) |
| volatile-random | Removes keys at random, but only those with an expiration date set. | ✔️ Yes (only keys with an "expire" field) |
| volatile-ttl | Removes keys with an expiration date, starting with the shortest remaining TTL. | ✔️ Yes (only keys with an "expire" field) |
Recommendations for Specific Web Applications
| Web Application | Recommended Policy |
|---|---|
| Shopware 6 | volatile-lru |
| JTL Shop 5 | allkeys-lru |
| Nextcloud | allkeys-lru |
| WordPress | allkeys-lru |