NodeJs and Architecture

Photo by Dim Hou on Unsplash

NodeJs and Architecture

Let's see how the NodeJs work. The flow of NodeJs starts with the client😎(who is a client I/me/you is the client).

Here the client is req to the server🌎 and this server is running on nodeJs. Then the interesting game starts from here- When this req comes into the server analyse this diagram:🤖

This green box is a NodeJs and the req comes into it and the Event Queue handles all the coming req from the client after that all req goes to the Event loop(Mechanism that always watches on the Event queue)worked on FIFO. It picks up the req from the Event queue and finishes it and the pick-up req coming from Event Q. It can be two types:

  1. Blocking operation / synchronous task

  2. Non-blocking operation/ asynchronous task

Prefer Official documentation for more:✨

NodeJs docs

If it is a non-blocking operation event loop processes it and passes it client. In case of blocking operation then what next🤷‍♀️

To resolve the blocking operation the operation goes to the thread pool. In the thread pool threads are responsible for resolving the operation and it checks if there is any thread available to resolve because we have limited threads in the pool. ok assume the thread is available then resolve the req and send a response to the client.

More on thread pool:

Let's understand the thread pool more:

Your code is blocking code and in the thread pool, there are limited threads in the pool to resolve the req by default 4. If clinet1 comes with a blocking code thread is working on it client2,3,4 all threads are busy now and client5 comes in case of this if any one of the threads gets free first it resolves client5's blocking code.

We have an operating system that has a scheduler look into the diagram

Ex: You are working on some projects in Nodejs there are some tasks you want to get done tasks x1,x2,x3,x3......so then how this will work x1 is a node asynchronous task picked by the scheduler and processed by the 4 core system or thread here, Nodejs is runtime env which is top of v8 engine inside the v8 engine a component named libuv that is responsible for all asynchronous task in nodejs

Diagram overview:

Let's take another example for more clarity: We are working on the "/login" route email and password for the password we are using the library called becrypt.js hashing function it takes the value and gives out the random value hashed. When you install this library it uses C++ binary inside it and this binary itself is asynchronous and uses the libuv to perform or speed up the computation.

We have "/login" route so many users come and go at the same time so libuv has a thread pool inside it...🦘

Big picture

this bcrypt req comes and sets into the thread pool all these go to the 4-core system which executes all req parallelly all rest operation or req goes into the thread pool.

Want to see the big picture: Here it is😁

Thanks to :

freecodecamp, Piyush Garg, codedamn.