Troubleshooting guide
Too many TIME_WAIT connections: what to do
TIME_WAIT is normal. A large number becomes a problem only when you run out of local ports to make new outbound connections.
3 min read11 sectionsUpdated
On this page
What TIMEWAIT is
After closing a TCP connection, the side that closed first holds the socket briefly, so that delayed packets are not mistaken for a new connection. It uses very little memory. The real limit is the four-part address: to one destination IP and port, you have only as many connections as local ephemeral ports, about 28,000 by default.
Diagnose it
See if it is really a problem: look for failures such as EADDRNOTAVAIL or "cannot assign requested address".
# Socket state summary
ss -s
# TIME_WAIT by destination
ss -tan state time-wait | awk 'NR>1 {print $4}' | sort | uniq -c | sort -rn | head
# Local port range
cat /proc/sys/net/ipv4/ip_local_port_range
# Errors in the kernel counters
nstat -az | grep -iE 'TCPTimeWaitOverflow|ListenDrops'Fixes
Reuse connections. Enable pooling in your HTTP and database clients. In Nginx, set keepalive 64; in the upstream, proxy_http_version 1.1; and clear the Connection header. Only then widen ip_local_port_range if needed, and enable net.ipv4.tcp_tw_reuse=1 for outbound connections. Do not look for tcp_tw_recycle: it broke clients behind NAT and was removed from Linux.
Which side holds it
The host that closes first carries TIME_WAIT. For a server that closes every connection, sockets pile up on the server, though they share its one listening port and so cannot exhaust ports. The risky case is a client making many outbound connections to the same destination, because each one consumes a local port. That is why proxies, API gateways and services calling a single database are the hosts where this appears. To reduce the pressure further, add destination IPs: each extra backend address gives you a whole new set of port combinations. A connection pool remains the better answer, since it also removes the latency of TCP and TLS setup on each request.
Catch it automatically
Chart socket states with infrastructure monitoring and correlate them with connection errors in APM. Connection time showing up in traces is a sign that pooling is not working.
Check it now, free
- Open port checker: Is a TCP port open, closed or filtered from the internet?
Related reading
- MySQL: Too many connections: Fix MySQL error 1040 Too many connections: check max_connections and current threads, find sleeping or leaked connections, size application pools.
- Nginx "worker_connections are not enough": how to fix it: Fix Nginx worker_connections are not enough: why proxying uses two connections per client, how to size limits, raise descriptors and add upstream keepalive.
- PostgreSQL "too many clients already": how to fix it: Fix PostgreSQL FATAL: sorry, too many clients already. See who holds connections, stop leaks, add PgBouncer and size max_connections the right way.
- EMFILE: too many open files in Node.js: Fix EMFILE too many open files in Node.js: leaked sockets and streams, unbounded concurrent file reads, ulimits and file watchers, with lsof commands.
- ERR_TOO_MANY_REDIRECTS: how to fix a redirect loop: Fix ERR_TOO_MANY_REDIRECTS: trace the chain with curl, find the loop between CDN, server and app, such as HTTPS or www rules, and prevent it.
- 429 Too Many Requests: what it means and how to fix it: 429 Too Many Requests explained: rate limits, the Retry-After header, backoff with jitter for clients, and how to set fair rate limits on your own API.
Watch your own site and services with VitalRix
Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.