Node. the selling point of js is "asynchronous single-thread". Although many mainstream Web backend programming languages have good support for asynchronous programming, only Node. js forces asynchronous all IO. Python and Ruby also have such a framework, but because it is inevitable to use a library containing Synchronous Code in actual use, it cannot grow up, but in Node. before js, JavaScript's server-side programming was almost blank, so Node. javascript can establish a code library where all IO operations are asynchronous.
The bottleneck of most Web applications is I/O, that is, read/write disks, read/write networks, and read/write databases. What policies are used to wait for this period of time has become a key point to improve performance.
PHP policy: multi-process running, directly waiting for IO completion. Disadvantage: multiple processes consume multiple memories, making it difficult to share data between processes.
C/C ++'s general strategy: multi-threaded running, the Program maintains the lock status by itself. Disadvantages: high development costs, error-prone, and difficult to debug.
Python (Tornado): multiple requests are executed in turn in a single process and switched to another request when I/O is encountered. Disadvantage: for a single request, there is still no time for the most efficient use.
What is "the most efficient use of time 」? For example, if there are two unrelated database queries, one is usually executed in PHP, and the second is executed after the execution is completed (the total time is a + B ). obviously this is not the most efficient. We should execute two queries at the same time. The time is max (a, B ).
The problem with Python and other languages that support multithreading is that at the language level, it is difficult for programmers to tell virtual machines that two operations should be executed at the same time, even if there is a solution, it is quite troublesome, most people are too lazy to use it (and it is not worth it ). Because Node. js forces all I/O asynchronous execution with extreme severity, Node. javascript programmers can also be said to be familiar with it. Combined with some promise and async libraries, they can easily execute unrelated operations in parallel.
The implementation of asynchronous IO is discussed above. What are the advantages of asynchronous IO. In fact, asynchronous IO does not magically reduce the pressure on the server. The server should be added, but asynchronous IO will reduce the time of a single request, remove meaningless wait times in a single request. Therefore, the number of requests processed per unit time does not change, but the processing time of each request is reduced. From this perspective, the server also saves some resources-that is, the memory consumed by each request connection is maintained.