Every few months someone pastes a 40-line sysctl.conf from an old blog post into a production server "for performance". Half the keys no longer exist, a few were defaults anyway, and one or two quietly make things worse. Modern kernels ship sensible defaults; tuning is worth doing only where a counter shows a real limit being hit.
This guide covers the settings that do matter for typical web, API and database servers, and for each one: the symptom, the counter that proves it, the change, and how to confirm it helped. Commands are for Ubuntu 24.04 and RHEL 9; kernel defaults mentioned are for 5.14 and later.
The method: measure, change one thing, measure again
Record a baseline under representative load: latency percentiles, throughput, error rate, and the specific counter for the setting.
Change one setting.
Repeat the same load and compare.
Persist the change only if the counter and the user-facing metric both improve.
Persist sysctl changes in a dedicated file so they are obvious and reviewable:
sudo tee /etc/sysctl.d/90-app.conf >/dev/null <<'EOF'
# Owner: platform team. Reason and ticket for each line.
net.core.somaxconn = 8192
EOF
sudo sysctl --system # load all sysctl.d files now
sysctl net.core.somaxconn # confirm the running value
File descriptor limits
Symptom: Too many open files (EMFILE) in application logs, or accept4() failed (24: Too many open files) from nginx.
There are three separate limits:
Limit | Scope | Check |
|---|---|---|
| System-wide maximum open files |
|
| Ceiling for any per-process limit |
|
| Per process ( |
|
The system-wide value is already very large on current kernels; it is almost never the problem. The per-process limit is. For a systemd service it comes from the unit, not from /etc/security/limits.conf (that file applies to PAM login sessions only):
# systemctl edit app.service
[Service]
LimitNOFILE=65536
Check the value the running process actually got via /proc/<pid>/limits after a restart. Our earlier post on LimitNOFILE covers this case in depth. Raising the limit hides a leak if descriptors grow without bound, so also watch the count (ls /proc/<pid>/fd | wc -l) over time.
Listen backlog: somaxconn
Symptom: under bursts, clients see connection timeouts or resets while the server's CPU is not saturated.
The counters that prove it:
nstat -az TcpExtListenOverflows TcpExtListenDrops
ss -ltn 'sport = :443'
For listening sockets, ss shows Recv-Q (connections completed and waiting for accept()) and Send-Q (the backlog limit). If Recv-Q reaches Send-Q and ListenOverflows increases during bursts, the accept queue is overflowing.
net.core.somaxconn caps the backlog any application may request. Its default is 4096 since kernel 5.4 (it was 128 before, which is where most old advice comes from). The application must also ask for a larger backlog, because the effective value is the smaller of the two:
listen 443 ssl backlog=8192;
Raise both together, then re-run the burst test and confirm ListenOverflows stays flat. Often the real fix is that the application accepts too slowly (a blocked event loop or too few workers), and a bigger queue only adds latency. Check that before tuning.
Ephemeral ports for heavy outbound clients
Symptom: a proxy or service making many outbound connections fails with Cannot assign requested address (EADDRNOTAVAIL).
sysctl net.ipv4.ip_local_port_range # default 32768 60999
ss -tan state time-wait | wc -l
The range gives about 28,000 ports per destination address and port. Widening it (1024 65535, minus any ports your services listen on) helps, but the better fix is usually connection reuse: HTTP keep-alive, upstream keepalive pools in nginx, and database connection pools. Leave net.ipv4.tcp_tw_reuse at its default unless you have measured a TIME-WAIT problem on the client side; never use the long-removed tcp_tw_recycle from old guides.
Swappiness
Symptom: latency spikes on a database or cache server while memory is not exhausted, with pages being swapped out.
sysctl vm.swappiness # default 60
vmstat 1 # si/so columns: pages swapped in/out per second
grep -E 'Swap(Total|Free)' /proc/meminfo
vm.swappiness (0 to 200) tells the kernel how willing it is to reclaim anonymous memory by swapping, relative to dropping page cache. On a dedicated database host where the database manages its own cache, a lower value such as 10 keeps its memory resident and lets the kernel drop file cache first. Do not set 0 expecting "no swap"; it means swap only to avoid an out-of-memory condition, which can make the OOM killer act sooner. If you never want swap, size memory properly and disable swap explicitly, knowing the trade-off.
Confirm by watching so in vmstat and p99 latency during the same load.
Dirty page writeback
Symptom: periodic multi-second stalls on write-heavy hosts as large amounts of buffered data flush at once.
sysctl vm.dirty_ratio vm.dirty_background_ratio
grep -E '^(Dirty|Writeback):' /proc/meminfo
Background writeback starts at dirty_background_ratio (default 10% of available memory) and writers are throttled at dirty_ratio (default 20%). On machines with a lot of RAM those percentages are tens of gigabytes. The byte-based equivalents (vm.dirty_background_bytes, vm.dirty_bytes) give tighter control, for example 256 MB and 1 GB, so flushes are smaller and more frequent. Setting a _bytes value zeroes the matching _ratio. Test with your write pattern; databases with their own write-ahead log and fsync discipline usually care less.
Transparent huge pages
THP lets the kernel back memory with 2 MB pages automatically, reducing TLB misses. It helps many workloads and hurts some, mostly through latency spikes during compaction or memory bloat in allocators that do not expect it.
cat /sys/kernel/mm/transparent_hugepage/enabled # [always] madvise never
cat /sys/kernel/mm/transparent_hugepage/defrag
grep -E 'AnonHugePages' /proc/meminfo
madvise is a good middle ground: only applications that ask for huge pages get them. Database vendors' advice differs and has changed over time, so follow your database version's current production notes rather than a general rule. Redis, for example, logs a warning at start-up when THP is set to always, and MongoDB 8.0 reversed its long-standing recommendation to disable THP. Set THP through a kernel parameter (transparent_hugepage=madvise) or a tuned profile, not a boot script, so the setting is applied before services start.
tuned profiles on RHEL (and Ubuntu)
tuned applies a coherent set of sysctl, CPU governor, disk and THP settings for a workload type. It is installed and active on RHEL; on Ubuntu install the tuned package.
tuned-adm active
tuned-adm recommend
tuned-adm list
sudo tuned-adm profile throughput-performance
Profiles such as throughput-performance, latency-performance, network-latency and virtual-guest encode vendor-tested combinations. Start from a profile and add a small custom one for your own overrides rather than mixing a profile with a pile of manual sysctls that it may overwrite:
# /etc/tuned/app-server/tuned.conf
[main]
include=throughput-performance
[sysctl]
net.core.somaxconn=8192
vm.swappiness=10
sudo tuned-adm profile app-server
tuned-adm verify
tuned-adm verify reports whether the current system state still matches the profile, which catches drift.
Settings to leave alone
Setting | Why |
|---|---|
| Removed in kernel 4.12; broke clients behind NAT |
TCP buffer sizes ( | Autotuning handles most links; tune only for measured high-bandwidth, high-latency paths |
| Breaks software that reserves large address space; use only with a plan |
| Many were moved or removed; profile first |
| Default already very high; per-process limit is what matters |
Key takeaways
Tune only after a counter proves a limit is being hit; change one thing at a time.
File descriptor problems are almost always the per-process limit, set in the systemd unit.
somaxconnand the application's own backlog work together; checkListenOverflows.Lower swappiness on dedicated database hosts; never treat 0 as "swap off".
Prefer a tuned profile with small, documented overrides over a pasted
sysctl.conf.
References
Kernel sysctl documentation (vm, net, fs): https://docs.kernel.org/admin-guide/sysctl/index.html
IP sysctl reference: https://docs.kernel.org/networking/ip-sysctl.html
Transparent Hugepage Support: https://docs.kernel.org/admin-guide/mm/transhuge.html
RHEL 9 Monitoring and managing system status and performance (tuned): https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/9/html/monitoring_and_managing_system_status_and_performance/index
systemd.execresource limits: https://www.freedesktop.org/software/systemd/man/latest/systemd.exec.html
Comments
0No comments yet. Be the first to comment!
Sign In to post a comment.