Fundamentals 8 min read

Understanding TCP TIME_WAIT State and Optimization Techniques

This article explains the TCP TIME_WAIT state, its role in connection termination, the performance impact of large numbers of TIME_WAIT sockets, and practical optimization methods such as using keep‑alive and tuning Linux kernel parameters to mitigate the issue.

360 Quality & Efficiency
360 Quality & Efficiency
360 Quality & Efficiency
Understanding TCP TIME_WAIT State and Optimization Techniques

When performing performance testing or server tuning, engineers often inspect TCP connection states and may encounter a large number of sockets in the TIME_WAIT state. This article explains the meaning of that state and demonstrates the scenario with real‑world performance test examples.

The TCP four‑way handshake is illustrated, showing the sequence of FIN and ACK exchanges that lead to the TIME_WAIT state on the side that initiates the close. Various intermediate states (FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT, CLOSING, LAST_ACK) are described, and the purpose of TIME_WAIT—ensuring the final ACK reaches the peer and allowing old packets to expire—is highlighted.

Although TIME_WAIT may seem harmless, a high volume of connections that are actively closed can cause many sockets to remain in TIME_WAIT, consuming port resources and degrading performance, especially for services that open many short‑lived connections.

Using HTTP keep‑alive (the default in HTTP/1.1) reduces the number of connections that need to be closed, thereby limiting the accumulation of TIME_WAIT sockets. The article presents a pressure test comparing connection:keep-alive and connection:close , showing that the latter leads to a rapid increase in TIME_WAIT sockets and a noticeable drop in throughput.

To further alleviate excessive TIME_WAIT sockets, the article suggests two approaches: (1) keep the connection header set to keep‑alive, and (2) modify Linux kernel parameters via /etc/sysctl.conf . The recommended settings are:

net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30

Enabling tcp_tw_recycle and tcp_tw_reuse allows the kernel to quickly reclaim or reuse sockets in TIME_WAIT, though the article warns that these changes carry risks and should be applied with caution.

After applying the kernel tweaks, a follow‑up performance test shows that the number of TIME_WAIT sockets stabilizes and does not grow dramatically, confirming the effectiveness of the optimization.

In summary, monitoring TCP states during performance testing and applying appropriate connection handling or kernel tuning can prevent TIME_WAIT from becoming a bottleneck in high‑traffic server environments.

Performance TestingTCPLinuxTIME_WAITsysctlNetwork Tuning
360 Quality & Efficiency
Written by

360 Quality & Efficiency

360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.

0 followers
Reader feedback

How this landed with the community

login 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.