Cloud Networking & SRE··2 min read

Linux Kernel and Sysctl Tuning for 50,000 Concurrent WebSocket and API Connections on Bare-Metal Nodes

Shafikul Islam

Shafikul Islam

Backend & Cloud Infrastructure Engineer · Infrastructure & Cloud Team Lead

Linux Kernel and Sysctl Tuning for 50,000 Concurrent WebSocket and API Connections on Bare-Metal Nodes

When the Linux Kernel Drops Traffic Before Your Application Sees It

We often see teams spend weeks optimizing Go or Rust application handlers, only to watch load tests fail at 5,000 concurrent connections with connect: resource temporarily unavailable or nf_conntrack: table full, dropping packet in dmesg.

Before running high-concurrency WebSocket servers or reverse proxies (Caddy, NGINX, Traefik, Axum), three Linux kernel subsystems must be tuned.


1. File Descriptors and Listen Queue Backlogs

In Linux, every open TCP socket is a file descriptor. The default per-process limit (1024) and listen backlog (somaxconn = 4096) silently truncate burst connection handshakes:

# /etc/sysctl.d/99-high-concurrency.conf
fs.file-max = 2097152
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 65535
net.ipv4.tcp_max_syn_backlog = 65535

And in the systemd service unit (or container securityContext):

LimitNOFILE=1048576

2. Preventing Ephemeral Port and TIME_WAIT Exhaustion on Reverse Proxies

When an ingress gateway proxies thousands of short-lived requests per second to upstream microservices or PgBouncer, each outbound connection consumes a local ephemeral port (source_ip, source_port, dest_ip, dest_port).

By default, Linux only allocates 28,232 ephemeral ports (32768 to 60999) and holds closed sockets in TIME_WAIT for 60 seconds:

net.ipv4.ip_local_port_range = 10240 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 15
net.netfilter.nf_conntrack_max = 1048576

Expanding the ephemeral port range to 55,295 ports and enabling safe timestamp-verified tcp_tw_reuse eliminated outbound port exhaustion under sustained 15,000+ RPS load tests.

Infrastructure & PostgreSQL Advisory

Scaling Infrastructure or Diagnosing P95 Latency?

I help engineering teams diagnose PostgreSQL indexing bottlenecks, concurrency limits, and K3s/Docker deployments. Book a 20-minute call with me to review your setup.

Related Technical Articles