🔶 Introduction
Imagine a web application that 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 issues and will provide a bad experience to 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.
Therefore, Node.js was a technology built to solve a specific problem: deal with intense asynchronous input and output events. And, which programming language has this capacity? The answer was: JavaScript.
So, in this article, I share my knowledge, experience and studies about this technology, with the main 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.
🔶 1 What is Node.js?
In simple terms, Node.js is an open-source JavaScript based platform for server-side programming built on Chrome's V8 JavaScript engine, which means that the engine that compiles JavaScript in a web Browser it's the same engine that runs Node.js in it’s core, and that's what makes it so fast, especially for web applications.
🔶 2 Why Node.js?
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.
🔸 2.1 Non-Blocking I/O
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.
🔸 2.2 Single Thread
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.
🔸 2.3 Event Driven
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 shutting down.
In other words, it’s like having a really efficient waiter waiting for orders until the restaurant closes.
🔸 2.4 Node Package Manager
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.
🔸 2.5 No Buffering
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.
🔸 2.6 Scalable
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.
🔶 3 Node.js Architecture
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:
🔸 3.1 Event Queue
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 sent to be processed in the main thread called Event Loop.
🔸 3.2 Event Loop
2- When a request (Blocking Operation) enters in the Event Loop, which is a single thread platform that runs the V8 Engine in its core to compile JavaScript, it’s delegated to the Thread Pool platform to be processed in background. So, with this architectural flow, the main thread is available again to handle other events.
🔸 3.3 Thread Pool
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.
🔶 Conclusion
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 has grown 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!
Top comments (58)
Unfortunately that's a wrong diagram for the event loop. Node.js event loop is not like the JavaScript loop at all. 99% of diagrams like these are just simply wrong.
I taught this as part of my premium Node CLI Development course, but just shared that 10min video on Node.js theory, publicly for free on my YouTube Channel here.
I hope it's helpful.
Thanks a lot for your feedback and, as a developer, I'm always looking forward to learning and improving my knowledge. I'll really appreciate suggestions about correct examples of Node.js diagrams in order to update my article, so feel free to share them!
I taught this as part of my premium Node CLI Development course, but just shared that 10min video on Node.js theory, publicly for free on my YouTube Channel here.
I hope it's helpful.
Would you have an example of a good diagram? Share with us, please!
You read about event loop here.
Thanks a lot for sharing, for sure I'll read about it!
I think both explanations are part of the bigger picture of what actually the event loop does. async tasks are assigned to the thread pool and when finished their call back functions are put in the event queue. when nodejs's execution stack is empty, the event pool takes the first cb from the event queue and puts it back in the stack for execution
Just edited my comment with the explanation/video — I hope it helps.
I found this talk from Bryan Hughes, Microsoft the best there is to understand the Node.js event loop
Thanks a lot for sharing this valuable content!
Hey Cássio, thanks for sharing this. I couldn't find anything online about Ryan Dahl originally being a PHP developer. What was your source for this? I'm intrigued!
You're welcome, thanks for reading it! Actually, in the process of writing this article I watched many Ryahn Dahl interviews and now I couldn't find the one that confirmed that he was a PHP developer. Sorry about that! What I can confirm is that he was a web developer in the early 2000s, years before Node.js development.
So, because I couldn't find the right source, I'll update my article here!
Nice after long years read a simple but informative article about nodejs
Thanks a lot for your feedback, I'm glad to read that!
And now there is Deno.. maybe it is still not so popular, however all issues of Node.js solved more or less with this new tech, created by the same guy who once created Node.js
Thanks a lot for your contribution, I heard about Deno and for sure I'll pay attention to this new technology!
Welcome! It has a lot of pros.. Typescript based and no more node_modules.. checked a lot of boilerplates and projects, for me it is a step forward even if there are those who will argue with me..
Growed, sended, shutted??? Please, next time you post, use some English grammar tool...
The message was passed and understood at least.
Thanks for your suggestion!
thanks man. now I know more about Node.js
You're welcome, I'm glad that this article helped you!
Wonderful article, thanks for sharing
You're welcome, thanks a lot for your feedback!
Really helpful. Thank you!
Thanks a lot for your feedback, I'm glad that this article was helpful to you!
Thanks for sharing awesome info Cassio Cappellari. Keep doing and will love to read this kind of articles.
Thanks a lot for your feedback and for your words, I'm really glad to read that!
Thanks for your alert, but I believe that this Spam has already been deleted!