Cassandra Linux Memory Configuration
Linux kernel settings govern how a node behaves outside the JVM: whether the heap can be paged to disk, how the kernel assembles large pages, and how many memory mappings the process may hold. Most settings have sensible defaults, and the ones below are the adjustments a Cassandra node needs.
Memory Management introduces the JVM heap, off-heap memory and the page cache that these settings act on.
Swap is disk space the kernel uses to hold anonymous memory pages, process memory that is not backed by a file, when it needs to free physical RAM. Reading a page back from swap is disk I/O, several orders of magnitude slower than RAM access.
Swapping is damaging to Cassandra for three reasons:
- The JVM heap is anonymous memory. Once any part of the heap is paged to swap, every access to those pages becomes disk I/O, producing latency spikes unrelated to the query being served.
- Garbage collection walks the whole heap. If part of the heap has been swapped out, a GC cycle faults it all back in, turning a short pause into a long one.
- A node stalled on swap can miss gossip heartbeats and be marked down by its peers, so local memory pressure can escalate into a cluster-level event.
vm.swappiness is a kernel tunable from 0 to 100 that biases memory reclaim between two choices: dropping file-backed page cache, or swapping out anonymous memory. A higher value makes the kernel more willing to swap anonymous pages; a lower value makes it prefer to reclaim page cache and keep anonymous memory resident. The setting has an effect only while a swap device is present.
The recommendation has two layers, and both should be applied.
1. Disable swap. This is the primary step. With no swap device present, the JVM heap can never be paged out, and vm.swappiness has no effect.
# Disable swap nowsudo swapoff -a
# Remove or comment any swap entries in /etc/fstab so it stays off after reboot2. Set vm.swappiness=1 as a safeguard. On hosts where swap cannot be removed, for example a cloud image that provisions swap or configuration management that re-adds it, this keeps the kernel from swapping anonymous memory except to avoid an out-of-memory kill.
sudo sysctl vm.swappiness=1
# Make permanentecho "vm.swappiness = 1" | sudo tee -a /etc/sysctl.confThe value should be 1, not 0. Since Linux 3.5, a vm.swappiness of 0 stops the kernel from swapping anonymous memory even under memory pressure, which raises the likelihood that the OOM killer terminates the node. For a database process, a small amount of swap is less disruptive than an OOM kill, so 1 is the lowest value that should be used.
| vm.swappiness | Behavior |
|---|---|
| 0 | Since Linux 3.5, the kernel avoids swapping anonymous memory even under pressure, raising the risk of an OOM kill. Do not use. |
| 1 | Minimal swapping; the recommended value where swap is present. |
| 10 | Light swapping. |
| 60 | Kernel default, moderate swapping. |
Cassandra also attempts to lock its heap into RAM at startup with mlockall (through JNA) when memory locking is permitted. A locked heap is never swapped, so setting memlock to unlimited for the Cassandra user in the OS limits keeps this protection active. Memory locking complements disabling swap; it is not a substitute for it.
Transparent Huge Pages (THP)
Section titled “Transparent Huge Pages (THP)”Transparent Huge Pages is a Linux feature that automatically manages large memory pages (2MB instead of 4KB). While THP can improve performance for some workloads, it causes problems for Cassandra:
- Memory allocation latency spikes during defragmentation
- Unpredictable pause times unrelated to GC
- Memory fragmentation over time
Disable THP:
# Check current statuscat /sys/kernel/mm/transparent_hugepage/enabledcat /sys/kernel/mm/transparent_hugepage/defrag
# Disable at runtimeecho never | sudo tee /sys/kernel/mm/transparent_hugepage/enabledecho never | sudo tee /sys/kernel/mm/transparent_hugepage/defrag
# Make permanent (add to /etc/rc.local or systemd unit)Both enabled and defrag must be set to never. The defrag control governs whether the kernel stalls the allocating thread to compact memory into a huge page on allocation; leaving defrag active reintroduces the allocation-time pauses even when enabled is set to madvise. Check both values before and after applying the change.
For systemd-based systems, create /etc/systemd/system/disable-thp.service:
[Unit]Description=Disable Transparent Huge PagesDefaultDependencies=noAfter=sysinit.target local-fs.targetBefore=cassandra.service
[Service]Type=oneshotExecStart=/bin/sh -c "echo never > /sys/kernel/mm/transparent_hugepage/enabled"ExecStart=/bin/sh -c "echo never > /sys/kernel/mm/transparent_hugepage/defrag"
[Install]WantedBy=multi-user.targetThe Before=cassandra.service ordering is required: without it, systemd may start Cassandra before THP is disabled, so the setting takes effect only after the JVM has already begun allocating heap. DefaultDependencies=no with After=sysinit.target local-fs.target places the unit early in boot, before the service manager reaches cassandra.service.
sudo systemctl enable disable-thpsudo systemctl start disable-thpTHP Must Be Disabled
Cassandra does not check Transparent Huge Pages at startup, so there is no runtime warning to rely on. Problems caused by THP appear as unpredictable latency spikes unrelated to GC and are difficult to attribute after the fact. Disable THP as part of node provisioning rather than in response to an alert.
Memory Map Limits
Section titled “Memory Map Limits”Cassandra uses memory-mapped files for SSTable access, so the kernel limit on the number of memory mappings per process must be raised. In Cassandra 5.0, 4.1 and 4.0, a startup check inspects vm.max_map_count and logs a warning when it is below 1048575:
Maximum number of memory map areas per process (vm.max_map_count) <current> is too low,recommended value: 1048575, you can change it with sysctl.The Linux default of 65530 is far below this threshold and triggers the warning on every start, so vm.max_map_count must be set to at least 1048575 on every node. The check is a warning rather than a fatal error, so the node starts either way, but a node left at the default is prone to mapping failures as SSTable counts grow.
# Check current limit (Linux default is 65530)cat /proc/sys/vm/max_map_count
# Set to the value Cassandra's startup check recommendssudo sysctl vm.max_map_count=1048575
# Make permanentecho "vm.max_map_count = 1048575" | sudo tee -a /etc/sysctl.confZone Reclaim Mode
Section titled “Zone Reclaim Mode”On NUMA systems, zone reclaim mode controls whether the kernel reclaims memory from local zones before allocating from remote zones.
# Disable zone reclaim (recommended for databases)sudo sysctl vm.zone_reclaim_mode=0
# Make permanentecho "vm.zone_reclaim_mode = 0" | sudo tee -a /etc/sysctl.confNUMA Considerations
Section titled “NUMA Considerations”On multi-socket servers with Non-Uniform Memory Access (NUMA), memory access latency varies depending on which CPU socket allocated the memory.
Options:
-
Interleave memory (simple, recommended for most cases):
Terminal window numactl --interleave=all cassandra -
Bind to single node (if Cassandra fits in one node's memory):
Terminal window numactl --cpunodebind=0 --membind=0 cassandra -
Let JVM handle it (modern JVMs are NUMA-aware):
Terminal window # jvm-server.options-XX:+UseNUMA
Recommended Kernel Settings
Section titled “Recommended Kernel Settings”# /etc/sysctl.conf additions for Cassandra
# Minimize swappingvm.swappiness = 1
# Increase memory map limitvm.max_map_count = 1048575
# Disable zone reclaimvm.zone_reclaim_mode = 0
# Network settings (optional, for high throughput)net.core.rmem_max = 16777216net.core.wmem_max = 16777216net.ipv4.tcp_rmem = 4096 87380 16777216net.ipv4.tcp_wmem = 4096 65536 16777216Apply changes:
sudo sysctl -pRelated Documentation
Section titled “Related Documentation”- Memory Management - The three memory pools and why a node divides memory that way
- JVM Architecture - The heap, the collector, and process memory outside the heap
- Cassandra Memory Structures - Where memtables, caches, and index structures sit