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 →
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 |
---|---|
noeviction | Keys are not deleted, server returns an error message as soon as an attempt is made to add a record |
allkeys-lru | Deletes the least recently used data records |
allkeys-lfu | Deletes the most recently used data records |
allkeys-random | Deletes random records |
volatile-lru | Deletes the least recently used records, which have also expired |
volatile-lfu | Deletes the most recently used data records, which have also expired |
volatile-random | Deletes random data records that have expired |
volatile-ttl | Deletes expired data records with the shortest Time-To-Live (TTL) value |
Recommendations for specific web applications
Web application | Recommended policy |
---|---|
Shopware 6 | volatile-lru |
JTL Shop 5 | allkeys-lru |
Nextcloud | allkeys-lru |
WordPress | allkeys-lru |