Skip to content

How to Change Redis Max Memory

Category
Web Servers
Covers
Redis
Versions & support
About Redis →

To configure the maximum amount of memory that Redis will use, you can use the `maxmemory` directive in the Redis configuration file (`redis.conf`). This directive takes an integer value, representing the maximum number of bytes that Redis will use to store data in memory.

For example, to set the maximum memory to `1GB`, (or 1024*1024*1024 bytes) you can use the following configuration:

You can also specify a policy for how Redis should handle the situation when the maximum memory limit is reached. This is done using the `maxmemory-policy` directive, which can take one of the following values:

  • noeviction: Redis will return an error when the maximum memory limit is reached and a new key needs to be added.
  • allkeys-lru: Redis will remove the least recently used keys in order to make space for new keys.
  • volatile-lru: Redis will remove the least recently used keys among keys with an expire set in order to make space for new keys.
  • allkeys-random: Redis will randomly select keys to remove in order to make space for new keys.
  • volatile-random: Redis will randomly select keys with an expire set to remove in order to make space for new keys.
  • volatile-ttl: Redis will remove keys with the shortest time to live in order to make space for new keys.

For example, to set the `maxmemory-policy` to `allkeys-lru`, you can use the following configuration:

Note that the maxmemory and maxmemory-policy directives must be set in the Redis configuration file (redis.conf) and cannot be set using the CONFIG SET command at runtime. You will need to restart Redis for the changes to take effect.

It’s also worth noting that Redis will automatically try to free memory when it is running out of available memory, by releasing memory used by the least recently used keys. However, this process is limited by the maxmemory-samples directive, which determines the number of keys that Redis will sample in order to determine the keys to be removed. By default, this value is set to 3, so Redis will only sample 3 keys in order to determine the keys to be removed. You can adjust this value if needed by using the maxmemory-samples directive in the Redis configuration file.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top