So as we know node js application are highly-scalable and
this is because of the non-blocking or async nature of node js.
What do I mean by async nature of node js??
Asynchronous programming is a design pattern which ensures
the non-blocking code execution
Non blocking code do not prevent the execution of piece of
code. In general if we execute in Synchronous manner i.e one after another we
unnecessarily stop the execution of those code which is not depended on the one
you are executing this is how application build with framework like ASP.net and
rails works out of the box.
Asynchronous does exactly opposite, asynchronous code
executes without having any dependency and no order. This improves the system
efficiency and throughput.
So when we receive a request on the server a thread is allocated to handle that request. And suppose in that request we have to fetch data form database as we all know sometime querying a DB takes a time.
When the database is executing the query that thread will be wait for the result and imaging what would happen if we have a large number of concurrent client at some point we are going to run out of threads for new client have to wait until threads are free. If we don’t want then to wait then we have to add more threads to server which more hardware and cost.
So this is the problem with blocking or sync architecture and as I explained that how’s application build with framework like asp.net work by default. We can also have async architecture in asp.net but we have to extra work for that.
But Node js application are async by default so we don’t have to do anything extra. In node we have a single thread to handle all request when request arrives that single thread is used to handel that request if we need to query a DB our thread doesn’t have to wait for the db to return the data. While the db is executing our query the thread used to serve another request.
Once DB ready with result it puts a message in what we call an event
queue. Node is consistently monitoring this event queue in the background. When
it find an event in this queue it will take it out and process it. This kind of
architecture makes node js ideal for building applications that include a lot
of disk or network access. We can serve more client without the need to throw
in more hardware. And that why node application are highly scalable. Node should not be used in CPU-intensive apps such as a video encoding service. Because while executing these
operations, other clients have to wait for the single
thread to finish its job and be ready to serve them. Node should only be
used for data intensive and real time application
0 comments:
Post a Comment