⬆️ ⬇️

OS and web server - more fun together

Hello to all. At first, I thought that it would be best to continue the nginx theme with the example of a cleaned up and commented out config from one of the combat servers, but then I realized that this would require a rather large amount of additional information. Therefore, I decided to try, for starters, to tell in general about how nginx works, and why it works so fast.





You need to start by understanding how a server with a single conditional processor can handle more than one network request at a time. Everything has long been aware of the division of CPU time between tasks and about timer switching tasks in modern operating systems. But if there was only one CPU in the computer, he could not carry out more than one request at the same time. The division of tasks into quanta, and their alternation would not lead to a decrease, but to an increase in the total time spent on all tasks.



The thing is that in modern computers there is not one processor. :) Even if your CPU has just one core, a lot of other chips help it. The main ones are the DMA controller (s), a specialized controller on the network board, a hard disk controller, a video card processor (although it is not yet used in web servers), and so on. All these devices can take on a lot of highly specialized work, freeing the CPU to perform other tasks.

')

The operation of the operating system is to correctly distribute the load among the devices found in your computer. For example, a processor might say to a DMA controller that “they transferred this data from a disk, into this section of memory,” and do something else. What exactly is the “other” determines one of the special planning algorithms, each with its own advantages and disadvantages (I advise you to read about a very good book by Mr. Tannenbaum “Modern Operating Systems”). But it is not about that now.



I would like to draw your attention to the fact that the architecture of modern x86 computers itself imposes asynchronous execution of tasks to increase the performance of the system as a whole. The operating system can hide this asynchrony from users by providing synchronous system calls , but you can win a lot by writing a program, taking into account what happens under the hood.



The truth is there is a small problem. Operating systems, traditionally, were designed in such a way that the executed programs could not even suspect that "they are not alone here." And even the means to perform fast asynchronous system calls appeared relatively recently in Linux, FreeBSD and Windows (I do not mean select and poll).



As a matter of fact, I think you already understood what I am leading to. Nginx works very quickly because it does not spawn a separate process (thread) for each request processed by the user, but performs all tasks in one process, using asynchronous system calls for I / O operations (networking, and hard disk). This approach has several advantages:



- all work can be performed by one process, or, in general, N processes, where N is the number of cores in your server;

- the number of switching between execution contexts is minimized;

- there is almost no need to synchronize processes or threads;

- decreases memory usage;



This approach has its drawbacks, the main of which are the need for a very careful approach to server programming, and the inability to use blocking I / O operations. More precisely, of course, you can use blocking operations, but this will stop the ALL web server process and the maintenance of all connections until the blocking is complete. In particular, this is one of the problems with the use of Perl embedded in nginx. With any I / O operation, it can be blocked - and this slows down all the work.



In conclusion, I would like to add that the speed of your server with nginx largely depends on the operating system and its settings. Proper tuning of the OS, under heavy loads, you can increase server performance by many tens of percent.



PS Thanks to my friends and colleagues for beta-testing the text. :)

Source: https://habr.com/ru/post/66115/



All Articles