Skip to content
statance
MenuClose
← All troubleshooting guides

Too many TIME_WAIT connections

Linux

TIME_WAIT is a normal TCP state on the side that closed a connection; it lasts about 60 seconds on Linux. Thousands are usually harmless. They become a problem when a client opens so many short-lived outbound connections that it runs out of local ports — fix that with keep-alive and connection pooling rather than kernel tweaks.

Symptoms

  • ss shows tens of thousands of TIME-WAIT sockets
  • Outbound connections fail with “Cannot assign requested address”
  • Reverse proxy to backend errors under load

How to diagnose it

  1. Count connections by state

    A summary of TCP states.

    ss -s ss -tan | awk 'NR>1{print $1}' | sort | uniq -c | sort -rn
  2. Find the remote endpoints involved

    Shows which destinations have the most TIME-WAIT sockets.

    ss -tan state time-wait | awk 'NR>1{print $4}' | sort | uniq -c | sort -rn | head
  3. Check the local port range

    Outbound connections to the same destination are limited by this range.

    cat /proc/sys/net/ipv4/ip_local_port_range

Common causes and fixes

Reverse proxy opening a new upstream connection per request

Enable upstream keep-alive in nginx.

upstream app { server 127.0.0.1:8080; keepalive 32; } # in the location block: proxy_http_version 1.1; proxy_set_header Connection "";

Application connecting to a database or API per request

Use a connection pool so connections are reused.

Local port range too narrow

Widen it if outbound port exhaustion is confirmed.

echo "net.ipv4.ip_local_port_range = 10240 65535" | sudo tee /etc/sysctl.d/99-ports.conf sudo sysctl --system

Outbound reuse not enabled

tcp_tw_reuse lets new outbound connections reuse TIME_WAIT sockets safely. Do not use tcp_tw_recycle; it was removed from Linux 4.12.

sudo sysctl -w net.ipv4.tcp_tw_reuse=1

Prevent it happening again

  • Review TCP connection state history in Statance to spot growth after deploys.
  • Add an HTTP check on the service behind the proxy so errors under load trigger an alert.

Set it up: Linux monitoring · TCP port monitoring.

FAQ

Is TIME_WAIT bad?

No. It prevents delayed packets from an old connection being mistaken for a new one. Only port exhaustion from huge numbers of outbound connections is a problem.

How long does TIME_WAIT last on Linux?

60 seconds. It is fixed in the kernel and not configurable through sysctl.

Related guides

Get alerted before this becomes an outage.

Install the agent in one line. Statance watches CPU, memory, disk and services, and its AI assistant helps find the cause.