Swap Space and Kernel Swappiness

  • concept
    On Linux, the kernel’s swappiness level indicates how likely the system is to swap pages out of physical memory based on RAM usage. Swappiness should be set to 1 or 0 on most Linux systems to achieve optimal Couchbase Server performance.

    Couchbase Server efficiently uses available RAM for your working set data; ideally, sufficient RAM remains available to the operating system above and beyond your cluster’s configured server RAM quota. It’s always a good idea to configure a reasonable amount of virtual memory or swap space on Linux-based nodes. (There are no recommended virtual memory optimizations for Windows-based nodes.) Sufficient virtual memory helps prevent Couchbase Server processes from being killed by the OS, such as by the Linux out of memory (OOM) killer.

    The Linux kernel’s swappiness setting defines how aggressively the kernel will swap memory pages versus dropping pages from the page cache. A higher value increases swap aggressiveness, while a lower value tells the kernel to swap as little as possible to disk and favor RAM. The swappiness range is from 0 to 100, and most Linux distributions have swappiness set to 60 by default.

    Couchbase Server is optimized with its managed cache to use RAM, and is capable of managing what should be in RAM and what shouldn’t be. Allowing the OS to have too much control over what memory pages are in RAM is likely to lower Couchbase Server’s performance. Therefore, it’s recommended that swappiness be set to the levels listed below.

    *Linux kernel 3.5-rc1 and higher introduced a change in behavior for swappiness=0. Depending on how a Linux OS implements this change, it has the potential to increase the risk of OOM killing under strong memory and I/O pressure. As a result, the recommended swappiness setting for these Linux platforms is 1. (Though, it may be the case that a given Linux OS has implemented or patched this change in such a way that a swappiness setting of 0 is still preferred.)

    Changing Swappiness Setting

    The Linux kernel’s swappiness setting tells the virtual memory subsystem how much it should attempt to swap to disk. Linux operating systems often default to a setting of 60, which can cause the OS to swap out items from memory even when there is plenty of RAM available. This behavior is undesirable given Couchbase Server’s memory-first architecture.

    1. Verify your current system’s swappiness setting.

      cat /proc/sys/vm/swappiness

      If the output doesn’t match the recommended settings, proceed to the next step.

    2. Change the swappiness setting.

      1. Set the value for the running system.

        sudo sh -c 'echo 1 > /proc/sys/vm/swappiness'
      2. Backup sysctl.conf.

        sudo cp -p /etc/sysctl.conf /etc/sysctl.conf.`date +%Y%m%d-%H:%M`
      3. Set the value in /etc/sysctl.conf so it stays after reboot.

        sudo sh -c 'echo "" >> /etc/sysctl.conf'
        sudo sh -c 'echo "#Set swappiness to 1 to minimize swapping" >> /etc/sysctl.conf'
        sudo sh -c 'echo "vm.swappiness = 1" >> /etc/sysctl.conf'

    Setting swappiness needs to be a part of the build process for any new Couchbase node. Make sure to modify any continuous deployment process that builds the OS. This also applies to golden master OS images and configuration automation systems. It is especially critical for public/private clouds where it is easy to bring up new instances.