Article Title: how to improve the load capability of Linux to cope with transient connections. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
In the case of a large number of short connections, the TCP stack of Linux usually generates a large number of sockets in the TIME_WAIT status.
You can see with the following command:
Netstat-ant | grep-I time_wait
Sometimes, this number is astonishing:
Netstat-ant | grep-I time_wait | wc-l
It may exceed 30 thousands or 40 thousands. At this time, we need to modify the tcp time wait of linux kernel to shorten it. the sysctl parameter seems to be usable, which is/proc/sys/net/ipv4/tcp_fin_timeout, the default value is 60, that is, 60 seconds. many online documents say that setting this value to a lower value can reduce the TIME_WAIT status in netstat, but this statement is incorrect. After carefully reading the Linux kernel source code, we found that this value was actually used for output. after modification, it was not actually read back to the kernel for use, what really works in the KERNEL is a macro definition, in $ KERNEL/include/net/tcp. h contains the following lines:
# Define TCP_TIMEWAIT_LEN (60 * HZ)/* how long to wait to destroy TIME-WAIT
* State, about 60 seconds */
This macro controls the timeout time of the TCP TIME_WAIT state. If we want to reduce the number of TIME_WAIT states (thus saving a little kernel operation time), we can set this value to a lower value. according to our test, it is appropriate to set it to 10 seconds, that is, modify the above:
# Define TCP_TIMEWAIT_LEN (10 * HZ)/* how long to wait to destroy TIME-WAIT
* State, about 60 seconds */
Then re-compile the kernel and restart the system to find that the TIME_WAIT status caused by short connections is greatly reduced:
Netstat-ant | grep-I time_wait | wc-l
Generally, it can be reduced by at least 2/3, and the system can respond to short connections accordingly.