Skip to content

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

Key points

  • TIME_WAIT lasts about 60 seconds on Linux.
  • It belongs to whichever side closed first.
  • The fix is connection reuse, not kernel tuning.
  • Never use the removed tcp_tw_recycle.

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.

Common causes

  • No connection pooling: a new connection for every HTTP or database call.
  • Connection: close on calls between proxy and backend.
  • Nginx upstreams without keepalive.
  • High-throughput clients talking to one destination.

Diagnose it

See if it is really a problem: look for failures such as EADDRNOTAVAIL or "cannot assign requested address".

bash
# 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.

Frequently asked questions

Are many TIME_WAIT sockets bad?

Not by themselves. They matter only when you run out of ephemeral ports.

Should I set tcp_fin_timeout to reduce TIME_WAIT?

It does not change TIME_WAIT on Linux; it governs FIN_WAIT_2.

What is the best fix?

Keep connections alive and pool them.

Check it now, free

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.