Interview Prep/Networking/Sockets, ports, TIME_WAIT and conntrack

`ss -s` on a reverse proxy shows 40,000 sockets in `TIME_WAIT`. Is that a problem?

BeginnerTroubleshootingSockets, ports, TIME_WAIT and conntrack
Answer

Likely causes → TIME_WAIT is normal: the side that closes a TCP connection first holds it for 2×MSL (60 s on Linux) so late packets don't corrupt a new connection. A proxy that opens a new upstream connection per request and closes it first will accumulate many. Each costs a few hundred bytes, so 40k is harmless memory-wise. It only becomes a problem when they exhaust ephemeral ports towards the same destination IP:port.

Diagnose →

ss -s
ss -tan state time-wait | awk '{print $4}' | sort | uniq -c | sort -rn | head   # per destination
sysctl net.ipv4.ip_local_port_range

If one upstream has ~28k entries (the default range size), you are close to Cannot assign requested address.

Fix → Reuse upstream connections (nginx keepalive 64; in the upstream block with proxy_http_version 1.1; and proxy_set_header Connection "";). If still needed, net.ipv4.tcp_tw_reuse=1 lets outgoing connections reuse TIME_WAIT ports safely.

Prevent → Don't "fix" TIME_WAIT counts by themselves; monitor per-destination port usage instead.

All Networking questions Roadmap: Networking for DevOps

More on Sockets, ports, TIME_WAIT and conntrack