Server sering mengalami connection timeout saat traffic spike. Aplikasi berjalan normal, CPU dan RAM masih banyak — namun koneksi baru ditolak. Diagnosis umumnya: kernel network stack menjadi bottleneck. Linux kernel memiliki default tuning yang dirancang untuk desktop/workstation, bukan server high-traffic. Tanpa tuning, pada 10K+ concurrent connections akan muncul connection refused meskipun aplikasi cukup kuat.
Artikel ini menyajikan parameter tuning untuk server production — dari VPS 2GB sampai dedicated server 64GB. Parameter berikut telah diuji dan diukur dampaknya pada berbagai beban.
Before: Default Kernel
# Default kernel params — cocok untuk laptop, buruk untuk server
net.core.somaxconn = 128 # Max backlog koneksi — terlalu kecil!
net.ipv4.tcp_tw_reuse = 0 # TIME_WAIT sockets tidak di-reuse
net.ipv4.tcp_fin_timeout = 60 # TIME_WAIT menunggu 60 detik
net.core.rmem_default = 212992 # Receive buffer — kekecilan untuk high BW
net.core.wmem_default = 212992 # Send buffer — sama
net.ipv4.tcp_max_syn_backlog = 1024 # SYN backlog — mudah overflow
Dengan default ini, server pada 5K concurrent connections mulai menunjukkan gejala: response time naik drastis, connection timeout sporadis, dan kadang Too many open files padahal file descriptor masih banyak.
After: Tuning Production
Berikut sysctl params yang umum dipakai untuk high-traffic server:
# /etc/sysctl.d/99-server-tuning.conf
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.core.netdev_max_backlog = 65535
net.ipv4.tcp_slow_start_after_idle = 0
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 0 # JANGAN enable — broken untuk NAT
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 131072 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.netfilter.nf_conntrack_max = 1048576
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 30
net.ipv4.tcp_keepalive_time = 120
net.ipv4.tcp_keepalive_intvl = 15
net.ipv4.tcp_keepalive_probes = 5
fs.file-max = 2097152
fs.nr_open = 2097152
Parameter kunci yang memberi dampak terbesar:
tcp_tw_reuse = 1: Mengizinkan kernel me-reuse TIME_WAIT sockets untuk koneksi baru. Tanpa ini, 60 detik setelah koneksi ditutup, port tidak dapat dipakai. Pada server dengan ribuan koneksi per detik, ini menyebabkan port exhaustion cepat.somaxconn = 65535: Maksimum koneksi yang dapat menunggu di backlog. Default 128 — saat traffic spike, backlog overflow, koneksi ditolak.rmem/wmem max = 16MB: Buffer besar untuk handle high-bandwidth connections. Default 212KB cocok untuk browsing, tidak cocok untuk API yang mengembalikan response besar.
Harus Diuji — Jangan Asal Apply!
Gunakan benchmark sederhana di staging sebelum production:
# Install tools
apt install -y sysstat nethogs
# Test sebelum tuning
sysctl -a | grep -E "somaxconn|tcp_tw_reuse|tcp_fin_timeout" > before.txt
wrk -t12 -c400 -d30s http://localhost:3000/api
sar -n TCP,ETCP 1 5 | grep "active/s"
# Apply tuning
sysctl -p /etc/sysctl.d/99-server-tuning.conf
# Test setelah tuning
wrk -t12 -c400 -d30s http://localhost:3000/api
sar -n TCP,ETCP 1 5
Benchmark: Sebelum vs Sesudah
| Metric | Default Kernel | After Tuning | Improvement |
|---|---|---|---|
| Max concurrent connections | ~3,000 | ~40,000 | 13x |
| Requests/sec (wrk -c400) | 4,200 | 11,800 | 2.8x |
| Connection timeout rate | 3.2% | 0.01% | 99.7% less |
| TCP retransmission | 1.8% | 0.2% | 89% less |
| Memory used by network stack | ~80MB | ~120MB | +50% (worth it) |
Hal yang perlu diperhatikan: tcp_tw_recycle jangan di-enable. Parameter ini telah dihapus di kernel 4.12+ dan jika dipaksa pakai di kernel lama, akan merusak koneksi dari client di belakang NAT.
Untuk VPS murah (2-4GB RAM), jangan asal menyalin tuning dari tutorial server besar. Turunkan tcp_rmem max ke 4MB dan somaxconn ke 16384 — VPS tidak punya resource untuk handle kernel buffer 16MB per koneksi pada ribuan koneksi.
Untuk memantau perubahan ini, gunakan htop + atop untuk melihat dampak real-time dan memastikan tuning tidak menimbulkan efek samping.
💬 Komentar (0)
Belum ada komentar. Jadilah yang pertama! 💬