How does node.js work?

I do not understand a few things about nodejs. Each source of information says that node.js is more scalable than standard streaming web servers due to the lack of blocking threads and context switching, but I wonder if node.js does not use streams, how to handle parallel requests in parallel? What does the event I / O model mean?

Your help is greatly appreciated. Thanks

+47
parallel-processing
Feb 29 '12 at 10:06
source share
3 answers

Node is fully event driven. Basically, a server consists of one thread processing one event after another.

A new query is a type of event. The server starts processing it, and when there is a blocking I / O operation, it does not wait for completion and instead registers a callback function. Then the server immediately starts processing another event (possibly another request). When the I / O operation is completed, it will be a different type of event, and the server will process it (that is, continue to work on the request) by making a callback as soon as it has time.

Therefore, the server never needs to create additional threads or switch between threads, which means that it has very little overhead. If you want to make full use of multiple hardware cores, you simply run multiple instances of node.js

Update At the lowest level (C ++ code, not Javascript) there are actually several threads in node.js: there is an IO worker pool whose job is to get I / O interrupts and put the appropriate events in the queue to be processing the main thread. This will prevent the interruption of the main thread.

+77
Feb 29 '12 at 10:22
source share

Although the question has already been explained until a long time, I put my thoughts on the same thing.

Node JS is a single-threaded technology. Essentially, the problem with the creator of JS (Ryan Dahl) was that parallel processing using multiple threads is not the right way or too complicated.

if node.js doesn't use threads, how does it handle concurrent requests in parallel

Ans: This is a completely wrong sentence when you say that it does not use threads, Node Js uses threads, but intellectually. It uses a single thread to serve all HTTP requests and multiple threads in a thread pool (in libuv) to handle any blocking operation

Libuv: A library for handling asynchronous I / O.

What does the event I / O model mean?

Ans: The correct member is non-blocking I / O. It is almost never blocked, as the official Node JS website says. When any request is sent to the Node server, it never queues the request. It accepts the request and starts execution, if it blocks the operation, then goes to the workflow area and registers a callback for the same one, as soon as the code execution completes, it calls the same callback and goes into the event queue and is processed by the event loop again after who create a response and send to the appropriate client.

Useful link: click here

+1
Aug 17 '17 at 13:22
source share

Node JS is a JavaScript runtime. Both the browser and Node JS are powered by the V8 JavaScript engine. Node JS uses an event-driven non-blocking I / O model that makes it easy and efficient. Node JS applications use a single-threaded event loop architecture to handle concurrent clients. In fact, its main event loop is single-threaded, but most of the I / O operations work in separate threads, because the Node JS I / O APIs are asynchronous / non-blocking in their design to accommodate the main event loop. Consider a scenario in which we query the database database for parts user1 and user2, and then print them on the screen / console. The response to this request takes time, but both user data requests can be executed independently and simultaneously. When 100 people connect at the same time, and do not have different flows, Node will loop these connections and fire any events that your code should know about. If the connection is new, it will notify you. If the connection sent you data, it will notify you. If the connection does nothing, it will skip it, instead of taking up the exact CPU time on it. Everything at Node is based on responding to these events. Thus, we can see the result: the central processor remains focused on this one process and does not have a bunch of threads for attention. There is no buffering in the Node.JS application; it simply outputs data in batches.

+1
Apr 7 '19 at 1:26
source share



All Articles