Concurrency in NodeJS

Concurrency in NodeJS

I will try my best to demystify Concurrency, that confuses most of developers to my best.

Concurrency is splitting the work across different time slots to get multiple tasks done efficiently in a given time. Wait! what do we mean by that! Lets understand with a simple example. Let's assume we have a scenario where we wanna cook food and do laundering as illustrated in Fig1.

Fig1: Concurrency

As we can notice from above diagram that we have two tasks Laundry and Cooking. Let us assume these two tasks are performed by a single person. One approach would be first finish the Laundry and then start Cooking or Put the cloths to laundry and while the cloths are being washed start cooking the food. Above diagram illustrates the later approach where it looks like performing the tasks parallel but it is not so. We are dividing the task across multiple time slots and hence looks asynchronous. We are not waiting on one task that blocks the other. We put the cloths to laundry and start cooking, this way we can use the time effectively instead of waiting on cloths to be washed.

Now lets look apply the same concept to understand concurrency in NodeJs. Javascript is single threaded which could be assumed as a single person performing one task at any given point of time.

Fig2: Concurrency in NodeJS


Req1 and Req2 in the above diagram indicates user Request1 and user Request2. t1, t2, t3, t4, t5 are different instances of time frame.

Diagram above illustrates, how a JS code listening to a user requests handles multiple requests concurrently. Let us assume we have a user1 requesting for a resource from a nodeJS http server. To complete the request let us assume we have to get some data from database, which is an I/O operation. As this is an asynchronous operation it gets offloaded to nodeJS and event loop keeps track of the offloaded task(In Fig2: at t2, arrow pointing to web API showing the async tasks getting offloaded by NodeJS ). At t2, user2 has made a request(denoted by Req2 in Fig2). JS Engine starts to process the request(Req2 ) until it encounters the async operation at t3. It then offloads the work to nodeJS, where it might need to perform I/O operation for resource fetching. At t3, let us say the async operation of user Requiest1(Req1) that was offloaded at t2 was finished by the web API and the call back is pushed on to the task Queue. Once the call stack is empty lets say at t3, event loop pushes the call back holding the result of the operation that was offloaded at t2 and completes the req1 at t4. At t4 let us assume that call stack is empty and the I/O operation related to user request2(Req2 )that was offloaded at t2 is finished and the result call back is pushed on to task Queue. As the call stack is assumed to be empty at t4, the async operation result call back(related to req2) would be pushed on to call stack to complete the user request2(Req2). This way even though NodeJs has a JS Engine which has a call stack which runs on single thread achieves concurrency.

Conclusion:

Even though JS Engine is Single threaded, NodeJS components such as Web API, libuv, Event Loop help offload asynchronous tasks and make concurrency possible.

要查看或添加评论,请登录

Santosh Kesireddy的更多文章

  • Core Components of API Response:

    Core Components of API Response:

    There are potentially four components to the HTTP Response. Status Code HTTP version Response header Response body…

  • Javascript Callback Hell

    Javascript Callback Hell

    What the hell is call back hell!!!!!! Lets check it out! It may be overwhelming for some developers to get there heads…

  • Reverse Proxy:

    Reverse Proxy:

    Have you ever wondered when developers use this buzz word, "Reverse Proxy"? Lets check it out what it is and why we…

  • Cluster Module:

    Cluster Module:

    It gives the ability for a nodejs application to handle large number of client requests by spawning multiple worker…

社区洞察

其他会员也浏览了