Linux single-machine TCP concurrent connection

http://blog.csdn.net/kobejayandy/article/details/47127991



summarizes the server's limitations on tcp connections and the method of increasing the number of tcp connections, which may never be used in work, but there will be some understanding of network knowledge help.
1. The server has nothing to do with the 16-bit port number (maximum 65535)
. The server ip+port (listening port) + client ip+port determines a connection. After the client connects to the server, the server does not assign another physical Port to connect with the client. In fact, all data is still received and sent through the listening port (whether full-duplex or half-duplex, it is duplex anyway), but there is one more logical socket. These should all be things on the network card. When a byte stream is received, the network card will call back to the operating system. When calling back, it will tell the operating system client ip+port. If it is an epoll model, this time it may only be After a few bytes are received, a method specific to that new socket is called back. Then the data called back from the listening port next time may be another logical socket, and they do not affect each other.
2. Memory limit The
system allocates a TCP control block (TCP control block or TCB) for each TCP connection. A tcb control block probably occupies more than 1k of memory. If there are millions of connections and one tcb occupies 1k of memory, then 1G of memory is required. This is still an ideal situation. Generally, tcb should be larger than 1k of memory. When there is data transfer on each connection, more memory is required at the same time.
In addition, the tcp parameter that needs to be set is the tcp read and write buffer, which is 86k by default, and can be changed to 4k. In addition, the value of tcp_mem needs to be modified.
[ruby] view plain copy
tcp_mem (3 INTEGER variables): low, pressure, high 
low: When TCP uses a memory page lower than this value, TCP will not consider freeing memory. 
pressure: When TCP uses more memory pages than this value, TCP tries to stabilize its memory usage and enters pressure mode, and exits pressure state when memory consumption is lower than the low value. 
high: Allows all tcp sockets to use the page amount for queuing and buffering datagrams. When the memory usage exceeds this value, the system refuses to allocate sockets, and the background log outputs "TCP: 
too many of orphaned sockets". 

3. File handle limit
Each socket is a file handle. The file handle limit under Linux includes the maximum file handle allowed by linux and the maximum simultaneously active file handle allowed by linux.
4. Network card limit
Gigabit network card, the upper limit of full load work, about 600 megabytes, the unit is b, divided by 8 to be 75k/single connection. This is generally sufficient.
Refer to http://www.blogjava.net/yongboy/archive/2013/04/11/397677.html The places that need to be modified under linux are:
[python] view plain copy
echo "* - nofile 1048576" >> /etc/ security/limits.conf #open file resource limit is the number of file handles that a process in linux can open. 
echo "fs.file-max = 1048576" >> /etc/sysctl.conf #The maximum allowable file descriptor of the system  
echo "net.ipv4.ip_local_port_range = 1024 65535" >> /etc/sysctl.conf #The range of ports that can be used, mainly for testing    
echo "net.ipv4.tcp_mem = 786432 2097152 3145728" >> /etc/sysctl. conf #see above 
echo "net.ipv4.tcp_rmem = 4096 4096 16777216" >> /etc/sysctl.conf      
echo "net.ipv4.tcp_wmem = 4096 4096 16777216" >> /etc/sysctl.conf 
in the reference article, build A 100w connection uses about 7500M of memory, and each connection uses about 7.5k of memory (mainly read and write buffers, the size of tcb).
In addition, it is not difficult to achieve 100w connection by modifying the system configuration, but it is still very difficult to realize the processing of concurrent 100w business.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326357618&siteId=291194637