So, I began writing all my backend projects in Node JS without even giving a second thought. After few projects, I realized that while Node JS fits in really well for most of my projects, it does not perform nicely for some. After some research, I finally found the answer to my question: “When exactly should I use Node JS ?”.
To understand this we need to see how Node works !
Understanding the way a Node app runs.
Node JS follows event based programming. This is the absolute core of Node. It’s a single threaded program that acts upon callbacks and never blocks on the main thread.
Hence, instead of spawning a new thread for every new user, node loops through events of all the users in a single thread itself. This avoids thread creation overhead. So, ideally the main thread only listens for events and nothing else. It never gets blocked because of the way node forces you to write code. The code you write should always be asynchronous and callbacks driven.
Take a look at the diagram below:
Events can be any action, like HTTP request or even callback from any I/O operation for which the application was waiting. The event loop processes every event that comes up in the event queue. Hence, whenever an event occurs, the event is processed and the associated callback is called. This is why we hear devs say: Node JS never blocks !
Now, there are two types of applications based on how they behave.
- CPU intensive - These applications require large amount of cpu cycles for their operations. For example : Heavy mathematical calculations, data crunching , Image processing ,etc.
- I/O intensive - These applications perform mostly Input/Output operations. For example: Database access, file reading and writing, responding to HTTP requests and so on.
CPU intensive applications are best suited for multi threaded systems, obviously because they will be performed by multiple cores simultaneously. But turns out node doesn’t work that way. Node based apps are single threaded. Hence, a single thread will need to perform all the cpu intensive jobs which will make it very slow. This is the reason if you have ever tried building image processing apps on node, you would have been pretty disappointed at the speed.
While, incase of I/O operations, node works really well.
This is because I/O operations in node are not carried out in the main thread. Most of the I/O is written as non blocking asynchronous. But certain background I/O are insanely difficult to write in asynchronous manner. For those I/O apis are executed in a separate threads and processes for DB access and process execution. Also, certain file operations are performed by OS. Internally, Node JS relies on
libev to provide the event loop, which is supplemented by
libeio which uses pooled threads to provide asynchronous I/O.
Thus the main even loop only focuses on handling the events and not the associated operations, making the response time very fast.
Due to the way NodeJS works we can come to the conclusion that :
Potential I/O intensive application - Node JS might be the best choice ✔
Potential CPU intensive application - Node JS will be very slow ✘
Go for multithreaded systems like Java.
As most of the web applications are not CPU intensive in nature, Node still wins and is my first preference !!!