TCP Retransmissions, TIME_WAIT and the Hidden Cost of Short-Lived Connections
A client's site was slow every afternoon. Not down, not erroring — just slow. Pages that loaded in under a second in the morning took four or five seconds after lunch. CPU was fine. Memory was fine. Disk was fine. The application logs showed nothing unusual.
The problem was not in the application at all. It was in the TCP layer, hiding in plain sight in counters I had not looked at yet. When I finally did, the server was retransmitting thousands of segments a minute and sitting on tens of thousands of sockets in TIME_WAIT. The afternoon traffic peak was not overwhelming the server's resources. It was overwhelming its connections.
This is the kind of problem that never shows up in top. You have to know where to look.
Retransmissions: the counter most people never check
TCP guarantees delivery. When a segment goes missing, the sender waits, then sends it again. A few retransmissions are normal — networks drop packets. A steady stream of them is not normal. It means the network path is congested, a link is flaky, or the receiving side is too slow to acknowledge.
The quickest way to see the scale of it is the kernel's own counters:
nstat -az TcpRetransSegs TcpExtTCPSpuriousRTOs Or the older, still reliable:
netstat -s | grep -i retrans On that slow-afternoon server, TcpRetransSegs was climbing by thousands per minute during peak hours. That told me packets were being lost or delayed somewhere between the server and its visitors — not that the server itself was broken. It also told me the retransmissions were adding real latency: every retransmitted segment waits for a retransmission timeout (RTO) before it goes out again, and the RTO grows with each failed attempt. A single dropped packet can add hundreds of milliseconds to a request. A page that needs dozens of connections feels every one of them.
If you want to see retransmissions as they happen, ss shows per-socket retransmission counts:
ss -ti Look for the retrans: field. A socket with a climbing retrans count and a growing rto: value is a connection that is struggling. If most of your sockets show retransmissions, the problem is the network path or an overloaded middlebox. If only a few do, it is usually the far end — a slow client, a bad mobile connection, or a misbehaving proxy.
And if you need proof at the packet level:
sudo tcpdump -i eth0 -nn 'tcp[tcpflags] & (tcp-syn|tcp-rst) != 0' -c 100 Retransmitted SYNs in particular are a red flag: they usually mean the far end never answered at all, which points at a firewall dropping packets silently instead of rejecting them.
TIME_WAIT: the socket graveyard
Every TCP connection that closes cleanly leaves a socket behind in TIME_WAIT state. This is by design — the socket lingers for twice the maximum segment lifetime (2MSL, 60 seconds on Linux by default) so that stray duplicate packets from the old connection cannot corrupt a new connection that reuses the same port numbers.
One lingering socket is harmless. Ten thousand of them is a problem, and I have seen servers with far more.
Check how many you have right now:
ss -tan state time-wait | wc -l And the overall picture:
ss -s On the server I was debugging, the count sat above 40,000 every afternoon. The cause was an application pattern I see constantly on shared hosting and VPS setups: a PHP application opening a fresh TCP connection to a remote MySQL server, a remote API, or a Redis instance for every single request, then closing it. Each of those connections became a TIME_WAIT socket for the next 60 seconds. At a few hundred requests per second, the sockets piled up faster than they expired.
Why does it matter? Two reasons.
First, ephemeral port exhaustion. Outbound connections need a local port, drawn from the ephemeral range:
cat /proc/sys/net/ipv4/ip_local_port_range The default range gives you roughly 28,000 ports per destination IP and port combination. A busy application opening short-lived connections to the same database server can genuinely run out. When it does, new connections fail with errors like Cannot assign requested address, and the application logs blame the database — which is perfectly healthy.
Second, memory and CPU. Every TIME_WAIT socket consumes kernel memory and shows up in connection tracking tables. On a small VPS with 2 GB of RAM, tens of thousands of dead sockets are not free.
What actually fixed it
There are sysctl knobs people reach for first, and I want to be honest about which ones help and which ones are traps.
The safe, useful ones:
# Shorten how long sockets linger in FIN_WAIT_2 / TIME_WAIT-adjacent states net.ipv4.tcp_fin_timeout = 30 # Allow reusing TIME_WAIT sockets for new *outbound* connections net.ipv4.tcp_tw_reuse = 1 # Cap the total number of TIME_WAIT sockets before the kernel starts # destroying the oldest ones aggressively net.ipv4.tcp_max_tw_buckets = 180000 Apply with sysctl -p after adding them to /etc/sysctl.conf.
But here is the important part: tcp_tw_reuse only helps outbound connections, and it only helps on the client side of the connection. It does nothing for a web server accepting inbound connections. And tcp_tw_recycle, the setting old blog posts still recommend, was removed from the kernel years ago because it breaks connections behind NAT — do not go looking for it, and do not trust any guide that still lists it.
The real fix on that server was not a sysctl at all. It was connection reuse:
- Persistent database connections instead of connect-per-request. One connection per PHP worker, kept alive, instead of hundreds of new connections per second.
- HTTP keep-alive between the reverse proxy and the application, so a visitor's page load did not open a dozen fresh connections for a dozen assets.
- Connection pooling in front of the remote services, so ten pooled connections served traffic that used to open thousands.
After those changes, TIME_WAIT dropped from 40,000+ to under 2,000 at peak, retransmissions fell back to background noise, and the afternoon slowdown disappeared. The sysctl tuning helped at the margins. The architecture change fixed the problem.
A quick triage checklist
When a server is slow but CPU, memory and disk all look healthy, run this before you start restarting services:
# 1. How many sockets are stuck in TIME_WAIT? ss -tan state time-wait | wc -l # 2. Are retransmissions climbing? nstat -az TcpRetransSegs # 3. Which sockets are retransmitting right now? ss -ti | grep -B1 retrans # 4. Are we close to ephemeral port exhaustion? ss -tan | awk '{print $4}' | sort | uniq -c | sort -rn | head # 5. What does the kernel think is happening? cat /proc/net/snmp | grep -i tcp If TIME_WAIT is high, look for short-lived connection patterns in the application. If retransmissions are high, look at the network path — and check whether the server's own NIC is dropping packets first:
ethtool -S eth0 | grep -i drop A counter climbing there means the problem is on your own network interface, not somewhere out on the internet.
The lesson I keep relearning
Short-lived connections look cheap. Opening a TCP connection costs almost nothing in code — one function call, one line. But every connection pays for the three-way handshake, the slow-start ramp, the four-way close, and then 60 seconds of TIME_WAIT. Multiply that by thousands of requests and the cheap thing becomes the most expensive thing on the server.
Keep connections alive. Pool them. Reuse them. And when a server is slow for no visible reason, check the TCP counters before you blame the application. The kernel has been counting the problem all along.
Related reading
- When DNS Lies: TTL, Propagation, Resolver Caching and Debugging with dig
- Mastering Netstat: The Linux Command That Separates Beginners from Real Infrastructure Engineers
About the author: Sovrab Roy is a Linux System Administrator & Server Engineer with 6+ years of experience managing 50+ production servers at 99.9% uptime. He works with WHM/cPanel, VPS, Docker, networking and server security. More of his field notes: dev.to/sovrab · sovrabroy.online
What is the highest TIME_WAIT count you have ever seen on a production server — and what turned out to be causing it?
