Reuse those TCP sockets faster

Performance Tuning & Kernel Parameters (Sysctl)

Reuse those TCP sockets faster

🧩 The Challenge

You’ve got a busy web server, hundreds or thousands of connections per second, and suddenly things just… slow down. Or new connections can’t even get through. You check netstat and see thousands of TIME_WAIT entries hogging all your ephemeral ports.

💡 The Fix

There’s a kernel parameter that lets your system reuse those sockets much faster, cutting down on the TIME_WAIT backlog. It makes a huge difference on systems handling a lot of short-lived connections, keeping things snappy.

sysctl -w net.ipv4.tcp_tw_reuse=1
echo "net.ipv4.tcp_tw_reuse = 1" > /etc/sysctl.d/99-tcp-reuse.conf
sysctl -p /etc/sysctl.d/99-tcp-reuse.conf

⚙️ Why It Works

Linux normally keeps closed TCP connections in a TIME_WAIT state for a while, just in case any stray packets show up. This parameter tells the kernel it’s okay to reuse those sockets for new outbound connections before the full TIME_WAIT period expires, as long as timestamps make sense.

🚀 Pro-Tip: Just be careful with tcp_tw_recycle; that one’s usually a bad idea behind NAT. Stick with tcp_tw_reuse.

Linux Tips & Tricks | © ngelinux.com | 11/25/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted