[Reading Notes] 1 _ exploring nginx architecture _ 2 _ event processing mechanism he laigao concurrency

Source: Internet
Author: User
Tags epoll
: This article mainly introduces [reading notes] 1 _ exploring nginx architecture _ 2 _ how to handle high concurrency events. if you are interested in PHP tutorials, refer to it. Original article: http://tengine.taobao.org/book/chapter_02.html

Nginx uses multiple worker methods to process requests. each worker has only one master thread, so the number of concurrent jobs that can be processed is limited. how many workers can process the number of concurrent jobs, why is it high concurrency?

Nginx uses an asynchronous non-blocking method to process requests. This asynchronous non-blocking event processing mechanism is specific to system calls such as select/poll/epoll/kqueue. They provide a mechanism for you to monitor multiple events at the same time, and call them is blocked, but you can set the timeout time. if an event is ready within the timeout time,.

Taking epoll as an example (in the following example, we use epoll as an example to represent this type of function)

  1. Put the event in epoll when it is not ready
  2. When the event is ready, we can read and write it.
  3. When read/write returns EAGAIN, we add it to epoll again.

In this way, as long as an event is ready, we will handle it. only when all events are not ready will we wait in epoll. In this way, we can process a large number of concurrency.

Of course, the concurrent requests here refer to the unprocessed requests with only one thread, so there is only one request that can be processed at the same time, but it is only a constant switch between requests, the switchover is also because the asynchronous event is not ready, and the switch is made automatically. There is no price for switching here. you can understand it as loop processing of multiple prepared events. In fact, this is the case. Compared with multithreading, this event processing method has great advantages. no threads need to be created, and each request occupies a small amount of memory. there is no context switch, and event processing is very lightweight. A large number of concurrent operations will not cause unnecessary resource waste (context switching ).

More Concurrency only occupies more memory.

The number of worker processes mentioned earlier can be set. Generally, the number of worker processes is set to be the same as the number of cpu cores of the machine. The reason is inseparable from the nginx Process Model and event processing model.

We recommend that you set the number of workers to the number of cpu cores. it is easy to understand here that more workers will only cause the process to compete for cpu resources, this leads to unnecessary context switching. In addition, to better utilize the multi-core features, nginx provides cpu affinity binding options. we can bind a process to a certain core, in this way, the cache will not become invalid due to process switching. Such a small optimization is very common in nginx, and it also shows the painstaking efforts of nginx authors. For example, when nginx compares four-byte strings, it converts the four characters into an int type and then compares them to reduce the number of cpu commands.

'). AddClass ('pre-numbering '). hide (); $ (this ). addClass ('Has-numbering '). parent (). append ($ numbering); for (I = 1; I <= lines; I ++) {$ numbering. append ($ ('
  • '). Text (I) ;}; $ numbering. fadeIn (1700) ;}) ;}; script

    The above introduces [reading notes] 1 _ exploring the nginx architecture _ 2 _ how to handle high concurrency of the event mechanism, including some content, and hope to help those who are interested in the PHP Tutorial.

    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.