K
K
KIQIK2022-04-02 16:05:22
Node.js
KIQIK, 2022-04-02 16:05:22

How does Node.js use computer resources?

I am finishing a node course and wanted to understand the operation of this platform in more detail. In the provided course, it was stipulated that Javascript itself is a synchronous language, but the Node.js API (file system, Internet requests) that we can connect and using a translator that binds Javascript function calls to C ++ calls, use the libuv library, which is written in pros and uses multithreading, and is also asynchronous.

There is a V8 engine -> Node.js API (http, fs, path) -> Node.js Bindings-> Libuv(c++, asynchronous i/o, multi-thread).
Node.js simply passes the request to Libuv to perform the task by contacting the operating system and will be notified when the task is completed, with the whole process Node.js working on accepting the requests. The platform acts as a manager, degenerating all the work to the Libuv library and operating system that is not done directly in Javascript.

Libuv itself has an EventLoop, it processes all incoming requests as events and registers a callback function for each, which, after the signal is completed, will be executed and return the result to the end machine.
If this is the task of communicating with the server over the Internet, then it is performed directly in the operating system, while other tasks, such as reading a file, are performed in the Thread Pool.Libuv itself has a thread group. One thread executes the V8 engine and the EventLoop, and also a number of threads (4 by default), each of which can perform one task at a time. But where possible, requests are executed directly by the operating system, instead of executing them through a thread, since this requires a little more management resources.
The definition in bold refers directly to the question
. The events themselves are divided into Event Loop Phases: Timers -> I/O callbacks -> setImmediate -> Close callbacks and are executed in that order.

We can also use clusters or the pm2 package, which is based on this idea, and deploy a nodejs instance for processing requests on a given number of processor cores. Since if we have one instance, then with complex cryptographic, sorting and working with complex objects during JSON transformations, EventLoop can block and the server will accept the next request on another instance.

I wrote a general retelling of the learned information, perhaps somewhere wrong. The question is about threads and processes, their definitions and connection with computer resources. If you take php, then for each request a separate process is created and they are managed by apache, minus the approach that all this needs to be managed, and therefore additional computer resources are needed. Of course, I understand that these are not minuses and not pluses, it all depends on the task, on the skills of the developer, etc.
But if we stop at nodejs, which is essentially a js code runtime on the server side and as additional tools we can hang Nginx on top, then explain to me in more detail about the thread pool in the node itself and the role of processor cores.

Let's say we have 4 logical and physical cores. A node instance is deployed on each core using the pm2 package.
Each instance is a real-time executor of the Javascript language and the ability to connect modules that will add functionality for working with files, requests, etc. by accessing the Libuv library.
Thread pool - I imagine it as a stack of operations, functions that are independent of each other and use the processor's ability to directly calculate or delegate work to a hard drive, network card, etc.

1. Each thread can be allocated on one core, that is, the V8 engine is running on one core and additionally allocated threads for asynchronous tasks?
I just thought that just additional threads should be allocated on other logical, physical cores.
When Node.js is running on one core, and the Libuv library itself, if necessary, creates threads by means of system calls to free cores.
2. Or if we allocate a Node.js instance for each processor, then there is no thread pool in principle and everything is executed in turn, not asynchronously.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rPman, 2022-04-02
@KIQIK

nodejs is ideologically single-threaded, the fact that it can have threads inside it is to implement the asynchronous behavior of obviously synchronous things ...
after version 10.5, worker_threads was added, where a developer can create real threads, but to access data from neighboring threads, you need to use the appropriate data types , for example, send messages or create a SharedArrayBuffer ...
ps since nodejs has not delivered adequate data structures for multi-threaded access, then all the work will be covered with a bunch of extra code to implement the missing functionality, so if it's so critical, it's better to implement important places in more adequate languages programming

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question