Imagine a web application which sends hundreds of requests per second to a server that only processes one request per time before moving to another request. Probably you will have some performance problems and provide a bad experience for your users.
With this context in mind, Node.js was created in 2009 by Ryan Dahl, a web developer, after he faced some difficult issues by developing features with intense input and output flow using synchronous programming and multi-threaded server-side platforms.
So, in this article, I share my knowledge, experience and studies about this technology, with the goal to provide the basic concepts of Node.js, and help you to understand why it’s such an important player in a modern server-side world.
Node.js is an extremely powerful server-side platform to develop modern, reliable and scalable web applications, trusted by global companies such as Netflix, Uber, LinkedIn and PayPal.
Due to its single thread characteristics combined with a multi thread platform that runs in background, it’s event driven architecture is perfect for applications with intense I/O (Inputs and Outputs), making it serve more clients with less hardware, which means more performance with less costs.
Besides that, this platform provides other amazing features for developers.
Non-blocking method receives an input and returns the output asynchronously. In other words, it makes it possible for a server to receive many requests without blocking the application while the response is being processed in the background.
Node.js can deal with many events at the same time with its single thread characteristic that delegates the asynchronous operations to a multi thread platform, which means that just one thread is able to handle inputs and outputs.
This feature makes it lightweight, scalable and efficient by consuming low memory and infrastructure capacity in order to process a huge number of requests.
Node.js is an event driven technology, which means that the control flow of this server side platform is driven by the occurrence of events. So, at the moment that a Node application starts, an event listener called Event Loop begins to wait for events and doesn't stop until the application is shutted down.
In other words, it’s like having a really efficient waiter waiting for orders until the restaurant closes.
Node Package Manager it’s the world's largest free and open source library of functionalities, and can be easily imported and used in any Node application. All this means that, regardless of the tools that an application needs, it will be found at Node Package Manager.
Node.js applications never buffer data, which dramatically reduces the processing time of uploading files, such as videos or audios. In other words, it simply outputs data in chunks, meaning that, for example, a user can watch videos without any interruption.
Scalability is a core Node characteristic, due to a high capacity of handling large amounts of requests asynchronously with low infrastructure by it’s architecture that operates on single thread combined with multi thread platform, allowing it receives thousands of simultaneous events.
Node.js architecture has 3 key elements that work together like an event processing factory. So, in order to make it easier to understand how these elements interact with each other, I will give an example of a REST API developed in Node.js.
Let’s say that a Client sends several requests to this application:
1- As soon as these requests reach the application, they go to the Event Queue, which is a queue where all the events that occur in the application goes first, and where they await to be sended to be processed in the main thread called Event Loop.
3- In the Thread Pool, which is a multi thread platform that runs a library called libuv and has C++ in its core, the request (Blocking Operation) is processed asynchronously in the background until it’s completed and ready to be returned.
I hope that this brief article helped you to have a better understanding of not just how Node.js works, but also why it’s a platform that growed quickly and it's used in so many companies and startups nowadays.
In a modern and ultra-connected world, a technology that is able to scale fast with low infrastructure for sure it’s an important player to pay attention to.
So, if you arrived here, I invite you to read my another article about the fundamentals of tools that are also essential in the web world that we live in: API, REST and HTTP Protocol.
Written with 💙 by Cássio Cappellari!