NginxDeveloping in +
PHP-Fpm mode is very simple without worrying about memory leaks.
fastcgi Process Manager
nginx the mate are already running well enough, but because
php-fpm it is itself a synchronous blocking process Model , all resources are released after the request ends, including a series of objects created by THE framework initialization, causing the PHP process to idling (Create a The "_ _ Destruction of the _ _ created) consumes a lot of
CPU resources, resulting in limited throughput capacity for a single machine. Request tamping, will result in the
CPU inability to release resources, greatly wasted the
CPU use rate.
The PHP-FPM process model is also very simple and belongs to the pre-derived child process pattern You must know that the early Apache was using this pattern to a request on
fork a process, the cost of the process is very large. This can significantly reduce throughput, and the number of concurrency is determined by the number of processes.
Pre-derived child process mode
After the program starts,
N a process is created. Each child process enters
Accept , waiting for the new connection to enter. When a client connects to the server, one of the child processes wakes up, begins processing client requests, and no longer accepts new
TCP连接 . When this connection is closed, the child processes are released, re-entered
Accept , and participate in the processing of the new connection.
The advantage of this model is that it is perfectly possible to reuse processes and does not require much context switching.
This model relies heavily on the number of processes to solve concurrency problems, a client connection needs to occupy a process, the number of worker processes, and how much concurrent processing power. The number of processes that the operating system can create is limited.
PHP framework initialization consumes a lot of compute resources, and each request needs to be initialized.
Starting a large number of processes can result in additional process scheduling consumption. When hundreds of processes, process context switching scheduling consumption accounts for less than 1% of the CPU is negligible, and if you start thousands of or even tens of thousands of processes, the consumption will rise in a straight line. Scheduling consumption may account for percent or even 100% of the CPU.
If requesting a third-party request is very slow, the request process will always consume
CPU resources and waste expensive hardware resources
For example, an instant chat program, a server may have to maintain a hundreds of thousands of connection, then start hundreds of thousands of of the process to hold. It's obviously not possible.
Is there a technology that can handle all concurrency within a process
IO ? The answer is yes, this is IO multiplexing technology.
The problem of PHP-FPM working mode
epollthe event model, one
workercan handle multiple requests at the same time
fpm-workerA request can be processed at the same time
fpm-workerThe framework needs to be reinitialized before each processing
mvcof the request, and then the resource is freed
High concurrency requests,
nginxdirect response 502
Large switching consumption between fpm-worker processes
So what other solutions do we have?
We analyze our business. It is not difficult to find that
90% the above business Is IO Intensive Business , we only need to improve THE ability of IO reuse can improve the ability of single-
php-fpm machine throughput, in addition to the need to replace synchronous blocking mode with Asynchronous non-blocking mode , asynchronous opening mode is more complex and not easy to maintain, of course
php-fpm , it can not be used, it will solve our core problem-performance.
In IO intensive business We need frequent context switching, which is too complex to develop in thread mode.
The number of threads that can be opened in a process is also limited, too many threads will also increase
CPU the load and memory resources, threads do not block state , IO blocking can not actively cede
CPU resources, belongs to the preemptive scheduling model . Not very suitable for PHP development.
swoole 4.+ full-collaboration mode is turned on to allow synchronous code to execute asynchronously. For more information, see Why you want to use Swoole