Stop your high-frequency servers from running out of ephemeral ports
Performance Tuning & Kernel Parameters (Sysctl)
Stop your high-frequency servers from running out of ephemeral ports
🧩 The Challenge
Dealing with a production app that hits a wall after a few thousand connections is infuriating, especially when the logs just say “connection refused.” I once spent a whole morning convinced the firewall was acting up, but it turned out the server just ran out of ports to talk to the database.
💡 The Fix
You need to shrink the TCP keepalive time and widen the range of ports the system can use for outgoing connections. It gives your stack more room to breathe during those traffic bursts.
sysctl -w net.ipv4.ip_local_port_range="1024 65535"
sysctl -w net.ipv4.tcp_keepalive_time=600
⚙️ Why It Works
Expanding that port range gives the kernel thousands of extra sockets to juggle instead of recycling the same few hundred constantly. Dropping the keepalive timer also forces the OS to prune dead connections faster so they don’t clog up your table.
🚀 Pro-Tip: Check your current usage with ss -s before you make the change so you actually have a baseline to brag about later.
Linux Tips & Tricks | © ngelinux.com | 8/9/2026
