Linux performance tuning is often treated as black magic. It is not. Every parameter has a documented purpose, measurable impact, and specific workload where it helps (or hurts).
This guide covers the kernel parameters that actually move the needle in production, organized by subsystem. Every recommendation includes the workload it benefits, the trade-off, and how to verify it worked.
The Tuning Philosophy
Before changing anything:
- Establish a baseline (metrics, benchmarks, latency percentiles)
- Change ONE parameter at a time
- Measure under realistic load
- Document why you changed it
Never copy-paste sysctl configs from blog posts. Your workload is not their workload.
Memory Management
vm.swappiness
# Current value
cat /proc/sys/vm/swappiness
# Default: 60
# For latency-sensitive workloads (databases, real-time):
sysctl -w vm.swappiness=1
# Or 0 for "swap only on OOM" (kernel 3.5+)
# For batch/throughput workloads:
sysctl -w vm.swappiness=100
Why it matters: Controls swap aggressiveness. Value 1 means “avoid swap unless absolutely necessary.” Value 100 means “swap aggressively to keep page cache hot.”
Verify: watch -n 1 cat /proc/meminfo | grep -E "Swap|Cached"
vm.dirty_ratio and vm.dirty_background_ratio
# Defaults: dirty_background_ratio=10, dirty_ratio=20
# These are PERCENTAGES of available memory
# For databases with large memory (64GB+):
sysctl -w vm.dirty_background_ratio=5
sysctl -w vm.dirty_ratio=10
# For write-heavy workloads on fast storage (NVMe):
sysctl -w vm.dirty_background_ratio=3
sysctl -w vm.dirty_ratio=5
Why it matters: Controls when background writeback starts (dirty_background_ratio) and when processes block on writeback (dirty_ratio). Lower values = more frequent, smaller flushes = lower latency spikes.
Alternative (absolute bytes, often better):
# 512MB background, 1GB max dirty
sysctl -w vm.dirty_background_bytes=536870912
sysctl -w vm.dirty_bytes=1073741824
vm.overcommit_memory
# Default: 0 (heuristic overcommit)
# Mode 1: Always overcommit (dangerous for databases)
# Mode 2: Strict overcommit (total commit limit)
# For Redis, MongoDB, JVM workloads:
sysctl -w vm.overcommit_memory=1
# Required because these allocate large virtual address spaces
# For general purpose:
sysctl -w vm.overcommit_memory=0
Critical: With mode 2, set vm.overcommit_ratio (default 50). Commit limit = RAM * ratio + swap.
vm.max_map_count
# Default: 65530
# Elasticsearch, ClickHouse, JVMs need more:
sysctl -w vm.max_map_count=262144
# Or higher for massive mappings
Network Stack
net.core.somaxconn
# Default: 128 (way too low for production)
sysctl -w net.core.somaxconn=65535
# Also set in application:
# nginx: listen 80 backlog=65535;
# redis: tcp-backlog 65535
# postgres: max_connections (related but different)
Why: Backlog queue for pending connections. If full, SYN packets dropped, clients see connection refused.
net.ipv4.tcp_max_syn_backlog
# Default: 1024 (too low)
sysctl -w net.ipv4.tcp_max_syn_backlog=65535
# Works with somaxconn for SYN flood protection
net.ipv4.tcp_tw_reuse
# Default: 0
sysctl -w net.ipv4.tcp_tw_reuse=1
# Allows reusing TIME_WAIT sockets for new connections
# Safe with TCP timestamps (default on)
# Critical for high connection churn (load balancers, microservices)
net.ipv4.tcp_fin_timeout
# Default: 60 seconds
sysctl -w net.ipv4.tcp_fin_timeout=15
# Faster TIME_WAIT cleanup
# Trade-off: may break NAT traversal in some edge cases
net.core.netdev_max_backlog
# Default: 1000
sysctl -w net.core.netdev_max_backlog=30000
# Packet queue per CPU before kernel drops
# Increase for 10GbE+ or bursty traffic
net.ipv4.tcp_rmem / tcp_wmem
# Defaults: min=4096, default=87380, max=6291456 (bytes)
# For high-latency, high-bandwidth (10GbE+, cross-region):
sysctl -w net.ipv4.tcp_rmem="4096 87380 33554432"
sysctl -w net.ipv4.tcp_wmem="4096 65536 33554432"
# BDP = bandwidth * RTT
# 10Gbps * 100ms = 125MB buffer needed
net.ipv4.tcp_congestion_control
# Default: cubic (good general purpose)
# For high-bandwidth, high-latency:
sysctl -w net.ipv4.tcp_congestion_control=bbr
# BBR v2 (kernel 5.10+): better fairness, less queue buildup
# For datacenter (low RTT, lossless):
sysctl -w net.ipv4.tcp_congestion_control=dctcp
# Requires ECN support on switches
Filesystem I/O
vm.vfs_cache_pressure
# Default: 100
# Reclaim dentries/inodes more aggressively:
sysctl -w vm.vfs_cache_pressure=200
# Reclaim less (keep metadata cached):
sysctl -w vm.vfs_cache_pressure=50
Use case: High inode workloads (millions of small files) → increase. Metadata-heavy workloads (git, CI/CD) → decrease.
Block scheduler (per-device)
# Check current
cat /sys/block/nvme0n1/queue/scheduler
# Output: [none] mq-deadline kyber bfq
# NVMe: none (hardware handles queueing)
echo none > /sys/block/nvme0n1/queue/scheduler
# HDD/SSD (SATA): mq-deadline or bfq
echo mq-deadline > /sys/block/sda/queue/scheduler
# bfq for desktop/interactive, mq-deadline for server
# Make persistent via udev rule:
# /etc/udev/rules.d/60-scheduler.rules
# ACTION="add|change", KERNEL="nvme*", ATTR{queue/scheduler}="none"
Read-ahead (per-device)
# Check current (in 512-byte sectors)
blockdev --getra /dev/nvme0n1
# Default: 256 (128KB)
# For sequential workloads (logs, backups, video):
blockdev --setra 4096 /dev/nvme0n1 # 2MB
# For random workloads (databases):
blockdev --setra 128 /dev/nvme0n1 # 64KB
CPU Scheduling
kernel.sched_min_granularity_ns
# Default: 2250000 (2.25ms)
# Lower = more responsive, more context switches
sysctl -w kernel.sched_min_granularity_ns=1000000
# For low-latency interactive workloads
kernel.sched_migration_cost_ns
# Default: 500000 (0.5ms)
# Cost of migrating task between CPUs
# Increase for NUMA systems to reduce cross-node migration:
sysctl -w kernel.sched_migration_cost_ns=5000000
CPU Isolation (for real-time / ultra-low latency)
# Kernel cmdline: isolcpus=2-7 nohz_full=2-7 rcu_nocbs=2-7
# Then pin critical tasks:
taskset -c 2-7 ./my-low-latency-app
# Or use cgroups:
echo 2-7 > /sys/fs/cgroup/cpuset/critical/cpuset.cpus
echo 0 > /sys/fs/cgroup/cpuset/critical/cpuset.mems
The tuned Daemon (RHEL/Fedora/CentOS/Ubuntu)
Instead of raw sysctl, use tuned profiles:
# Install
apt install tuned # or dnf install tuned
# List profiles
tuned-adm list
# Key profiles:
tuned-adm profile throughput-performance # Batch/throughput
tuned-adm profile latency-performance # Low latency
tuned-adm profile network-throughput # Network heavy
tuned-adm profile virtual-guest # VM optimization
tuned-adm profile oracle # Database
tuned-adm profile sap-hana # SAP HANA
# Create custom profile:
# /etc/tuned/my-profile/tuned.conf
# [main]
# include=latency-performance
# [sysctl]
# vm.swappiness=1
# net.ipv4.tcp_congestion_control=bbr
NUMA Optimization
# Check topology
numactl --hardware
lscpu | grep NUMA
# Memory policy for process
numactl --interleave=all ./myapp
numactl --membind=0 ./myapp
# For JVM:
-XX:+UseNUMA -XX:+UseParallelGC
# For databases (PostgreSQL):
# postgresql.conf: numa=on (auto-detect)
Huge Pages
# Check
cat /proc/meminfo | grep Huge
# Reserve 1GB huge pages (2MB each = 512 pages)
echo 512 > /proc/sys/vm/nr_hugepages
# For 1GB pages (kernel 3.8+, hardware support):
echo 4 > /proc/sys/vm/nr_overcommit_hugepages
# Use with databases:
# PostgreSQL: huge_pages=on
# MySQL: large_pages=ON
# JVM: -XX:+UseLargePages
Monitoring: Did It Work?
Key Metrics to Track
# Latency percentiles (not averages!)
# Use: histogram_quantile(0.99, rate(http_request_duration_seconds_bucket[5m]))
# Context switches
watch -n 1 cat /proc/stat | grep ctxt
# Interrupts
watch -n 1 cat /proc/interrupts
# Memory pressure
watch -n 1 cat /proc/pressure/memory
# IO wait
iostat -x 1
# TCP retransmits
netstat -s | grep -i retransmit
# Or: ss -s
BPF Tools (Modern, Low Overhead)
# Install bcc or bpftrace
# Ubuntu: apt install bpftrace linux-tools-$(uname -r)
# TCP retransmits
bpftrace -e 'tracepoint:tcp:tcp_retransmit_skb { @[comm] = count(); }'
# Block I/O latency
bpftrace -e 'tracepoint:block:block_rq_issue { @start[arg0] = nsecs; } tracepoint:block:block_rq_complete /@start[arg0]/ { @lat[comm] = hist(nsecs - @start[arg0]); delete(@start[arg0]); }'
# Scheduler latency
bpftrace -e 'tracepoint:sched:sched_wakeup { @[comm] = hist(nsecs - args->timestamp); }'
Complete sysctl.conf Template
# /etc/sysctl.d/99-performance.conf
# Memory
vm.swappiness = 1
vm.dirty_background_ratio = 5
vm.dirty_ratio = 10
vm.max_map_count = 262144
vm.overcommit_memory = 0
vm.vfs_cache_pressure = 100
# Network
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 30000
net.core.rmem_max = 33554432
net.core.wmem_max = 33554432
net.ipv4.tcp_rmem = 4096 87380 33554432
net.ipv4.tcp_wmem = 4096 65536 33554432
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_congestion_control = bbr
net.ipv4.tcp_slow_start_after_idle = 0
# Filesystem
fs.file-max = 1000000
fs.inotify.max_user_watches = 524288
fs.inotify.max_user_instances = 8192
# Apply: sysctl --system
Verification Checklist
After applying changes:
- [ ]
sysctl --systemloads without errors - [ ] Baseline benchmark re-run shows improvement
- [ ] No OOM kills in
dmesg | grep -i oom - [ ] No TCP drops in
netstat -s | grep -i "listen queue overflow" - [ ] Latency percentiles improved (p99, p999)
- [ ] Throughput maintained or improved
- [ ] Changes survive reboot (
cat /etc/sysctl.d/99-performance.conf)
What NOT to Touch
| Parameter | Why |
|---|---|
vm.min_free_kbytes |
Auto-calculated correctly; manual = OOM risk |
net.ipv4.tcp_timestamps |
Required for PAWS, TCP extensions |
net.ipv4.tcp_sack |
Breaks recovery on loss |
kernel.panic_on_oops |
Use panic_on_oops=1 only for debugging |
vm.zone_reclaim_mode |
Deprecated, NUMA handled automatically |
Summary
Linux tuning is not about finding “magic numbers.” It is about:
- Understanding your workload’s bottlenecks (CPU, memory, network, disk)
- Knowing which knobs control those resources
- Measuring before and after
- Documenting the rationale
Start with the defaults. They are good for 90% of workloads. Only tune when you have data proving a bottleneck exists, and you understand the trade-off.
The parameters that most often need adjustment in production:
vm.swappiness(databases → 1)net.core.somaxconn(high concurrency → 65535)net.ipv4.tcp_tw_reuse(microservices → 1)net.ipv4.tcp_congestion_control(WAN/high-BDP → bbr)vm.max_map_count(ES/ClickHouse/JVM → 262144)- Block scheduler (NVMe → none, HDD → mq-deadline)
Everything else: measure first, tune second, verify always.