DEV Community

Cover image for Linux Kernel Tuning for High-Performance Servers
Shuvo Halder
Shuvo Halder

Posted on

Linux Kernel Tuning for High-Performance Servers

Linux Kernel Tuning for High-Performance Servers

The Linux kernel ships with safe defaults, not fast ones. The defaults are chosen so a kernel works on a laptop, a Raspberry Pi, and a 64-core server without breaking anything — which means on a busy production server they leave performance on the table. A web server that drops connections under load, a database that stalls on writes, a server that swaps when it has free memory: these are often not application bugs. They are kernel defaults doing exactly what they were designed to do, in a place they were never tuned for.

Kernel tuning is the art of changing those defaults deliberately, one at a time, with measurements before and after. This post covers the settings production engineers actually change — memory, network, CPU, and huge pages — how to make them persistent, and the one rule that separates tuning from guessing.

How Tuning Works: sysctl Basics

Most kernel behaviour you can tune lives in /proc/sys, and sysctl is the tool that reads and writes it. A change made with sysctl -w applies immediately but vanishes on reboot. A change written to a config file survives reboots.

# See the current value of one setting
sysctl vm.swappiness

# Change it right now (lost on reboot)
sysctl -w vm.swappiness=10

# Make it permanent: write it to a file, then apply
echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.d/99-performance.conf
sudo sysctl -p /etc/sysctl.d/99-performance.conf
Enter fullscreen mode Exit fullscreen mode

Three habits to build from day one:

  • Put your changes in /etc/sysctl.d/, not in /etc/sysctl.conf directly. A separate file like 99-performance.conf is easy to review, version, and roll back. Package updates can touch the main file; they leave your drop-in alone.
  • Keep the file in version control. Every tuned value should have a comment saying why it was set and when. A value without a reason is a mystery for the next engineer — and for you, six months later.
  • Apply with sysctl -p and verify with sysctl <name>. Never assume a value took effect. Check it, and check that the service you tuned actually behaves differently.

Memory Tuning: Swap, Dirty Pages, and Overcommit

Swappiness

vm.swappiness (0–100, default 60) controls how eagerly the kernel moves anonymous memory to swap. At 60, the kernel swaps out idle pages fairly readily to keep more file cache. On a server with plenty of RAM, that is usually the wrong trade: swapping a database's working set to disk to keep cache warm is a latency disaster.

vm.swappiness=10
Enter fullscreen mode Exit fullscreen mode

A value of 10 is the common production starting point — the kernel still swaps under real memory pressure, but it stops swapping when RAM is available. Databases commonly go lower, to 1. A value of 0 does not mean "never swap" on modern kernels; it means swap only to avoid running out of memory. Do not set it to 0 and assume swap is disabled — if you want swap off, disable the swap device itself.

Dirty pages

When a process writes to disk, the kernel does not write immediately. It keeps the data in memory as "dirty" pages and flushes them later. vm.dirty_background_ratio (default 10% of memory) is the point where the kernel starts writing in the background; vm.dirty_ratio (default 20%) is the point where a process writing data is forced to write synchronously — and that synchronous write is a latency spike your users feel.

vm.dirty_background_ratio=5
vm.dirty_ratio=10
Enter fullscreen mode Exit fullscreen mode

Lowering both makes the kernel flush earlier and more often, smoothing out write latency at the cost of slightly more write activity. For latency-sensitive workloads — databases, real-time services — this is one of the highest-value changes you can make. For a backup server that writes huge files sequentially, the defaults may be fine. Measure your workload; the right values depend on it.

Overcommit

vm.overcommit_memory controls how the kernel answers a request for more memory than the system has:

Value Behaviour
0 (default) Heuristic: allow reasonable overcommit, refuse obviously absurd requests
1 Always allow overcommit — never refuse
2 Strict: refuse allocations beyond swap + RAM × vm.overcommit_ratio

Databases and Java services often run with mode 2, because a process that is promised memory it cannot get is a process the OOM killer will eventually visit — and the OOM killer does not ask which process is most important. Strict mode turns a surprise kill into a clean allocation failure at startup, which is far easier to diagnose. But mode 2 must be sized correctly: set vm.overcommit_ratio to match your real workload, or legitimate services will fail to start.

Network Tuning: Connections, Buffers, and File Descriptors

A server that handles thousands of connections hits kernel limits long before it hits CPU limits. These are the settings that matter.

Connection backlog

net.core.somaxconn (default 128) is the maximum backlog of pending connections the kernel will queue for a listening socket. A busy NGINX or application server with bursts of new connections will drop connections once the queue is full — and the client sees a refused connection for no obvious reason.

net.core.somaxconn=4096
net.ipv4.tcp_max_syn_backlog=4096
Enter fullscreen mode Exit fullscreen mode

Set the kernel value and the application's own listen backlog to match. Raising one without the other changes nothing: the lower of the two wins.

TIME_WAIT sockets

Every closed TCP connection sits in TIME_WAIT for a while, holding a socket. On a server making thousands of short outgoing connections — a proxy, an API gateway — TIME_WAIT sockets pile up and exhaust the ephemeral port range.

net.ipv4.tcp_tw_reuse=1
net.ipv4.tcp_fin_timeout=30
Enter fullscreen mode Exit fullscreen mode

tcp_tw_reuse lets the kernel reuse TIME_WAIT sockets for new outgoing connections. tcp_fin_timeout (default 60 seconds) shortens how long a connection waits in FIN-WAIT-2 before closing — 30 seconds is a common production value, 15 for very busy proxies. One warning: tcp_tw_recycle used to be the companion setting, but it was removed in kernel 4.12 because it breaks connections behind NAT. If you find it in an old blog post, do not use it — it no longer exists, and the advice is a decade out of date.

Socket buffers

net.core.rmem_max and net.core.wmem_max (defaults around 212 KB) cap how large a socket's receive and send buffers can grow. On high-bandwidth or high-latency links, small buffers throttle throughput: the sender cannot keep the pipe full.

net.core.rmem_max=16777216
net.core.wmem_max=16777216
net.ipv4.tcp_rmem="4096 87380 16777216"
net.ipv4.tcp_wmem="4096 65536 16777216"
Enter fullscreen mode Exit fullscreen mode

Raise these for servers moving large amounts of data — file servers, media streaming, backup targets. For a small API server, the defaults are fine; bigger buffers are not free, they consume memory per connection.

File descriptors

Every open connection, every open file, is a file descriptor, and the default per-process limit (usually 1024 soft) is far too low for a production server. A web server with 1024 connections open hits "too many open files" and starts failing requests.

# System-wide maximum
fs.file-max=1000000

# Per-process limit — check first
ulimit -n
cat /proc/sys/fs/file-nr
Enter fullscreen mode Exit fullscreen mode

Set the per-process limit where the service actually runs: /etc/security/limits.conf for interactive shells, and LimitNOFILE=65535 in the systemd unit for services. A limit set in the wrong place is the most common reason this fix "doesn't work" — check cat /proc/<pid>/limits for the running process to confirm it took effect.

CPU Tuning: Scheduler, Affinity, and Interrupts

The kernel's default CPU scheduler (CFS, the Completely Fair Scheduler) is a good general-purpose choice, and for most servers you should leave it alone. What you can tune is where work runs.

IRQ affinity matters on multi-queue network cards: by default, all NIC interrupts may land on one CPU, which becomes a bottleneck while the other cores idle. The irqbalance service distributes interrupts across CPUs automatically — make sure it is running. For the last mile of performance, you can pin a NIC queue's interrupts to specific CPUs via /proc/irq/<n>/smp_affinity, but measure first: on most servers, irqbalance is enough, and manual pinning without measurement is guessing.

CPU affinity (taskset) pins a process to specific cores. It helps when cache locality matters — a database worker that keeps its working set in one core's cache — and hurts when it starves a process of idle cores. Use it for known hot processes, not as a blanket policy.

CPU frequency scaling is the silent one: a server in "powersave" governor mode downclocks idle cores, and the first requests after idle pay the ramp-up cost. For latency-sensitive servers, set the governor to performance and verify with cpupower frequency-info. The power saving is real, but so is the tail latency it creates.

Transparent Huge Pages: Turn Them Off for Databases

Transparent Huge Pages (THP) lets the kernel use 2 MB pages instead of 4 KB pages, reducing page-table overhead. It sounds like a free win, and for some workloads it is. For databases, it is a known source of latency spikes: the kernel's background compaction to create huge pages stalls the process at unpredictable moments.

PostgreSQL, MySQL, MongoDB, and Redis all recommend disabling THP, and MongoDB will warn you at startup if it is on.

# Check the current setting
cat /sys/kernel/mm/transparent_hugepage/enabled

# Disable until reboot
echo never | sudo tee /sys/kernel/mm/transparent_hugepage/enabled

# Disable permanently: add to the kernel command line
# transparent_hugepage=never
Enter fullscreen mode Exit fullscreen mode

The permanent fix is a kernel boot parameter or a systemd unit, because the sysfs write does not survive a reboot. This is a setting you change once, verify, and forget — but verify it after every kernel upgrade, since defaults can change.

Measuring Before and After: No Numbers, No Tuning

Tuning without measurement is superstition. Before you change anything, capture a baseline, and capture the same numbers after each change.

# CPU, memory, I/O — one second at a time
vmstat 1

# CPU and memory history
sar -u 1 5
sar -r 1 5

# Disk I/O, per device
iostat -x 1

# Network sockets and listeners
ss -s
ss -tlnp

# Overall load and memory pressure
uptime
free -h
Enter fullscreen mode Exit fullscreen mode

What to look for: vmstat shows swap activity (si/so columns should be near zero on a healthy server) and I/O wait (wa); sar -r shows memory pressure and cache behaviour; iostat -x shows per-disk utilisation and await times; ss -s shows how many sockets sit in TIME_WAIT. Run the same command, under the same load, before and after a change. If the numbers do not move, the change did not help — revert it. A sysctl file full of values nobody measured is technical debt, not tuning.

The Golden Rule: One Change, One Measurement, One Note

This is the rule that keeps tuning from breaking production:

  1. Change one thing. One sysctl value, one setting, one service. If you change five things and performance improves, you do not know which one helped — and you cannot roll back the four that did nothing.
  2. Measure before and after. Same load, same commands, same time window. Write the numbers down.
  3. Document it. In the sysctl file, in the runbook, in the commit message: what you changed, why, what the numbers said. The next engineer — or you, after a holiday — should be able to read why a value is what it is.
  4. Have a rollback. Every change should be reversible in one command: the old value, in the same file, commented out or in the git history. If a change makes things worse at 2 AM, you should not have to think.
  5. Never copy-paste random sysctl values from the internet. A "performance tuning" snippet from a blog post was written for someone else's hardware, kernel, and workload. Values that help a 64-core database server can hurt a 2-core VPS. Understand what a setting does before you set it, test it in staging, and treat any list without measurements as a starting point for experiments — not a recipe.

The Production Checklist

  • Baseline captured: vmstat, sar, iostat, ss before any change.
  • Changes in /etc/sysctl.d/99-performance.conf, version-controlled, commented.
  • Swappiness lowered for servers with enough RAM; databases at 1–10.
  • Dirty ratios lowered for latency-sensitive workloads.
  • somaxconn and application backlog raised together.
  • TIME_WAIT handled with tcp_tw_reuse and a shorter tcp_fin_timeout — never tcp_tw_recycle.
  • File descriptor limits raised where the service runs, verified per-process.
  • THP disabled for databases, verified after reboot.
  • irqbalance running; CPU governor set to performance on latency-sensitive servers.
  • Every change measured, documented, and reversible.

What Hiring Managers Actually Ask

If you are targeting remote DevOps, SRE, or sysadmin roles, kernel tuning is interview material because it tests whether you understand the system underneath the application:

  • "A web server drops connections under load, but CPU is idle — where do you look?" (Answer: connection backlog and file descriptor limits first — somaxconn, the application backlog, ulimit -n — then TIME_WAIT exhaustion with ss -s.)
  • "A database stalls for a few seconds every few minutes. What do you check?" (Answer: dirty page flushing and THP — check vm.dirty_ratio, look for compaction stalls, and confirm THP is disabled. Then look at I/O wait with iostat -x.)
  • "How do you make a sysctl change permanent, and how do you prove it worked?" (Answer: write it to /etc/sysctl.d/, apply with sysctl -p, verify with sysctl <name>, and compare vmstat/sar numbers before and after under the same load.)
  • "Someone pasted a 40-line sysctl tuning snippet into production. What do you do?" (Answer: revert it, then re-apply one setting at a time with measurements. A change you cannot attribute is a change you cannot trust.)

The answer they want is never a list of values. It is the method: measure, change one thing, measure again, document it.

Key Takeaway

Change one thing, measure it, document it — and never copy-paste random sysctl values from the internet. The kernel's defaults are safe; your job is to make them fast, deliberately, with numbers to prove it.


Your turn: Have you ever tuned a kernel setting that made a real difference — or one that made things worse? Which setting, and what did the numbers say? Tell me in the comments.

Top comments (0)