VPS Kernel Parameters for High-Performance Web Servers: sysctl Tuning Deep Dive

The Linux kernel’s default sysctl settings are designed for general-purpose workloads, not high-traffic web servers. Tuning these parameters can yield significant latency reductions and throughput improvements on VPS instances without any hardware changes.

Network Stack Optimization

The network stack is the most impactful area for kernel tuning on a web server VPS. Apply these settings in /etc/sysctl.d/99-network-performance.conf:

# Increase TCP buffer sizes for throughput
net.core.rmem_max = 134217728
net.core.wmem_max = 134217728
net.ipv4.tcp_rmem = 4096 87380 134217728
net.ipv4.tcp_wmem = 4096 65536 134217728

# Enable TCP window scaling
net.ipv4.tcp_window_scaling = 1

# Enable fast socket recycling
net.ipv4.tcp_tw_reuse = 1

# Increase backlog queue
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535

# Reduce TIME_WAIT socket duration
net.ipv4.tcp_fin_timeout = 15

# Enable TCP fast open (TFO)
net.ipv4.tcp_fastopen = 3

# Increase ephemeral port range
net.ipv4.ip_local_port_range = 1024 65535

Key tunings explained:

  • Buffer sizes: Setting rmem_max and wmem_max to 128 MB allows the kernel to dynamically scale buffers for high-latency connections, critical for serving international audiences.
  • tcp_tw_reuse: Allows reuse of TIME_WAIT sockets for new connections. Essential for high-throughput web servers that create many short-lived connections.
  • tcp_fastopen: Saves one round trip during connection establishment. Set to 3 (enable both client and server side) for maximum benefit.
  • somaxconn: Increases the listen backlog. Match this to your Nginx/Apache listen.backlog setting.

Virtual Memory Tuning

Memory management settings directly affect swap behavior and OOM handling:

# Reduce swap tendency
vm.swappiness = 10

# Increase dirty page cache thresholds
vm.dirty_ratio = 30
vm.dirty_background_ratio = 5

# Reduce vfs cache pressure
vm.vfs_cache_pressure = 50

# Enable memory compaction
vm.compact_memory = 1

# Reduce OOM killer tendency
vm.overcommit_memory = 1
vm.overcommit_ratio = 50

Why these matter on a VPS: Setting swappiness=10 tells the kernel to avoid swapping until absolutely necessary, keeping your application’s working set in RAM. The dirty page thresholds (dirty_ratio, dirty_background_ratio) control how much data accumulates in memory before being written to disk, reducing write amplification on SSDs.

Filesystem Performance

For VPS instances using ext4 or XFS filesystems:

# Increase inotify watches
fs.inotify.max_user_watches = 524288

# Increase file handles
fs.file-max = 200000

# Enable AIO for async I/O
fs.aio-max-nr = 1048576

# Increase epoll limits
fs.epoll.max_user_watches = 1048576

Applications with many files (CMS file watchers, CI/CD agents) benefit from the increased inotify.max_user_watches. The file-max setting prevents out-of-file-handle errors during traffic spikes.

Applied Tuning and Verification

Apply all settings and verify they took effect:

# Apply changes
sudo sysctl --system

# Verify specific parameter
sysctl net.ipv4.tcp_fastopen

# Check network performance before/after
# Simple latency test
ping -c 10 target-server

# TCP throughput test (requires iperf3 on both ends)
iperf3 -c target-server -t 30

Always test these settings in a staging environment first. Incorrect network buffer settings can cause memory exhaustion on low-RAM VPS. Start with conservative values and increase incrementally while monitoring free memory with free -m.

For comprehensive VPS performance tuning covering all layers of the stack, explore our VPS optimization resources.

Leave a Reply