How does NodeJs work? An explanation of its architecture

September 13, 2021

nodejs

 

 

When it comes to building BackEnd services, NodeJs is one of the best options. This open source, cross-platform, back-end JavaScript runtime is ideal when you want to power applications that run within a web browser or mobile device.

 

Applications built with Node are scalable and this is due to its asynchronous nature. What does this mean? For example, imagine that in a restaurant, the waiter takes your order and takes it to the kitchen, then they continue to the next table while the chef prepares the food for the first table, so he can serve several tables while they are preparing the food of the others, this is what is called asynchrony.

 

This is how an application built with NodeJs works

 

When we take this metaphor to the practical field of NodeJs applications, the server is like a connected thread to handle an order, so a single thread is used to handle multiple requests.

 

When the server receives a request, the thread will take care of it. If you need to create a database, the thread should not wait until it is created to fulfill another request, it does so while the database is being created.

 

 

When the database already has what is requested in the request, a message is sent to what is known as an "event queue". Node is continually monitoring this queue in the background, when it gets a ready event, it pulls it out and processes it.

 

This architecture makes NodeJs ideal for building applications that include disk or network access. More tasks and orders can be executed without the need to include extra hardware and therefore these applications are highly scalable.

 

In which cases should NodeJs be used?

 

NodeJs should not be used for CPU intensive applications, for example for video encoding. In these types of applications, many calculations must be made through the cpu and few operations that touch the file system or the network.

 

Since Node applications are single threaded, when they do the calculations to serve one client, the others have to wait, and therefore should not be used for applications with a intensive cpu or where multiple calculation processes must be performed.