Skip to content

TCP backlog tuning for high-connection servers

Servers under burst load drop new connections silently when the TCP backlog is too small. Here is how to size somaxconn, tcp_max_syn_backlog, and listen() correctly.

A web server that handles 1,000 requests per second on a quiet afternoon will not gracefully handle 5,000 connection attempts in the same second. The bottleneck is not CPU or memory — it is the kernel’s per-socket queue of half-open and accepted connections waiting for accept(). When that queue overflows, the kernel drops SYNs and the client sees a connection timeout.

What’s happening

Every listening socket has two queues:

  • The SYN queue (tcp_max_syn_backlog), holding half-open connections that received a SYN but not yet the final ACK of the handshake.
  • The accept queue (somaxconn, plus the backlog argument the application passes to listen()), holding fully established connections waiting for the application to accept them.

The smaller of somaxconn and the application’s backlog argument wins. Most distributions ship somaxconn=4096 (recent kernels) or 128 (older defaults). Nginx defaults to listen ... backlog=511. PHP-FPM defaults to listen.backlog = 511. Under a burst, both queues fill, the kernel drops new SYNs, and TCP retransmission delays surface as multi-second connection times on the client side.

How to verify

Look at the per-socket drop counters:

ss -ltnp
ss -ltn '( sport = :443 )'
nstat -az | grep -iE 'listendrops|listenoverflows'

ListenOverflows is the count of accept-queue overflows since boot. ListenDrops includes both overflows and other listen-time drops. A counter that increments during a traffic spike — and zero during quiet periods — is the diagnostic signature.

While reproducing the load:

ss -ltn | awk 'NR>1 {print $2, $3, $5}'

The Recv-Q column on a listening socket is the current accept-queue depth. The Send-Q is the configured maximum. If Recv-Q is sitting at or near Send-Q, the application is not calling accept() fast enough — or the backlog is too small for the burst.

For SYN-side pressure:

nstat -az | grep -iE 'tcpext.*syncookies|tcpsynretrans'

A surge in SyncookiesSent means the SYN queue overflowed and the kernel fell back to SYN cookies. The connection will still complete, but at a cost.

The fix

  1. Raise net.core.somaxconn to at least 4096, often 16384 for high-connection front-ends, in /etc/sysctl.d/99-network.conf.
  2. Raise net.ipv4.tcp_max_syn_backlog to a comparable value (4096–16384). Apply with sysctl --system.
  3. Update the application’s listen backlog to match. For nginx: listen 443 ssl backlog=4096;. For PHP-FPM: listen.backlog = 4096. For systemd socket units: Backlog=4096. Restart the service — these are only read at socket creation.
  4. If the application cannot accept connections fast enough, the queue size is treating a symptom. Investigate worker pool size and request duration first.

After the change, ListenOverflows should stay at zero through traffic peaks, and client-side connection timeouts on bursts disappear.

If you are sizing this for a real workload and want a second opinion, that is exactly the kind of thing Cloud Compute ships with — reach out.