Operations 9 min read

Why Do TIME_WAIT Connections Accumulate and How to Fix Them?

The article explains why massive TIME_WAIT TCP connections appear during high‑concurrency scenarios, their impact on server ports and new connections, and provides practical analysis and solutions such as keep‑alive headers, socket reuse, and reducing the TIME_WAIT duration.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Why Do TIME_WAIT Connections Accumulate and How to Fix Them?

1. Problem Description

In high‑concurrency simulations, a large number of TCP connections enter the TIME_WAIT state. After a short period these connections disappear as they are reclaimed, which is normal, but in sustained high‑traffic environments new TIME_WAIT connections keep appearing and can overwhelm the system.

Impact: Each TIME_WAIT connection occupies a local port (max 65535). When many connections are in TIME_WAIT, new connections may fail with "address already in use: connect" errors.

2. Problem Analysis

The root causes are:

Massive short‑lived connections.

HTTP requests with Connection: close cause the server to actively close the connection.

TCP’s four‑handshake termination keeps the socket in TIME_WAIT for twice the Maximum Segment Lifetime (MSL).

TIME_WAIT details:

State appears on the side that actively closes the connection (sends FIN, receives ACK).

It lasts for 2 × MSL (typically 4 minutes, as MSL is 2 minutes).

3. Solutions

To mitigate excessive TIME_WAIT and connection‑creation failures:

Client side: set Connection: keep-alive in HTTP headers to keep connections alive.

Server side:

Enable reuse of sockets in TIME_WAIT state (e.g., SO_REUSEADDR).

Reduce TIME_WAIT duration by configuring it to 1 MSL (≈2 minutes).

4. Core Takeaways

TIME_WAIT occupies a local port and prevents its reuse until the timeout expires.

The default port limit is 65535 (16‑bit).

Excessive TIME_WAIT can cause "address already in use" errors for new connections.

In practice, browsers now send Connection: keep-alive, reducing the problem.

Server configuration should allow socket reuse and optionally shorten the TIME_WAIT timer.

5. Appendices

Appendix A – Query TCP Connection States

# Count connections by state
$ netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
ESTABLISHED 1154
TIME_WAIT 1645

Appendix B – MSL (Maximum Segment Lifetime)

MSL is the maximum time a packet can exist on the network before being discarded. RFC 793 defines MSL as 2 minutes, though implementations often use 30 seconds, 1 minute, or 2 minutes.

Appendix C – TCP Handshake and Four‑Way Close

The three‑way handshake establishes a connection; the four‑way termination ensures reliable closure and discards delayed packets by keeping the socket in TIME_WAIT for 2 × MSL.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

networkTCPTIME-WAITserver performancesocket reuse
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.