Skip to main content

Node Architecture

Node.js is based upon an event-driven, light-weight, and non-blocking I/O model. This is a lightweight, minimal framework based on the Google Chrome V8 engine. The Event-loops are the soul of event-driven programming, almost all the programs using UI use event-loops to detect and react to the user events.
Like Clicking a button, Ajax Requests, etc.

Node.js is a command-line tool(See getting started with Node). It is a high-performance network application, JavaScript minimal framework well suitable for the concurrent environments. Node is not just JavaScript, its major part is written in C/C++.

Node.js Architecture


Node uses event-loops by JavaScript callbacks to implement the non-blocking I/O. (We must always keep in mind that JavaScript used in Node is not exactly same as we know, it is different, in fact, it is no DOM implementation used by Node, so be careful).

Everything inside the Node runs upon a single thread(main thread), and this thread should never be blocked. It means if we need to wait for something to happen, it must provide a callback. So CPU-intensive operation must be kept off from the event-loops.


Event-loops
Event-loops 

Conventional IO Model



var myresults=db.query("Execute some query in database");   
      executeWithResults(myresults);
      //wait forre results to load             
      executeWithOutResult();
      //execution will be blocked untill the
      //executeWith(myresults) finishies              


Non-blocking IO Model



db.query("Execute some query in database",function(myresults){           
      executeWithResults(myresults);//wait for result     
      });  
 executeWithOutResults(); //executes without any delay!                  


Blocking Vs Non-blocking IO


The conventional model is an asynchronous model. It waits for input before proceeding. The thread is blocked.

Non-blocking IO is asynchronous, the main thread is not blocked and keeps executing without blocking. The Node.js implements a non-blocking IO with event-based JavaScript callbacks.

The biggest advantage of non-blocking IO is no-overhead is associated with the threads, so we can handle lots of client requests at the same time. This is best for achieving the high-concurrency.




Threads consume resources like memory on stack and some processing time is consumed in order to provide context switching among the threads. 

On the other hand, no thread management is required in non-blocking IO. Only JavaScript callbacks are executed when some event occurs. 

These callbacks are usually in the form of anonamous functions.

Comments

Popular posts from this blog

How to use Ngx-Charts in Angular ?

Charts helps us to visualize large amount of data in an easy to understand and interactive way. This helps businesses to grow more by taking important decisions from the data. For example, e-commerce can have charts or reports for product sales, with various categories like product type, year, etc. In angular, we have various charting libraries to create charts.  Ngx-charts  is one of them. Check out the list of  best angular chart libraries .  In this article, we will see data visualization with ngx-charts and how to use ngx-charts in angular application ? We will see, How to install ngx-charts in angular ? Create a vertical bar chart Create a pie chart, advanced pie chart and pie chart grid Introduction ngx-charts  is an open-source and declarative charting framework for angular2+. It is maintained by  Swimlane . It is using Angular to render and animate the SVG elements with all of its binding and speed goodness and uses d3 for the excellent math functio...

JavaScript new features in ES2019(ES10)

The 2019 edition of the ECMAScript specification has many new features. Among them, I will summarize the ones that seem most useful to me. First, you can run these examples in  node.js ≥12 . To Install Node.js 12 on Ubuntu-Debian-Mint you can do the following: $sudo apt update $sudo apt -y upgrade $sudo apt update $sudo apt -y install curl dirmngr apt-transport-https lsb-release ca-certificates $curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash - $sudo apt -y install nodejs Or, in  Chrome Version ≥72,  you can try those features in the developer console(Alt +j). Array.prototype.flat && Array.prototype. flatMap The  flat()  method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. let array1 = ['a','b', [1, 2, 3]]; let array2 = array1.flat(); //['a', 'b', 1, 2, 3] We should also note that the method excludes gaps or empty elements in the array: let array1 ...

Understand Angular’s forRoot and forChild

  forRoot   /   forChild   is a pattern for singleton services that most of us know from routing. Routing is actually the main use case for it and as it is not commonly used outside of it, I wouldn’t be surprised if most Angular developers haven’t given it a second thought. However, as the official Angular documentation puts it: “Understanding how  forRoot()  works to make sure a service is a singleton will inform your development at a deeper level.” So let’s go. Providers & Injectors Angular comes with a dependency injection (DI) mechanism. When a component depends on a service, you don’t manually create an instance of the service. You  inject  the service and the dependency injection system takes care of providing an instance. import { Component, OnInit } from '@angular/core'; import { TestService } from 'src/app/services/test.service'; @Component({ selector: 'app-test', templateUrl: './test.component.html', styleUrls: ['./test.compon...