How to achieve a million-level long connection on a single server, here are some optimizations to achieve this goal:
1. First, you need to prepare a server with large memory and install a Linux system, such as rehat and centos (kernel version is inAbove 2.6.25)wait.
Why do you need large memory? Because each connection needs to have a read and write cache, please see the content of the second part;
Why is the kernel version above 2.6.25?There was a macro definition before the 2.6.25 kernel, which defined that the maximum file descriptor size is 1024*1024, which is exactly 1 million.This value can be set via /proc/sys/fs/nr_open.
2. After installing the system,Adjust the system parameters, in /etc/sysctl.conf
#Defines the maximum listening queue length of each port in the system. This is a global parameter, the default value is 128
= 2048
#System read and write cache, default value
.rmem_default = 262144
.wmem_default = 262144
#System read and write cache
.rmem_max = 16777216
.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 4096 16777216
net.ipv4.tcp_wmem = 4096 4096 16777216
net.ipv4.tcp_mem = 786432 3145728 4194304
net.ipv4.tcp_max_syn_backlog = 16384
.netdev_max_backlog = 20000
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_rmem is used to configure the size of the tcp read buffer. The first 4096 is the minimum value of this read buffer, the third is the maximum value, and the middle is the default value. zhe modify the size of the read buffer in the program, but cannot exceed the minimum and maximum. In order to minimize the amount of memory used by each socket, I set the default value here to 4096.
net.ipv4.tcp_wmem is used to configure the size of the write buffer. The size of read buffer and write buffer directly affects the memory usage of socket in the kernel. This is why large memory is needed in the first article.
net.ipv4.tcp_mem configures the memory size of tcp, and its unit is page, not bytes. When the second value exceeds, TCP enters pressure mode. At this time, TCP tries to stabilize its memory usage and exits pressure mode when it is less than the first value. When the memory usage exceeds the third value, TCP refuses to allocate the socket. Check itdmesg, will produce a lot of logs "TCP: too many of orphaned sockets".
In addition, the server needs to open a large number of file descriptors, such as 2 million, and the configuration is as follows:
Set the nofile to 2 million (2.6.25 kernel and later versions), this value can be set through /proc/sys/fs/nr_open.
Now set the nofile:
admin soft nofile 2000000 admin hard nofile 2000000