Operations 12 min read

Boost Linux Network Performance: Practical TCP/IP Stack Tuning Guide

This guide explains why network performance becomes a bottleneck in high‑traffic Linux servers and provides a step‑by‑step set of sysctl tweaks, buffer adjustments, congestion‑control choices, interrupt balancing, and monitoring scripts to dramatically improve throughput and latency.

Raymond Ops
Raymond Ops
Raymond Ops
Boost Linux Network Performance: Practical TCP/IP Stack Tuning Guide

Why Network Performance Matters

In high‑concurrency, high‑traffic environments, the default Linux TCP/IP stack often limits throughput, causing excessive TIME_WAIT sockets, high latency, and unstable response times. Proper tuning of kernel parameters can eliminate these bottlenecks.

Core TCP Stack Optimizations

1. TCP Connection Management

# /etc/sysctl.conf
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 30000
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_max_tw_buckets = 10000
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_intvl = 15

2. TCP Buffer Tuning

# Buffer sizes
net.core.rmem_default = 262144
net.core.rmem_max = 16777216
net.core.wmem_default = 262144
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_window_scaling = 1

3. TCP Congestion Control

# Choose congestion algorithm
net.ipv4.tcp_congestion_control = bbr
net.ipv4.tcp_frto = 2
net.ipv4.tcp_dsack = 1
net.ipv4.tcp_fack = 1
net.ipv4.tcp_slow_start_after_idle = 0

IP Stack Parameter Optimizations

1. IP Layer Tweaks

# Disable IP forwarding on non‑router hosts
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.rp_filter = 1
# Fragmentation thresholds
net.ipv4.ipfrag_high_thresh = 262144
net.ipv4.ipfrag_low_thresh = 196608
net.ipv4.ipfrag_time = 30
# ICMP improvements
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1

2. Port Range and UDP

# Expand local port range
net.ipv4.ip_local_port_range = 1024 65535
# UDP memory limits
net.ipv4.udp_mem = 94500000 915000000 927000000
net.ipv4.udp_rmem_min = 8192
net.ipv4.udp_wmem_min = 8192

Network Queue and Interrupt Optimizations

1. Device Queue Settings

# Increase device processing budget
echo '4096' > /proc/sys/net/core/netdev_budget
echo '2' > /proc/sys/net/core/netdev_budget_usecs
# RPS/RFS for multi‑core balancing
echo 'f' > /sys/class/net/eth0/queues/rx-0/rps_cpus

2. Interrupt Balancing Script

#!/bin/bash
# network_irq_balance.sh
IRQ_LIST=$(grep eth0 /proc/interrupts | awk -F: '{print $1}' | xargs)
CPU_COUNT=$(nproc)
i=0
for irq in $IRQ_LIST; do
    cpu_mask=$((1 << (i % CPU_COUNT)))
    printf "%x" $cpu_mask > /proc/irq/$irq/smp_affinity
    echo "IRQ $irq -> CPU $((i % CPU_COUNT))"
    ((i++))
done

High‑Concurrency Scenario Optimizations

1. Large Connection Count

# File descriptor limits
* soft nofile 1048576
* hard nofile 1048576
# Process limits
* soft nproc 1048576
* hard nproc 1048576
# systemd limits
DefaultLimitNOFILE=1048576
DefaultLimitNPROC=1048576

2. Memory Management

# Virtual memory tweaks
vm.swappiness = 10
vm.dirty_ratio = 15
vm.dirty_background_ratio = 5
vm.overcommit_memory = 1

Performance Monitoring & Validation

1. Key Metrics Script

#!/bin/bash
# network_monitor.sh
echo "=== Network Connection Summary ==="
ss -s

echo -e "
=== TCP State Distribution ==="
ss -tan | awk 'NR>1{state[$1]++} END{for(i in state) print i, state[i]}'

echo -e "
=== Network Throughput ==="
sar -n DEV 1 1 | grep -E "eth0|Average"

echo -e "
=== Memory Usage ==="
free -h

echo -e "
=== System Load ==="
uptime

2. Stress Test Commands

# HTTP load test with wrk
wrk -t12 -c400 -d30s --latency http://your-server-ip/
# Bandwidth test with iperf3
iperf3 -s   # server
iperf3 -c server-ip -t 60 -P 10   # client
# TCP connection test with ab
ab -n 100000 -c 1000 http://your-server-ip/

Real‑World Case Study: E‑Commerce System

After applying the above tuning, the e‑commerce platform saw a 200% increase in QPS (from 15 k to 45 k), average latency dropped from 120 ms to 35 ms, 99th‑percentile latency improved by 71%, concurrent connections grew fourfold, and CPU usage fell from 85% to 45%.

Key Optimization Highlights

BBR Congestion Control : Boosted throughput by ~40%.

TCP Buffer Tuning : Significantly reduced latency jitter.

Connection Reuse : TIME_WAIT sockets cut by 90%.

Interrupt Balancing : Better multi‑core CPU utilization.

Best‑Practice Recommendations

1. Scenario‑Specific Strategies

High‑Concurrency Web Servers

net.ipv4.tcp_tw_reuse = 1
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535

Large File Transfer Servers

net.core.rmem_max = 134217728
net.core.wmem_max = 134217728
net.ipv4.tcp_window_scaling = 1

Database Servers

net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_retries2 = 5

2. Production Deployment Workflow

Test Environment Validation : Apply settings in a staging environment first.

Canary Release : Deploy to a subset of servers.

Monitoring : Observe key metrics closely.

Full Rollout : Deploy to all servers after verification.

3. Configuration Persistence

# Apply all sysctl settings
sysctl -p
# Verify critical parameters
sysctl net.ipv4.tcp_congestion_control
sysctl net.core.somaxconn
# Ensure settings survive reboot
echo 'sysctl -p' >> /etc/rc.local
chmod +x /etc/rc.local

Precautions & Common Pitfalls

1. Tuning Missteps

Blindly increasing buffers : May exhaust memory.

Over‑optimizing TIME_WAIT : Can lead to port exhaustion.

Ignoring workload characteristics : Different services need different parameter sets.

2. Rollback Plan

# Backup current sysctl.conf
cp /etc/sysctl.conf /etc/sysctl.conf.backup.$(date +%Y%m%d)
# Quick rollback script
cat > /root/network_rollback.sh <<'EOF'
#!/bin/bash
cp /etc/sysctl.conf.backup.* /etc/sysctl.conf
sysctl -p
echo "Network config rollback completed!"
EOF
chmod +x /root/network_rollback.sh

Conclusion

Systematic TCP/IP stack tuning can dramatically improve Linux server network performance. The essential steps are to understand the specific workload, apply incremental changes, continuously monitor metrics, and validate each adjustment with thorough testing.

TCPLinuxSysctlNetwork Tuning
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.