What is Node.js?
Node.js is well accepted by leading companies like Netflix, LinkedIn, Uber and PayPal.
Initially, Node.js was created to build real-time websites with push capability. Inspired by applications like Gmail. Node.js creator gave developers a tool for working in the non-blocking, event-driven I/O paradigm.
After over a long period the web development frameworks were stateless, a stateless model is where data generated in one session for a user is not maintained for usage with the next session for the same user.
Finally, with Node.js there is a way to maintain session information between requests for a user. Node.js helps to have real-time two-way communications between client and server, where both server and client can initiate a communication, allowing them to exchange data easily.
Why use Node.js?
Node.js is increasing its popularity day by day but what makes it so popular among developers. Well over two decades of web applications were based on a stateless request-response paradigm were developers have to put a lot of effort to maintain the session between requests for a user.
But with Node.js web applications are stateful, now your applications are real-time and have two-way communications between client and server, both client and server can initiate a communication.
With all these advantages, Today, Node.js is widely used and plays a critical role in the tech stack of many high-profile companies, they heavily depend on its unique benefits.
How Does it Work?
The architecture of the Node.js mainly depends on the use of non-blocking, event- driven I/O to remain lightweight and efficient in the face data-intensive, real-time applications.
Node.js stands out well among the back-end services because of its asynchronous nature, also this is why Node.js is widely adopted by developers.
What is I/O?
Stands for Input and Output and refers primarily to interact with the system’s disk and network supported by libuv engine. Once the application is started it is loaded into the system’s memory. That’s what the CPU will mostly use for running your application.
I/O accessing memory is pretty fast, hence a lot of caching mechanism is implemented into simply storing data in RAM. However, the application will often have to load data from files and over the network and these I/O are by far the slowest types. This is where non-blocking came into place.
Most of the synchronous based services will spawn additional threads to execute pending requests or the developers will write their implementation to handle requests asynchronously.
But with Node.js we don’t have to worry about it since it runs asynchronously by default.
Asynchronous non-blocking servers like Node executes all requests in a single thread, this means a single instance of Node makes most out of a single thread also this means the server can serve more requests with limited hardware resource which will be cost-effective.
When a request arrives at the server-side they’re processed one at a time however if any blocking I/O operation comes in between like query data from the database then it sends the callback to the second queue and it doesn’t wait and continue executing remaining requests. Now when the DB operation completes the corresponding callback is pushed to the third queue where they are pending execution. When the main thread gets time to execute something this callback will be processed.