Introduction
For optimal performance of Couchbase Server on Linux systems, understanding and configuring the kernel's swappiness setting is crucial. Swappiness controls the degree to which your system swaps memory pages from physical memory to disk. This article explains the concept of swappiness, why it's important for Couchbase Server, and provides guidance on setting it correctly to enhance your Couchbase deployment.
Understanding Swappiness
Swappiness is a kernel parameter that influences the swapping behavior of the Linux operating system. The parameter's value can range from 0 to 100:
- 0 instructs the kernel to avoid swapping processes out of physical memory for as long as possible.
- 100 tells the kernel to aggressively swap processes, even when there is plenty of free memory.
Why Adjust Swappiness for Couchbase?
Couchbase Server is designed to operate efficiently by utilizing available RAM for data storage and operations, thereby reducing disk I/O which is usually slower. High swappiness values can lead to increased disk swapping, which might degrade the performance of Couchbase Server due to slower access times when compared to RAM.
Recommended Swappiness Settings
First, you need to determine which Linux kernel version you're using. You can achieve this by running the below command in the terminal of your cluster nodes:
uname -r
If you are using kernel version 3.5-rc1 or newer, you will need to set swappiness to 1.
If you see that you're using kernel version 3.4.113 or lower, you will need to set swappiness to 0.
Please note! If you're using Red Hat Enterprise Linux version 8.8 or higher, your kernel version is at least 4.18.0-477.10.1.el8_8 or newer (as per official Red Hat documentation). In this case, you need to set swappiness to 1. However, due to a known issue with RHEL 8+ where changes to the swappiness setting might not be respected, it's advised to set vm.force_cgroup_v2_swappiness to 1. We will cover more details later in this article.
Linux Kernel 3.5-rc1 and Newer:
Set swappiness to 1. This setting helps mitigate the risk of out-of-memory (OOM) kills under high memory and I/O pressure without being too aggressive about moving processes to swap.
Older Linux Kernels:
Set swappiness to 0 to minimize swapping, as older kernels handled a swappiness of 0 without the risk of aggressive OOM killing.
Special Note for Red Hat Enterprise Linux 8.8+:
Due to a known issue with RHEL 8+ where changes to the swappiness setting might not be respected, it's advised to set vm.force_cgroup_v2_swappiness to 1. When set to 1, all the cgroup's memory.swappiness value becomes deprecated, and all per-cgroups swappiness values mirrors the system-wide vm.swappiness sysctl value (i.e. /proc/sys/vm/swappiness file). As a result, the memory swapping behavior of cgroups is more consistent. This is the recommended solution while using cgroups v1.
Please refer to the official Red Had documentation in the Red Hat Customers Portal article for more details about how to set it up.
Special Note for Cloud-Native Deployments:
By default, Kubernetes does not support running pods on nodes with swap enabled. The kubelet, which is the Kubernetes node agent, requires swap to be disabled for stability and performance reasons. As mentioned in the official Kubernetes documentation:
The default behavior of a kubelet was to fail to start if swap memory was detected on a node. Swap has been supported since v1.22. And since v1.28, Swap is supported for cgroup v2 only; the NodeSwap feature gate of the kubelet is beta but disabled by default.
As mentioned in the Guidelines and Best Practices article of the Couchbase Autonomous Operator documentation:
It is recommended for Couchbase Server that vm.swappiness be set to zero. Kubernetes does not allow swap in containers therefore this parameter can be safely ignored.
Step-by-Step Configuration Guide
Verify Current Swappiness Value:
cat /proc/sys/vm/swappiness
Change Swappiness Setting Temporarily. For immediate effect (does not survive reboot):
sudo sh -c 'echo 1 > /proc/sys/vm/swappiness'
Or Make Swappiness Setting Persistent(recommended):
Backup the current sysctl configuration:
sudo cp -p /etc/sysctl.conf /etc/sysctl.conf.`date +%Y%m%d-%H%M`
Append the swappiness setting to the sysctl configuration:
sudo sh -c 'echo "" >> /etc/sysctl.conf'
sudo sh -c 'echo "# Set swappiness to 1 to optimize Couchbase performance" >> /etc/sysctl.conf'
sudo sh -c 'echo "vm.swappiness = 1" >> /etc/sysctl.conf'
Reload Sysctl Configuration. Apply the changes without rebooting:
sudo sysctl -p
Confirm Changes. Recheck the swappiness setting to ensure the configuration is applied:
cat /proc/sys/vm/swappiness
Comments
4 comments
Thanks for this note. With CNF deployments becoming common, it would be useful to also consider the applicability of the above in case of CNF deployments and include the same in above note.
Could you please let us know the above settings to change swappiness to 1 is across all RHEL OS or any specific RHEL version.
In the couchbase documentation it says we need to set the swappiness to “vm.swappiness” to zero but here article says need to set 1. Could you please confirm which one is correct.
https://docs.couchbase.com/server/current/install/install-swap-space.html#:~:text=Swappiness%20should%20be%20set%20to,cluster's%20configured%20server%20RAM%20quota.
Thanks,
Debasis
Thank you for your comments. We have updated the article to reflect them. Please review it and let us know if you have any other questions.
Please sign in to leave a comment.