Nginx Series (three)--management process, multi-work process design

Source: Internet
Author: User

Nginx by a Master processes and multiple worker process consists of, but Master process or worker No more threads are created in the process.


First, Master Processes and worker role of the process

Master Process

do not need to handle network events, not responsible for the execution of the business, only through the management of workers and other sub-processes to achieve restart services, smooth upgrade, change log files, configuration files in real-time function and other functions.

Master is through Fork the system calls the child process to implement communication with the child process.

worker Process

A request to handle the master process fork .

worker processes are implemented by processing signals to achieve and Master of Communication

====================================================================================

Second, the signal processing process

Master How does the process receive signal processing?

Master Once the process receives the signal, it reloads the configuration file, then starts the new process and sends a signal to all the old processes that they can retire honorably. After the new process starts, it begins accepting new requests, and the old process no longer receives the new request after it receives the master signal, and exits after all outstanding requests in the current process have been processed.

Worker How does the process receive signal processing?

        First,workerprocesses are equal, and every process, the opportunity to process requests, is the same. When we provide theof the Porthttpservice, a connection request comes in, each process has the possibility to handle this connection, how to do it? First, eachworkerprocesses are all from theMasterProcessForkcome on, inMasterprocess inside, the first to establish a good needListenof theSocketafter that, and thenForkout multipleworkerprocess, so that eachworkerprocess can accept this.Socket. Generally, when a connection comes in, all theAcceptthisSocketthe process above will be notified, and only one process can accept the connection, while the othersAcceptfailure, this is the so-called surprise group phenomenon.

          So in order to solve this problem, nginx provides a accept_mutex (optional, Open by default). This is an addition to the accept accept connect So there's no such thing as a surprise group.

When a worker process starts to read the request, parses the request, processes the request, generates the data, and then returns it to the client after the connection is accept. The connection is not finally disconnected. A request is handled entirely by the worker process and is handled only in a worker process.

====================================================================================

Third, the advantages of such design

1 , leveraging the concurrent processing capabilities of multicore systems

         now the operating system is multi-coreCPUIf there has been only one process at work, then wasting resources, if it isworkerUnequal inter-process status will inevitably lead to process bottlenecks,Nginxto avoid this problem, designworkerThe process of equality between. In addition,workernumber andCPUwith the same number of cores, it will achieveCPUthe full use of resources, ifworkernumber more thanCPUthe number of nuclear, is bound to cause the competition of resources;CPUthe number of nuclear, is bound to cause waste of resources. Improve network performance and reduce request latency.

2 , load Balancing

load Balancing is achieved through process communication between multiple workers. That is, when a request arrives, it is easier to allocate it to a worker process with less load. This reduces the latency of the request and, to a certain extent, improves network performance

3 , the management process is responsible for monitoring the status of the work process and is responsible for managing its behavior

The benefits of doing this are:

A. How much system resources are not consumed by the management process

B. The management process is responsible for monitoring the status of the worker process, and if a worker process dies, the management process is responsible for creating a new worker process to avoid system performance degradation. Improve the reliability of the system.

c. Management Process Support Nginx The program upgrade and configuration item modification in service operation make dynamic extensibility, dynamic customization and dynamic evolution easier.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Nginx Series (three)--management process, multi-work process design

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.