How does NodeJs work

... running JavaScript in the backend ?

Javascript is a language that came to the world to help the web running code on the client devices. Along with HTML & CSS, we were able to use javascript to program a very advanced behaviour that we can defer its execution on the client side, in a ligthweight web architecture.

Now javascript runs some very complex framework that can make complete Single Page Applications and leveraging core functionalities to do more on the client’s devices, even with offline access.

Javascript is meant to run on browsers, that provide Web APIs and container needed for the web.

But Javascript is no more a frontend language, it has been a while since it became also used to build backend servers with access to databases and so. Thanks to NodeJs, we now can run complete entreprise applications on javascript, either for the backend or the frontend.

So how is it possible to run Javascript outside the browser ?

Event Loop

NodeJs uses event based architecture for handling tasks. The program will have two sort of tasks, synchronous and asynchronous.

Node executes synchronous tasks in the main thread, and when it encounters an synchronous one, it will invoke the corresponding system API (ie I/O) and registers the call back in the callback queue. In the meanwhile it continue to execute next task. When there are no more tasks to do, meaning that the call stack is empty, the event loop will start popping the callbacks from the queue and bring them to the scope, executing them on the main thread.

Non blocking I/O

Syncronous tasks goes on the stack, synchronous like the I/O goes to the system API (or web API in the browser context), in a non-blocking way, nodeJs once completed places the callback in the callback queue. Once the stack empty, it takes the first thing on the queue and executes it. So the JS runtime is single threaded, but other system API aren’t. They are handled by the system and executes in the C++ runtime.

V8

V8 is a performant javascript engine written by Google in C++. It is used in chrome as well as in NodeJs.

It is important to note that javascrip engines (ie V8) does not provide any synchronous APIs, they handles only the javascript runtime. For example, the setTimeout, XHR and DOM API are provided by the browser API and does not exist in the V8 source code. For the web they are provided by the browser, and outside by Node’s system API.

Single Threaded ?

YES nodeJs receives all HTTP requests using only one thread. It does not mean that the servers waits for the request to finish before serving another.

It just uses Event Queue, the main thread receive the request and queue it to be handled in the event loop and then serves the response once the callback fired. The other system tasks in which node calls C++ code is still multithreaded.

Conclusion

First time I saw poeple intersted on nodeJs, I thought it should have more drawbacks using Javascript on the backend, comparing it to Java, C#, Ruby … and the advanced features they offer to handle performance, concurrency … Things that are most importing on the backend side. But now I think that NodeJs, and thanks to its architecture, can solve some backend problem and provide solid stack for many entreprise applications. Althought, I think it still will be lacking many advanced control over the concurrency, multithreading, memory management that are crucial to the most of the entreprise backend applications.