Nginx the relationship between processes

Source: Internet
Author: User

Deployment Nginx uses a master process to manage multiple worker processes, typically the same number of processes as the number of CPUs on the server, and each worker's process is busy. They really provide Internet services, the master process is only responsible for monitoring the management of the worker process, the worker process through the sharing of memory, atomic operations and other processes between the communication mechanism to achieve load balancing mechanism.

Why the product environment is in accordance with the Master-worker way. Because the master process does not provide a request service to the user and is used only to manage the worker process that truly provides the service, the master process is unique, focusing only on its own pure administration, providing command-line services to administrators, including starting services, stopping services, reconfiguring files, smoothing upgrades, and so on. The master process's permission level is generally greater than or equal to the worker process multiple worker processes processing Internet requests can not only improve the robustness of the service (after a worker process error, the other processes can still provide services), and most importantly, This can take advantage of the current common SMP multi-core architecture to achieve micro-real multicore concurrency processing. Why should the number of worker processes be set to match the CPU core number? This is the difference between Nginx and the Apache server.
Each process on Apache processes only one request at a time, so if you want the Web server to have more concurrent processing requests, set the number of processes or processes in Apache more, typically one server has hundreds of worker processes, so a large number of process transitions lead to the so-called The consumption of system resources. Nginx is not the case, a worker process can handle the size of only limited memory at the same time, on the schema, there is almost no synchronization lock limit when processing concurrent requests between different worker processes. The worker usually does not go to sleep state, when the number of processes Nginx is equal to the number of CPUs, in theory, the cost of switching between processes is minimal. Summary

Someone may have to ask, nginx use more than a worker to process requests, each worker has only one main thread, that can handle the number of concurrent is very limited Ah, how many worker can handle how many concurrent, how high concurrency it. No, this is Nginx's genius, Nginx uses asynchronous non-blocking to process requests, that is, Nginx can handle thousands of requests at the same time. Think about the common way Apache works (Apache also has asynchronous non-blocking version, but because it and some of the modules that conflict, so it is not common), each request will monopolize a worker thread, when the concurrency count to thousands of, while thousands of of the thread in the processing request. This is a big challenge for the operating system, the memory footprint of the thread is very large, the CPU overhead of the thread's context switch is very high, the natural performance is not going to go, and these expenses are completely meaningless.

Why is it that nginx can be handled in an asynchronous non-blocking manner, or what is the problem with asynchronous non-blocking? Let's go back to the original point and look at the complete process of a request. First, request to come over, to establish a connection, and then receive data, receive data, then send data. Concrete to the bottom of the system, is read and write events, and when the reading and writing events are not ready, must not operate, if not without blocking the way to call, it will have to block the call, the event is not ready, it can only wait, and so on, and so on, you continue. Blocking calls go into the kernel and wait, CPU will let go out to others use, for the single thread of the worker, obviously not appropriate, when the network events, we are waiting, the CPU idle down no one to use, the CPU utilization of natural, not to talk about high concurrency. Well, you say plus the number of processes, what's the difference between this and the Apache threading model, notice that you don't add unnecessary context switches. So, inside the nginx, the most taboo blocking system is called. Don't block, it's not blocking. Non-blocking is, the event is not ready, immediately return to Eagain, tell you, the incident is not ready, you panic about what, will come again. Well, after a while, check out the event until the event is ready, during which time you can do something else and then look at the event. Although not blocked, but you have to come to check the status of the event, you can do more things, but the cost is not small. Therefore, there will be asynchronous non-blocking event handling mechanism, specific to system call is like select/poll/epoll/kqueue such a system call. They provide a mechanism that allows you to monitor multiple events at the same time, calling them to be blocked, but you can set the timeout time, within the timeout period, and if there are events ready, return. This mechanism just solves two of our problems, take epoll as an example (in the following example, we take epoll as an example to represent this kind of function), when the event is not ready, put into the epoll inside, the event is ready, we go to read and write, when read and write back to Eagain, We added it to the epoll again. So, as soon as the event is ready, we will deal with it and wait in Epoll only if all events are not ready. In this way, we can handle a lot of concurrency concurrently, of course, the concurrent request here, refers to the unhandled request, the thread has only one, so at the same time can handle the request of course only one, but in the request to constantly switch, but also because the asynchronous event is not ready, and the initiative to let go. There is no cost to switching here, and you can understand that there are many prepared events to be recycled, and this is actually the case. Compared to multithreading, this event handling method has great advantages, do not need to create a thread, each request occupies a small amount of memory, no contextSwitching, event handling is very lightweight. No amount of concurrent numbers will cause unnecessary waste of resources (context switching). More concurrent numbers will only consume more memory. I have previously tested the number of connections, the number of concurrent requests processed on 24G memory machines reached 2 million. Now the network server basically uses this way, this is also the main reason that nginx performance is efficient.

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.