Asynchronous IO performance of node. js

  • 2020-03-30 04:02:28
  • OfStack

Python and Ruby also have frameworks like this, but they didn't grow because of the inevitable use of libraries with synchronous code, and before node. js, JavaScript server-side programming was almost empty, so node. js was able to build a code base where all IO was asynchronous.

The bottleneck for most Web applications is IO, read/write disk, read/write network, read/write database. What strategy you use to wait for this time becomes the key to improving performance.

PHP's strategy: run multiple processes and wait for the IO to complete. Disadvantages: multiple processes consume multiple portions of memory, making it difficult to share data between processes.
C/C++ common strategy: multi-threaded running, the program itself to maintain the lock state. Disadvantages: high development cost, error prone, not easy to debug.
Python(Tornado): multiple requests take turns in a single process, switching to another request when an IO is encountered. Cons: still not the most efficient use of time for a single request.
What is "the most efficient use of time"? For example, there are now two unrelated database queries. In PHP, one is usually executed first, and the second is executed after the execution (the total time is a + b). Obviously, this is not the most efficient, and two queries should be executed at the same time, and the time is Max (a, b).

The above mentioned asynchronous IO implementation, so what exactly is the advantage of asynchronous IO. In fact, asynchronous IO does not magically reduce the pressure on the server, the server should be added as well, but asynchronous IO will reduce the time of a single request, eliminating the meaningless waiting time in a single request. So the number of requests processed per unit time has not changed, but the processing time per request has decreased. From this perspective, the server also saves some resources -- that is, the memory it consumes to maintain a connection per request.


Related articles: