Skip to content

AxonOps — AI-Native Control Plane for Open Source Data Platforms

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.

Terminal window
# Disable swap now
sudo swapoff -a
# Remove or comment any swap entries in /etc/fstab so it stays off after reboot

2. 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.

Terminal window
sudo sysctl vm.swappiness=1
# Make permanent
echo "vm.swappiness = 1" | sudo tee -a /etc/sysctl.conf

The 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.swappinessBehavior
0Since Linux 3.5, the kernel avoids swapping anonymous memory even under pressure, raising the risk of an OOM kill. Do not use.
1Minimal swapping; the recommended value where swap is present.
10Light swapping.
60Kernel 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 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:

Terminal window
# Check current status
cat /sys/kernel/mm/transparent_hugepage/enabled
cat /sys/kernel/mm/transparent_hugepage/defrag
# Disable at runtime
echo never | sudo tee /sys/kernel/mm/transparent_hugepage/enabled
echo 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 Pages
DefaultDependencies=no
After=sysinit.target local-fs.target
Before=cassandra.service
[Service]
Type=oneshot
ExecStart=/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.target

The 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.

Terminal window
sudo systemctl enable disable-thp
sudo systemctl start disable-thp

THP 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.


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.

Terminal window
# Check current limit (Linux default is 65530)
cat /proc/sys/vm/max_map_count
# Set to the value Cassandra's startup check recommends
sudo sysctl vm.max_map_count=1048575
# Make permanent
echo "vm.max_map_count = 1048575" | sudo tee -a /etc/sysctl.conf

On NUMA systems, zone reclaim mode controls whether the kernel reclaims memory from local zones before allocating from remote zones.

Terminal window
# Disable zone reclaim (recommended for databases)
sudo sysctl vm.zone_reclaim_mode=0
# Make permanent
echo "vm.zone_reclaim_mode = 0" | sudo tee -a /etc/sysctl.conf

On multi-socket servers with Non-Uniform Memory Access (NUMA), memory access latency varies depending on which CPU socket allocated the memory.

Options:

  1. Interleave memory (simple, recommended for most cases):

    Terminal window
    numactl --interleave=all cassandra
  2. Bind to single node (if Cassandra fits in one node's memory):

    Terminal window
    numactl --cpunodebind=0 --membind=0 cassandra
  3. Let JVM handle it (modern JVMs are NUMA-aware):

    Terminal window
    # jvm-server.options
    -XX:+UseNUMA

Terminal window
# /etc/sysctl.conf additions for Cassandra
# Minimize swapping
vm.swappiness = 1
# Increase memory map limit
vm.max_map_count = 1048575
# Disable zone reclaim
vm.zone_reclaim_mode = 0
# Network settings (optional, for high throughput)
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216

Apply changes:

Terminal window
sudo sysctl -p