Keeping REDIS RAM Under Control

Redis is a wonderful tool, but (by design) it hoovers up all the RAM, and even dips into swap space on a linux host. In fact, the official documentation recommends that you disable swap entirely on the host. But in the real world, that is not always an option. Sometimes, depending on your application design, or the scope of the application (say, a single dev box) you might have to run other things on the same host and don’t want redis to completely consume the box.

One approach would be to run it out of a container, which inherently puts a box around the sand, as it were. But in the case where you do not have or want to use a container environment (there are reasons to use and not to use them), I have been experimenting with settings that keep redis in check.

The first, and most obvious, setting is the maxmemory parameter in your redis.conf file. In my case, I set it to 4gb (the comments say to specify in bytes, but the Ng for gigabytes format was accepted without complaint). If you set this, you will notice that it will still consume a good bit more than 4GB of RAM. This parameter seems to be just for table/keypair space. Redis will still consume more for its own internal processes and usage.

Even with this set, after a couple of days, my OS swap space was still about 60% consumed, causing my monitoring tools to bark at me. While it is not unusual for small amounts of swap to be used in the normal operation of the linux kernel, this seemed to be far more than normal.

The next parameter that seemed appropriate at this point is the kernel parameter called vm.swappiness. This parameter is often greatly misunderstood to mean the percentage of RAM in use before the OS swaps. It is actually the ratio of the priority of file based pages to anonymous pages for which is written to swap when the kernel decides to swap. That, of course, is a whole topic unto itself. Rather than overload the topic here, I will refer you to an excellent article that explains it in great detail.

In the case of a redis server, almost all of the RAM that redis uses will be anonymous pages. So, to keep them in RAM, we want to tell the kernel to give a higher priority to writing file system pages instead of anonymous. To accomplish this, use a lower number (from the range of 0-100). In my case, I used a value of zero. This has greatly improved the system from swapping out the redis cache, and since in many cases file based pages can simply be re-read in from the original file, actual OS swap space may not be used.

The parameter can be set temporarily on the fly without a reboot:

sysctl vm.swappiness=0

However that will not make it persist across a reboot. To set the swappiness value permanently, add the following line to your /etc/sysctl.conf file (or another named file in /etc/sysctl.d/):


Leave a Comment