What is Node JS and its architecture?
- June 25, 2019
- Posted by: admin
- Category: Web Development
Why NodeJS matters ?
It raises a question and eyebrows of everyone around the corners of Software Industry lately. It’s the new cool lingo you want to know and why not, since NodeJS has gotten maximum number of downloads at Github and it has its own repository NPM.
So what is NodeJS?
As per the official description at Node JS website
But I don’t want another new technology since there are already enough — Java, DotNet, Python, PHP to name a few.
Yes we agree but every new technology bridges the gap which its peers couldn’t yet. NodeJS not only helped in optimization of resources but helped in executing operations asynchronously.
It challenges the potential of the machine and converts the Machine Power into Horse Power.
Sounds Exciting Right? Yes it is the more you get to know about it.
What are NodeJS Applications?
- Chat Servers
- Backend Servers
- Program Processing Softwares
- Networking Applications
Synchronous Requests vs Asynchronous Requests
A request is called to be synchronous if it doesn’t have to wait for resources or is dependent on another operation to finish its execution.
Examples of Synchronous Tasks:
- Addition of 2 number
- Display a result on screen
On the other hand a request is called to be asynchronous if it has a dependency on other operations, tasks or resources over which its execution is dependent on.
Examples of Asynchronous Tasks:
- Pulling data from Database
- To read a file from Server.
Node JS applications uses “Single Threaded Event Loop Model” architecture to handle multiple concurrent clients. I would like to explain the architecture of NodeJS with the help of the diagram inline below :
Basically, NodeJS architecture contains 3 layers:
- Request Queue
- Single Threaded Event Loop
- Callback Operations
Let’s me help you understand every layer one by one in the simplest way possible. For the same let’s consider a request statement as quoted below.
Rajeev’s mom asked him to wash clothes and recharge the set top box.
Here if we try to decode the above quoted statement then we can decipher 2 requests/tasks for Rajeev:
- Rajeev will put clothes in washing machine.
- Rajeev need to recharge the expired set top box TV connection.
Once NodeJS receives the quoted request, it will generate 2 requests and put them in a request queue internally and will wait for the Event Loop to process them.
Single Threaded Event Loop
From Request Queue, Event Loop will pick up the first request and put that request into operation. Event Loop will check whether this request is Synchronous or Asynchronous.
Request 1 has a dependency on washing machine which makes it an asynchronous request. So, till the time washing machine is not done washing the clothes, Rajeev has 2 choices to take a decision from :
- Either he has a choice to wait for 20 minutes and sit idle.
- Or he can in the meantime recharge the set top box.
If Rajeev, decides to use his time wisely then he will work on to execute the second task instead of sitting idle. So he will let the washing machine complete the task and would like to be notified about its completion in the form of Callback or here in the form of Alarm from Washing Machine.
Feeling connected with NodeJS now? That’s Great !!! Gladly you didn’t notice but we have reached to our final layer.
Are you now scared of Callbacks and Promises ? Okay let me try to explain them.
As per our above Request 1, our alarm served as a callback for Rajeev. Got it right ?
Promises; just like in real life is a token of appreciation which will be fulfilled once a particular objective is achieved.
A mother promises her child to buy her child a chocolate if she completes her homework.
Try to understand Request 1 and you’ll find that washing machine is internally programmed that it will ring the alarm once it complete the task of washing the clothes. So Washing machine promised Rajeev that it will callback once the task is completed.
Get yourself familiar with callbacks and promises. Understand them and use them. Connect NodeJS with real life practical examples and you will start loving it.
That’s all for Node JS and its Architecture and Node JS single threaded event loop.