Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internals of node.js. How does it actually work

Perhaps somebody who implemented node.js module can explain the protocol between node.js queue processed on a single thread and a blocking IO operations that will be performed by a module.

I suspect that it goes something like this:

  1. node.js thread registers a callback in a form of a closure and saves it with some correlation id.
  2. node.js invokes a method (which should perform blocking IO) on a module and passes method parameters and correlation id to it.
  3. module method spins off a thread and blocks on IO operation.
  4. when IO operation completes, modules' thread calls back to node.js thread and passes results and correlation id to it.
  5. node.js thread finds stored callback closure by correlation id and invokes it with result returned from module.

Question 1: Is above sequence correct?

Question 2: What exactly is node.js queue? Is it the part where epoll, kqueue or IO completion port on windows is used? Is it a callback mechanism for module to notify node.js thread that some IO had finished? How does it work?

like image 733
Sharas Avatar asked Dec 20 '11 12:12

Sharas


People also ask

How does Node.js works internally?

Node JS Web Server internally maintains a Limited Thread pool to provide services to the Client Requests. Node JS Web Server receives those requests and places them into a Queue. It is known as “Event Queue”. Node JS Web Server internally has a Component, known as “Event Loop”.

How do Node.js works?

It is a used as backend service where javascript works on the server-side of the application. This way javascript is used on both frontend and backend. Node. js runs on chrome v8 engine which converts javascript code into machine code, it is highly scalable, lightweight, fast, and data-intensive.

How does Node.js threading work?

Node. js runs JavaScript code in a single thread, which means that your code can only do one task at a time. However, Node. js itself is multithreaded and provides hidden threads through the libuv library, which handles I/O operations like reading files from a disk or network requests.

How does Node.js runtime work?

Nodejs is a Javascript runtime based on the google v8 engine and that's why it appears here as a dependency and the v8 engine enables Nodejs to understand the javascript code that we write. The v8 engine is what converts javascript into a machine language that the computer can understand.


1 Answers

Node.js doesn't really manage any of this quite as you speculated. It instead relies on the OSs to do most of the async IO. It uses select/epoll/kqueue depending on the operating system. "They" just put a call out and the OS calls back with a stream, chunks, etc... As far as the evented portion of it, this is built into V8, it does all the work tieing callbacks to specific events same as it does in the browser. Finally, you can look into libuv, which was written along with node and is now all maintained by Joyent. It's open source on Github so you can browse through the code if you really want the details =D

like image 65
Scott Avatar answered Oct 21 '22 06:10

Scott