opening
Before we start today’s essay, let’s start with an interview question:
What is the maximum number of concurrent requests you have ever encountered on a single machine? What do you think is the maximum number of concurrent requests on a physical server that is normally configured? Give your understanding and analysis.
Think about it for a few minutes, and if you can give an answer that makes sense, then you really don’t have to look down, and it’s a good idea to turn off your phone and go spend time with your family.
Think about it for a few minutes, and if you don’t have a clue or you’re not sure about the answer, then don’t close the page and go play, you should keep reading, because this is a good question.
For back-end developers, concurrency is often positively correlated with technical difficulty, and indeed it is: volume determines architecture.
The server will have different priorities according to different business scenarios. The simple pursuit of high concurrency is not the fundamental purpose, but high availability and stability are more important.
Therefore, our ultimate goal is to pursue high concurrency on the basis of high availability and high stability and reduce cost and increase efficiency.
High availability & high concurrency is intuitively perceived by us. In essence, this is a complex system engineering. Every link will affect the results, and every piece is worth studying and deepening.
C10K problem and C10M problem
At the beginning of 2000, the scale of the global Internet was not large, but C10K problem had been put forward at that time, the so-called C10K is the single machine 1W concurrency problem, although now do not think it is a difficult problem, but it was a visionary and challenging problem at the beginning.
The C10K question was first posted by Dan Kegel on his website, and the link to the original article is as follows:
http://www.kegel.com/c10k.html
Dan Kegel, currently at Google, has been working on computer programming since 1978 and is the author of Winetricks and Crosstool.
Dan Kegel’s article is easy to read, and Baymax recommends that anyone working in server-side development or interested in high-performance web development try it out.
Epoll is not mentioned in the third edition of APUE, so we have not solved the C10K problem for a long time, in which IO reuse epoll/ Kqueue/IOCP technology plays a very important role in solving the C10K problem.
Open source gurus have developed network libraries such as Libevent/Libuv based on epoll/ KQueue, which have greatly improved the efficiency of highly concurrent network development. These things are familiar to C/C++ programmers.
Here’s a quick mention of the outlook and challenge for the next 10 years: the C10M problem.
Those at the top of the wave have long been thinking about achieving 1000W concurrency on a single machine, which sounds incredible now, but to achieve this goal, in addition to hardware improvement, more important is the transformation of the system software and protocol stack.
Errata Security CEO Robert Graham’s keynote speech at Shmoocon 2013:
Don’t let the OS kernel do all the heavy lifting: move tasks like packet processing, memory management, and processor scheduling from the kernel to the application efficiently, and let an OS like Linux handle only the control layer, leaving the data layer entirely to the application.
Indeed, don’t you think the Linux kernel does a lot of things that it shouldn’t?
In recent years, DPDK, PFRING, NETMAP and other technologies are similar to the idea, and now the popular coprocessor +CPU architecture is also like this:
Analysis of the maximum number of concurrent servers
The C10K and C10M issues mentioned above are all about improving the concurrency of the server, but the inevitable question is: What is the maximum concurrency of the server?
Five yuan group
Those of you who have ever communicated have heard of the concept of a quintuple. A quintuple uniquely identifies a network connection, so to understand and analyze maximum concurrency, you must understand quintuples:
Number of port/IP combinations
So what is the maximum number of unique quintuples for a server?
Some people say that 65535, obviously not, but the reason why this kind of answer is because the current Linux port number is 2 bytes short type, a total of 2^16 ports, excluding some of the system occupied ports, there are only more than 64,000 available ports.
For the server itself, the number of destports is indeed limited, not to mention IP addresses. Even if there are multiple network adapters and each network adapter is bound with multiple IP addresses, the combination type of the number of ports and IP addresses on the server is limited.
The client itself has a limited number of ports and IP addresses, although this is a combination problem, but the number is limited:
Just how big
Looking at the previous port-IP combination count calculation, it seems that the concurrency is not particularly large.
No, it’s really big.
Analysis, the previous calculation is for a single server or client, but in fact, each server will handle all the clients of the whole network, so from the server side, the number of source IP and source Port is very large.
In theory, the server can accept a client IP of 2^32(based on IPv4), the number of ports is 2^16, the current port number is still 16bit, so the theoretical maximum is 2^48.
The actual situation
There is no free lunch.
Each connection consumes system resources, so in practice, a maximum number of concurrent requests may be set to ensure the security and stability of the server, so this theoretical maximum number of concurrent requests is impossible to achieve.
Maximum number of client connections
Knowing that the maximum number of concurrent connections to a server is 2^48, what is the maximum number of servers a client can connect to?
For the client, of course, we can increase the connection capability by means of multiple network adapters and multiple IP addresses. We still assume that the client has only one network adapter and one IP address. Due to the limit of the number of ports to 2^16, after removing the ports occupied by the system (such as 22/23/80, etc.), there are about 64,000 available.
In other words, although the client can connect to any destination IP address and destination port, the client itself has a limited number of ports, so the theoretical maximum number of connections for the client is 2^16, including the system occupied port.
Client in the NAT environment
After solving the first two problems, let’s look at another problem:
What is the maximum number of Intranet IP addresses that a public network egress NAT service device can concurrently support to access Internet services?
After all, public IP is limited and cost money, most of our machines are in the LOCAL area network (LAN) combined with NAT to access the external network, so this scene is very familiar.
Therefore, in the NAT environment, a maximum of 65535 concurrent accesses to the Extranet are supported, because the client is a NAT device.
summary
This paper starts with an interview question, firstly describes the C10K and C10M problems, then explains the calculation and principle of the maximum access number of clients and the maximum concurrent number of servers in detail, and finally describes the access concurrent number of NAT scenarios.
Finally, thank you for your reading.