As can be seen from the schematic above, the TIME_WAIT is actively shutting down the side of the connection, and after the client completes the data transfer, it initiates the active shutdown of the connection close (), which enters the time_wait state, and then maintains this state 2MSL (max Segment lifetime), close the recycle resource completely. Why would you do that? Obviously has been active shut down the connection why still need to keep resources for a period of time. This is the design of TCP/IP, mainly for the following two considerations: 1 to prevent the last connection in the packet, lost again, affect the new connection (after 2MSL, all the previous connection of the duplicate package will disappear)
2 reliable shutdown of TCP connections. The last ACK (FIN) sent by the active shutdown is likely to be lost, when the passive side will resend the fin, and if the active party is in the CLOSED state, it will respond to RST instead of ACK. So the active side should be in the TIME_WAIT state, but not the CLOSED. In addition to this design time_wait will regularly recycle resources, and will not occupy a lot of resources, unless a short period of time to accept a large number of requests or attacks.
It is worth saying that for TCP based HTTP protocol, the TCP connection is turned off by the server side, so that the server end will enter the TIME_WAIT state, it is conceivable that for the large number of access to the Web Server, there will be a lot of time_wait state, If the server receives 1000 requests a second, it will have a backlog of 240*1000=240,000 of TIME_WAIT records, maintaining these states to bring the server a burden. Of course, the modern operating system will use a fast lookup algorithm to manage these time_wait, so for the new TCP connection request, to determine whether hit a time_wait not too time-consuming, but there are so many States to maintain is always bad.
The HTTP protocol version 1.1 stipulates that the default behavior is keep-alive, which is to reuse a TCP connection to transmit multiple request/response, a major reason is the discovery of this problem.
In the HTTP interaction, the connection is closed not by the client, but by the server, so there will be a lot of time_wait in the Web server.
The solution is to enable the server to quickly reclaim and reuse those time_wait resources.
The following parameters are mainly modified for/etc/sysctl.conf:
Net.ipv4.tcp_tw_reuse and Net.ipv4.tcp_tw_recycle are opened to reclaim resources that are in the TIME_WAIT state.
Net.ipv4.tcp_fin_timeout this time can reduce the time the server transfers from Fin-wait-2 to time_wait in exceptional circumstances.
Net.ipv4.tcp_keepalive_* a series of parameters that are used to set the server to detect the connection to survive the related configuration.
Reference:
Talking about the time_wait and close_wait:http://shootyou.iteye.com/blog/1129507 in the application environment