Tune net.core.somaxconn: Listen Backlog and ss Recv-Q/Send-Q
Before raising net.core.somaxconn, read ss -ltn Recv-Q/Send-Q and nstat ListenOverflows to see if the accept queue overflows. Raise the app backlog too.
net.core.somaxconn is only a cap on the backlog an application passes to listen(), so raising it alone often changes nothing. First read Send-Q (the backlog in effect) and Recv-Q (connections waiting for accept) in ss -ltn, plus TcpExtListenOverflows in nstat. If the queue really overflows, raise the kernel cap and the application backlog together.
[1] Check the current values. Per the kernel documentation the defaults are somaxconn 4096 (128 before Linux 5.4), ip_local_port_range 32768–60999 and tcp_syncookies 1. tcp_max_syn_backlog has no fixed default: it scales with memory, minimum 128.
$ sysctl net.core.somaxconn net.ipv4.tcp_max_syn_backlog net.ipv4.ip_local_port_range net.ipv4.tcp_tw_reuse net.ipv4.tcp_syncookiesnet.core.somaxconn = 4096net.ipv4.tcp_max_syn_backlog = 512net.ipv4.ip_local_port_range = 32768 60999net.ipv4.tcp_tw_reuse = 2net.ipv4.tcp_syncookies = 1These are two different queues. As listen(2) explains, backlog is the length of the queue of fully established connections waiting to be accepted. Half-open connections (SYN_RECV) are capped separately by tcp_max_syn_backlog. A backlog larger than somaxconn is silently capped — no error.
[2] Read the backlog actually applied per socket with ss -ltn. On a LISTEN line, Send-Q is the backlog in effect and Recv-Q is the number of connections waiting for accept. An app that calls listen(10000) with somaxconn at 4096 shows this:
$ ss -ltn 'sport = :18080'State Recv-Q Send-Q Local Address:Port Peer Address:PortLISTEN 0 4096 0.0.0.0:18080 0.0.0.0:*Run the same program in a namespace where somaxconn is 128 and Send-Q reads 128. Trust this column, not the number in the app's config file.
[3] Check whether the queue overflows. A server listening with backlog 2 that never calls accept, hit with six connections, filled Recv-Q past the backlog, and the SYNs it could not take showed up as counters:
$ ss -ltn 'sport = :18081'State Recv-Q Send-Q Local Address:Port Peer Address:PortLISTEN 3 2 0.0.0.0:18081 0.0.0.0:*$ nstat -az TcpExtListenOverflows TcpExtListenDrops#kernelTcpExtListenOverflows 9 0.0TcpExtListenDrops 9 0.0The kernel documentation defines ListenOverflows as SYNs dropped because the accept queue was full. Clients retransmit the SYN, so it climbs faster than the number of connections. On a production box, sample nstat a few minutes apart and look for growth; a nonzero lifetime total alone says little. When Recv-Q sits at Send-Q and this counter keeps rising, it is time to tune. If it stays at 0 over the window you watched, that only tells you no accept-queue overflow happened in that window. If things are still slow, look at the Recv-Q trend and client-side latency together with why the app accepts slowly (too few workers, a blocked event loop).
[4] Check the application's backlog too. nginx uses 511 on Linux when listen has no backlog= (official docs). Here is nginx with listen 8080; and listen 8081 backlog=8192;:
State Recv-Q Send-Q Local Address:Port Peer Address:PortLISTEN 0 511 0.0.0.0:8080 0.0.0.0:*LISTEN 0 4096 0.0.0.0:8081 0.0.0.0:*Port 8081 asked for 8192 and got capped at somaxconn 4096. Raise only the kernel cap and nginx stays at 511; raise only backlog= and it stops at somaxconn. Other servers have their own backlog settings — confirm with Send-Q.
[5] Change it at runtime and confirm it took. Before changing anything, save the current values and the list of existing config files — you will need them to revert:
$ sysctl net.core.somaxconn net.ipv4.tcp_max_syn_backlog | tee ~/sysctl-backlog.before$ ls /etc/sysctl.d/Change only the queue you saw overflow in [3]. An accept-queue overflow calls for somaxconn plus the app's backlog; touch tcp_max_syn_backlog only if you have separately confirmed the SYN_RECV queue is short. The kernel docs give no recommended value, and 8192 below is an example to show the behavior — pick your number from how far Recv-Q was filling up. Bigger queues cost memory for every waiting connection (the kernel docs put a SYN_RECV request socket at about 304 bytes).
$ sudo sysctl -w net.core.somaxconn=8192sysctl -w does not survive a reboot. Sockets that are already listening keep their backlog, so restart the app (for nginx, after editing backlog=) to open new sockets, then check Send-Q again. Connections to that port can drop during the restart, so do it at a quiet time, or first check the service's docs for a way to reopen sockets without dropping connections.
Once the runtime change shows the Send-Q and counters you expected, persist it with a file in /etc/sysctl.d/. sysctl.d(5) suggests names in the 60–90 range for /etc, and for the same key the file that sorts later wins.
net.core.somaxconn = 8192If you decided to raise the SYN_RECV queue as well, add a net.ipv4.tcp_max_syn_backlog = <your value> line to the same file.
$ sudo sysctl --system$ sysctl net.core.somaxconn[6] Revert. Deleting the file does not reset the live value, so write back the values you saved in ~/sysctl-backlog.before in [5].
$ sudo rm /etc/sysctl.d/60-net-backlog.conf$ sudo sysctl -w net.core.somaxconn=4096$ sudo sysctl -w net.ipv4.tcp_max_syn_backlog=5124096 and 512 are the original values on the test machine — use the ones you saved. Skip the second line if you never changed tcp_max_syn_backlog. Restart the app afterwards so its sockets pick up the old backlog.
Common misconceptions
| Belief | Reality |
|---|---|
Raising somaxconn is enough | If the app backlog stays the same, so does Send-Q ([4]) |
Lots of TIME_WAIT, so enable tcp_tw_recycle | That knob is gone on this kernel (6.8); /proc/sys/net/ipv4/ only has tcp_tw_reuse |
tcp_tw_reuse must be 1 | The default 2 reuses TIME_WAIT sockets for new connections on loopback only; 1 enables it globally. Neither has anything to do with the backlog |
| Out of ports, so raise the backlog | Outgoing connections pick a local port from ip_local_port_range (default 32768–60999). Unrelated to the backlog |
| Changing it in a container changes the host | Per Docker docs, docker run --sysctl only sets namespaced sysctls (net.*) in that container and refuses ones that would modify the host |
Connection-count limits are covered elsewhere: nginx in worker_connections are not enough, Apache in MaxRequestWorkers reached.