Trace connection bottlenecks through TCP listen queues

Trace connection bottlenecks through TCP listen queues

Trace connection bottlenecks through TCP listen queues

When an API starts timing out on connections during a traffic spike, even though average CPU utilization is low and outbound bandwidth is not saturated, first determine where connections are getting stuck rather than immediately increasing bandwidth. The TCP handshake, the application accepting a connection, and request processing are three separate stages. A backlog at any of them can make requests appear slow.

VPSAMD’s EPYC 7543P dedicated server provides 32 cores and 64 threads on a single socket, but a high core count does not guarantee that an application calls accept() promptly. This article uses Debian 12 and the iproute2 tools available from its system repositories to build an evidence trail. Run the commands in the network namespace hosting the service you are investigating.

1. Distinguish the two listen queues first

The SYN queue holds connection requests that have not yet completed the handshake. The accept queue holds connections that have completed the handshake and are waiting for the application to accept them. After acceptance, requests may still wait for a worker process, a database connection pool, or a downstream response. Those waits are outside the listen queues.

For LISTEN sockets in ss -lnt, Recv-Q is the number of established connections waiting to be accepted, while Send-Q is the listen backlog limit. For non-listening sockets, these columns generally represent byte counts, so do not interpret them in the same way.

Enlarging a queue only creates more room to wait; it does not increase the application’s processing capacity. If overflows fall while response times rise, failures may simply have been replaced by longer queueing delays.

Also distinguish client connection timeouts from response read timeouts. If the handshake has completed and the service has already accepted the connection, investigate queueing inside the application rather than continuing to tune SYN parameters.

2. Collect queue data and counter deltas

List all listening addresses first, then select the target port. The examples below use 18080 for the service port under investigation. Replace it with the actual configured port before running the commands; it is not a default service port.

ss -lntp
ss -lntp 'sport = :18080'
ss -nt state syn-recv 'sport = :18080'
nstat -az TcpExtListenOverflows TcpExtListenDrops TcpExtSyncookiesSent

Viewing process information may require administrator privileges. Record the address, port, process, and network namespace so that you do not mistake a reverse proxy’s listening port for the backend application’s port. With SO_REUSEPORT enabled, several sockets may share a port; inspect each row separately.

nstat -az reports absolute counter values and includes entries whose values are zero. Take two samples at an interval and focus on the differences. Do not treat cumulative counts since system startup as evidence of the current incident.

nstat -az TcpExtListenOverflows TcpExtListenDrops TcpExtSyncookiesSent \
  > /tmp/tcp-before.txt
sleep 10
nstat -az TcpExtListenOverflows TcpExtListenDrops TcpExtSyncookiesSent \
  > /tmp/tcp-after.txt
diff -u /tmp/tcp-before.txt /tmp/tcp-after.txt

The ten seconds here are simply the sampling interval. Infrequent bursts may require a longer observation window, while frequent bursts call for more closely spaced queue samples. Counters belong to the current network namespace and are not broken down by port. In a shared environment, check activity from other services at the same time.

3. Locate the backlog by combining signals

The accept queue is close to its limit

If Recv-Q remains close to Send-Q and ListenOverflows increases, the accept queue is under pressure. Next, check whether the application accepts connections promptly, whether worker processes are blocked, and whether the process has reached a file descriptor limit.

Low average CPU utilization across the server does not rule out a bottleneck: a single event loop or worker process may already be saturated. Examine per-thread CPU usage, application connection counts, processing times, and database connection pool wait times.

ListenDrops is not specific to accept queue overflows; it can also reflect drops at other points in the listening path. A conclusion is more reliable when queue observations, counters, and request errors all line up.

Handshakes are backing up, but the accept queue is not full

If the number of SYN-RECV connections rises significantly while the accept queue still has room, investigate the handshake path, client retries, and abnormal connection traffic. When SYN cookies are enabled, some requests do not retain the usual half-open connection state. A low SYN-RECV count alone therefore does not prove that the handshake stage is free of pressure.

An increase in SyncookiesSent shows that the system has sent cookies, but does not prove an attack. Legitimate traffic bursts can trigger them too. Assess the traffic sources alongside the volume of application requests.

If neither queue is under pressure, continue checking routing, firewalls, TLS handling after connection establishment, and application responses. Do not change kernel parameters based on a single cumulative counter.

4. Adjust both the application and kernel settings

Linux caps the application’s requested listen backlog at net.core.somaxconn. Raising only the kernel limit will not increase the effective queue capacity if the application still requests a small backlog.

Record the current value first. The 4096 below is only an example used to demonstrate the change procedure, not a recommended value for every workload:

sysctl -n net.core.somaxconn > /tmp/somaxconn.previous
sudo sysctl -w net.core.somaxconn=4096
ss -lnt 'sport = :18080'

Then adjust the backlog through the application’s own configuration or startup options, and recreate the listening socket using that application’s procedure. Existing sockets generally do not adopt the new limit automatically. Editing a configuration file alone does not mean the change has taken effect.

If systemd socket activation creates the listening socket, check Backlog= in the corresponding socket unit rather than looking only for application options. Before any restart or operation that recreates a listening socket, establish what it will affect.

net.ipv4.tcp_max_syn_backlog controls requests that have not completed the handshake; it is not the same setting as the accept queue for established connections. When the accept queue overflows, do not increase several TCP parameters at once, or it will be difficult to attribute the results of the retest to a particular change.

To roll back, restore the application’s original settings and recreate the listening socket, then restore the kernel limit and verify it:

old=$(cat /tmp/somaxconn.previous)
sudo sysctl -w "net.core.somaxconn=$old"
ss -lnt 'sport = :18080'

Only after the temporary adjustment has passed validation should you add it to a separate configuration file under /etc/sysctl.d/. Check for conflicting definitions of the same parameter. Do not make an untested change persistent.

5. Turn the results into a validation record

Keep request types, connection reuse behavior, and concurrency patterns consistent during retesting. A workload using long-lived connections cannot replace one that frequently opens new connections; doing so can easily hide listen queue problems. Run active load tests in an isolated environment rather than flooding a production listening port with new connections.

Validation should cover at least four points:

Save the before-and-after parameters, counter samples, application metrics, and client error types. If all you can show is a larger queue, without evidence that request handling has improved, restore the original settings and continue investigating how the application accepts connections and processes downstream work.

The goal of listen queue diagnostics is not to reach a larger number. It is to establish where connections are waiting and demonstrate, through retesting under the same conditions, that the change works. When multiple services share a dedicated server, preserving port ownership and network namespace information is especially important: otherwise, server-wide counters can be incorrectly attributed to a single application.

Frequently asked questions

What does Recv-Q approaching Send-Q mean for a listening socket?

For a TCP socket in LISTEN state, Recv-Q counts established connections waiting for the application to accept them, while Send-Q shows the backlog limit. Sustained pressure suggests slow connection acceptance. Check overflow counter deltas and application metrics as well.

Will increasing somaxconn fix connection timeouts?

Not by itself. The application must request a sufficient backlog, and an existing listening socket usually needs to be recreated. A larger queue absorbs bursts but does not fix blocked workers or exhausted downstream connection pools.

EPYC 7543P Dedicated Physical Server

From $442.9/mo

Configure a Dedicated Server VPSAMD monthly totals