Skip to main content

Command Palette

Search for a command to run...

Understanding the NodeJS and Its architecture

Knowing the terms like non-blocking, event loop, event driven, concurrency model

Published
4 min read
Understanding the NodeJS and Its architecture

NodeJS is the run time environment to execute javascript outside of the browser. In this article , we discussed about the introduction of nodejs, meaning of non-blocking event driven model, and event loop.

Introduction of NodeJS

Nodejs is created on 2009 by using the chrome Javascript engine which is V8. The main motive of bringing nodejs is that obviously to run javascript outside of browser. It enables the door for building server side application. It is used to build highly scalable, efficient networking applications by using its powerful non-blocking event driven model.

Dive into some reasons of arrival of nodejs

  • Server-Side JavaScript: Initially JavaScript is used in client side on the browser. It is just used for making website interactive, so nobody take it seriously but when nodejs arrives it is fully capable to build highly performant server side application , for making scalable full stack data intrensic application.

  • Non-blocking I/O model : In past time, the server was multi-threaded means the each operation takes dedicated thread to execute and finished which is inefficent for high concurrent operations so nodejs is single threaded, non-blocking event driven I/O model which is more suitable to handle multiple thousands of concurrent tasks easily like reading file system , database query etc.

  • unified language: it is simple both client and server side can be now built by using single language JavaScript.

  • performance: nodejs is buit bu using high perfomant chrome v8 engine which compiles the JavaScript code into machine code and fast executes it.

  • Rich eco system : nodejs comes with npm package manager which enables access of world largest ecosystem of libraries , frameworks and tools which are used to build application really fast in sort period of time.

  • Real Time Applications: It is non-blocking event driven I/O model so It is more suitable to build I/O bound applications and data-intrensic applications like web-server, chat application, collaborative tool, online games etc.

what is meant by non-blocking and event driven model

Non-blocking event driven I/O model means it initiate input/output operation, without waiting them to finish. It delegates or offloads the tasks to the underlying sytem (kernel) or threadpool by libuv. so when the operation finish, It executed the associated callback which makes its more fast, efficient, responsiveness making ideal for high concurrency environments like web-servers.

Non-blocking I/O means when your code request data like reading files or some network request. It does not wait for the result. The system return the control immediately allowing to execute the other code.

Event-Driven Architecture: The program operates by listening the events when I/O tasks finished, the underlying system kernel signal that event occurred .

Event Loop : It is main core coordinator. It keeps monitoring the new tasks or finished I/O tasks. when a task completed, the loop picks up the associated callback and execute to it.

Why this model ?

The reasons behind it are:

  • Scalability : It can handle high concurrency connections without blocking the main thread, you can handle significantly more traffic than thread per request model.

  • Efficiency: It eliminated the idle time . The CPU doesnot have to wait for the slow response from hard dirve or network request.

  • Suitability: It is highly suited to build I/O bound applications like web severs, chat applications, streaming applications etc.

What is meant by I/O tasks

It is simple the program interaction with external environment like reading and writing file system, database query, network request or logging some files data.

Summary

Nodejs is JavaScript run time environment which is based on non-blocking event driven I/O model that means It doesnot wait for tasks to finished, rather it offloads the I/O task to underlying sytem kernel or threadpool by libuv.

It is most suitable for handling high concurrency tasks meaning different tasks at same time which increase the efficiency , responsiveness , resources utilization especially on I/O bound operations .