DEV Community

Cover image for Basic Concepts of Node.js
Cássio Santiago Cappellari
Cássio Santiago Cappellari

Posted on • Updated on

Basic Concepts of Node.js

🔶 Introduction

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.

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 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 this is 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.

Node.js Advantages

🔸 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 shutted down.

In other words, it’s like having a really efficient waiter waiting for orders until the restaurant closes.

Node.js Event Driven

🔸 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 sended 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.

Node.js Architecture

🔶 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 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.

Fundamentals of REST API

Written with 💙 by Cássio Cappellari!

Top comments (58)

Collapse
 
ahmadawais profile image
Ahmad Awais ⚡️ • Edited

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.

Nodejs Event Loop

I hope it's helpful.

Collapse
 
cassiocappellari profile image
Cássio Santiago Cappellari

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!

Collapse
 
ahmadawais profile image
Ahmad Awais ⚡️ • Edited

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.

Collapse
 
olaviolacerda profile image
Olavio Lacerda

Would you have an example of a good diagram? Share with us, please!

Collapse
 
bam92 profile image
Abel Lifaefi Mbula

You read about event loop here.

Thread Thread
 
cassiocappellari profile image
Cássio Santiago Cappellari

Thanks a lot for sharing, for sure I'll read about it!

Thread Thread
 
yiorgosc profile image
yiorgosc

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

Collapse
 
ahmadawais profile image
Ahmad Awais ⚡️

Just edited my comment with the explanation/video — I hope it helps.

Collapse
 
ppicom profile image
Pere PicĂł

I found this talk from Bryan Hughes, Microsoft the best there is to understand the Node.js event loop

Collapse
 
cassiocappellari profile image
Cássio Santiago Cappellari

Thanks a lot for sharing this valuable content!

Collapse
 
iamjonjackson_37 profile image
Jon Jackson

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!

Collapse
 
cassiocappellari profile image
Cássio Santiago Cappellari

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!

Collapse
 
dhirajpatra profile image
Dhiraj Patra

Nice after long years read a simple but informative article about nodejs

Collapse
 
cassiocappellari profile image
Cássio Santiago Cappellari

Thanks a lot for your feedback, I'm glad to read that!

Collapse
 
eshimischi profile image
eshimischi

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

Collapse
 
cassiocappellari profile image
Cássio Santiago Cappellari

Thanks a lot for your contribution, I heard about Deno and for sure I'll pay attention to this new technology!

Collapse
 
eshimischi profile image
eshimischi

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..

Collapse
 
authorantheya profile image
Antheya

Growed, sended, shutted??? Please, next time you post, use some English grammar tool...

Collapse
 
jerevick83 profile image
jerevick83

The message was passed and understood at least.

Collapse
 
cassiocappellari profile image
Cássio Santiago Cappellari

Thanks for your suggestion!

Collapse
 
tanth1993 profile image
tanth1993

thanks man. now I know more about Node.js

Collapse
 
cassiocappellari profile image
Cássio Santiago Cappellari

You're welcome, I'm glad that this article helped you!

Collapse
 
afozbek profile image
Abdullah Furkan Ă–zbek

Wonderful article, thanks for sharing

Collapse
 
cassiocappellari profile image
Cássio Santiago Cappellari

You're welcome, thanks a lot for your feedback!

Collapse
 
aacraf profile image
acraf

Really helpful. Thank you!

Collapse
 
cassiocappellari profile image
Cássio Santiago Cappellari

Thanks a lot for your feedback, I'm glad that this article was helpful to you!

Collapse
 
iliyas503 profile image
iliyas503

Thanks for sharing awesome info Cassio Cappellari. Keep doing and will love to read this kind of articles.

Collapse
 
cassiocappellari profile image
Cássio Santiago Cappellari

Thanks a lot for your feedback and for your words, I'm really glad to read that!

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
cassiocappellari profile image
Cássio Santiago Cappellari

Thanks for your alert, but I believe that this Spam has already been deleted!