Benefits of nginx using a multi-process model
First of all, for each worker process, it is independent The process does not need to be locked, so the overhead caused by locking is eliminated. At the same time, it will be much more convenient during programming and problem finding. (Recommended learning: nginx use)
Secondly, using independent processes can prevent each other from affecting each other. After one process exits, other processes are still working and the service will not be interrupted. , the master process will quickly start a new worker process.
Of course, if the worker process exits abnormally, there must be a bug in the program. Abnormal exit will cause all requests on the current worker to fail, but it will not affect all requests, so the risk is reduced.
nginx multi-process event model: asynchronous non-blocking
Although nginx uses multiple workers to process requests, there is only one in each worker The main thread, but nginx uses an asynchronous and non-blocking method to process requests. In other words, nginx can handle thousands of requests at the same time.
The number of requests that a worker process can handle at the same time is only limited by the memory size, and in terms of architectural design, there are almost no synchronization lock restrictions when processing concurrent requests between different worker processes. Worker processes usually do not Go to sleep.
Therefore, when the number of processes on Nginx is equal to the number of CPU cores (it is best for each worker process to be bound to a specific CPU core), the cost of switching between processes is minimal.
As for the common working method of apache (apache also has an asynchronous non-blocking version, but it is not commonly used because it conflicts with some of its own modules), each process only processes one request at a time.
Therefore, when the number of concurrency reaches thousands, there will be thousands of processes processing requests at the same time. This is a big challenge for the operating system. The memory occupied by the process is very large, and the CPU overhead caused by the context switching of the process is very large. Naturally, the performance cannot be improved, and these overheads are completely meaningless. .
The above is the detailed content of What are the benefits of using multiple processes in nginx?. For more information, please follow other related articles on the PHP Chinese website!