Nginx study notes (c) -Nginx of I / O model Comments

A, web request processing

More than 1.1 way process

Multi-process mode: Each server receives a client request to have the server Primary processGenerate aChild processResponds to the client until the user closes the connection
advantages: fast processing speed, independent of each other child process
Disadvantages: accessing too much can cause depletion of server resources can no longer provide the requested

More than 1.2-thread

Multiple threads: : With multi-process mode is similar, but each will receive a client request Service processDerive aThreadCome and client side interaction.
Pros: cost far less than a thread of a process , thus reducing the required web server on system resources
Cons: When multiple threads within the same process in the working, they can access the same memory address space to each other, so the
threads influence each other
,Once the main process hang all child threads are not working, IIS server uses a multi-threaded mode, you need to reboot a time lag to stabilize.

1.3 asynchronous

Asynchronously: nginx's epoll, apache of the event, etc.

Second, synchronous asynchronous, non-blocking and blocking Nginx the I / O model

2.1 synchronous and asynchronous (applications interact with the kernel)

  • Synchronize: After issuing process data, so after kernel returns a response before continuing to the next request, that is, if the kernel will not return data, then the process has been so on, until forever, crash error.
  • asynchronous:After the process of issuing the data, ranging from the kernel returns a response, and then processing the next request, the kernel processing procedure by the callback function. Nginx is asynchronous.
  • Synchronous and asynchronous message communication mechanism is concerned, A synchronization mechanism, all requests obtained server-side synchronization, the sender and receiver of the request processing step is the same; in the asynchronous mechanism, all requests from the sender forming a queue, after the receiver processing completion notification sender.
  • For popular example: You call to ask Bookseller there is no "Arabian Nights" this book, if it is synchronous communication mechanism , bookstore owner would say, you wait, "I'll check," and then began to check ah check, check and other good (probably five seconds, it could one day) tell you the result (return result). The asynchronous communication mechanism , the bookstore owner to tell you that I check ah, good call your check, and then directly to hang up (does not return a result). Then check Well, he will take the initiative to call you. Here the boss callback through the "call back" in this way.

2.2 blocking and non-blocking (kernel interacts with the IO devices)

Can be understood as interaction kernel and IO device, when the kernel process requests received IO data handling time, can be simply understood as the kernel needs to do one thing can not immediately return a response, if not immediately returned, need to wait , it is blocked, otherwise it can be understood as non-blocking.

  • Blocking: IO call can not immediately return a result, a process that is initiated when the IO request can not be satisfied immediately, the process will wait until the response kernel, the kernel should copy the data from the IO device to the kernel space, and then returned to the process, which is blocked .
  • Non-blocking: IO call can return immediate results, when a process initiated by the IO process can not meet immediately, not waiting, but over and over again to see if the poll is complete and the next call IO executed immediately.

2.3 Nginx support concurrency model

nginx supports multiple concurrent model, specific concurrency model implementation vary depending on the platform.
On the concurrency model supports a variety of platforms, nginx automatically select the most efficient model. But it may also be explicitly defined in a configuration file concurrency model use directive.

  • select:IO multiplexing, standard concurrency model. When compiling nginx, if the platform is not used for more efficient concurrency model, select the module will be automatically compiled. Options configure script: -with-select_module and --without-select_module can be used to forcibly enable or disable compile select modules.
  • poll:IO multiplexing, standard concurrency model. And select Similarly, when compiling Nginx, if the platform is not used for more efficient concurrency model, poll module will be automatically compiled. Options configure script: -with-poll_module and --without-poll_module can be used to forcibly enable or disable poll compiled module
  • the epoll: the IO multiplexing, high concurrency model, can be used in the core and above Linux 2.6+
  • kqueue; IO multiplexing, high concurrency model, can be used in FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0, and Mac OS X platform
  • /dev/poll: Efficient concurrency model, can be used in the internet + Solaris 7 11/99 +, HP / UX 11.22+ (eventport), IRIX 6.5.15+, and Tru64 UNIX 5.1A
  • Event port: Efficient concurrency model, can be used for Solaris 10 platforms, PS: due to some known problems recommended / dev / poll instead.

1, Why epoll fast?

Compare the Apache common select, and epoll Nginx common

select:

Limit the maximum number of concurrent, because a process of the open FD (file descriptor) is limited by the FD_SETSIZE set, the default value is 1024/2048, the maximum number of concurrent Select model was correspondingly restricted.

Efficiency, select each call will scan all linear set of FD, so that efficiency will be presented linear decline, the consequences of FD_SETSIZE big change is that we are slowly, what? All overtime.

Kernel / user-space memory copy of the problem, how to tell the kernel to FD notifies the user space? In taking on this issue select memory copy method, FD in a lot of time, very time-consuming.

Summarized as follows: 1, limited number of connections 2, 3 to find the pairing slow, data copied to the user by the kernel mode time consuming

epoll:

Epoll no limit on the maximum concurrent connections, the upper limit is the maximum number of files can be opened, this number is much larger than 2048 in general, this number is generally great, and the relationship between the system memory, the specific number may cat / proc / sys / fs / file-max View.

Efficiency gains, the biggest advantage is that it just you Epoll "active" connection, but nothing to do with the total number of connections, so in a real network environment, Epoll efficiency will be much higher than the select and poll.

Memory sharing, Epoll use of the "shared memory" In this regard, the memory copy is omitted.

For example:
Suppose you study in college, to wait for a friend to visit, and this friend know you are in Building A, but do not know specifically where to stay, then you made an appointment to meet in A Building door.
Use is 阻塞IO模型to deal with this problem, then you can only have been waiting at the door waiting for the arrival of Building a friend, at this time you can not do anything else, it is easy to know the efficiency of this approach is low.
use 多路复用IO模型to deal with this problem. you tell your friends to find the floor of the a building tube aunt, let her tell you how to get there. Aunt floor where the pipe is to play the role of IO multiplexing.

  • Aunt version to do is select the following things: for example, the students A's friends, and select edition Aunt stupid, she took a friend who is querying the room one by one A classmate, your other friends, and so in actual code , select version aunt to do the following things:

     int n = select(&readset,NULL,NULL,100);  
      for (int i= 0; n > 0; ++i) {
      if (FD_ISSET(fdarray[i], &readset))
      {
          do_something(fdarray[i]);
          --n;
      } } ```
    
  • Aunt epoll version is more advanced, and she jotted down the information the students A's, for example, his room number, wait time students A friend coming, friends, classmates only need to tell the armor in which room can be, do not have to personally take . the people who filled the building to find a version of Aunt so do epoll can use the following code says:

     n=epoll_wait(epfd,events,20,500);  
      for(i=0;i<n;++i)
       {
        do_something(events[n]);
         } ```
    
    

Nginx's IO model is based on event-driven , making applications to quickly switch between multiple IO handle, to achieve the so-called asynchronous IO. Event-driven server is most suitable for IO-intensive work , such as a reverse proxy, which plays a role in the transfer of data between the client and server WEB purely IO operation itself does not involve complex calculations. Reverse proxy to do with the event-driven, clearly better, a worker processes to run, there is no process, thread management overhead, CPU, memory consumption is small.

Apache server such applications, the general run specific business applications, such as scientific computing, graphics, images and the like. They are likely to be CPU-intensive services, event-driven is not appropriate. For example, a computing took 2 seconds, 2 seconds, is completely blocked, what event would be useless. Think about what would happen MySQL if the change of event-driven, or join a large sort of living will block all clients. This time multi-process or thread to demonstrate the advantages of each process all of the dry matter, and do not obstruction and interference. Of course, the modern CPU faster and faster, blocking a single computing time may be small, but as long as there is obstruction, event programming to no advantage. So processes, threads of such technology, and will not disappear, but complementary and event mechanism, long time.

Overall, event-driven service suitable for IO-intensive, multi-process or thread is suitable for CPU-intensive services, in fact, that is more suitable for nginx front-end proxy, or static files (especially under high concurrency), but apache suitable for back-end application servers, powerful [php, rewrite ...], high stability.

Published 102 original articles · won praise 21 · views 5309

Guess you like

Origin blog.csdn.net/ranrancc_/article/details/102684755