Search code examples
javascriptnode.jsmultithreadingevent-loop

Why are multiple requests to a route executed sequentially in Node.js?


I have a route in my Node.js application that takes around 10 seconds to respond to each request.

router.get("/", async (req, res, next) => {
    console.log("Service health request.", req.ip, new Date());
    let response = { success: true, message: "Service is running!!" };
    await wait(10000);
    return res.json(response);
});

const wait = (milliseconds) => {
    return new Promise(resolve => setTimeout(resolve, milliseconds));
}

When I make multiple requests to this route concurrently, I notice that each request is executed sequentially, with the first request taking 10 seconds to complete, the second request taking 20 seconds, the third request taking 30 seconds, and so on.

What's interesting is that when I request another route in my application at the same time, it is processed without delay, which leads me to believe that the issue is specific to the route in question.

router.get("/status", async (req, res, next) => {
    console.log("Any other route request.");
    let response = { success: true, data: {} };
    await wait(10000);
    return res.json(response);
});

I am wondering why my requests to this particular route are being processed sequentially instead of concurrently. While I am aware that callbacks and async programming can help me achieve concurrent processing. I have read about Node.js's event loop and its single-threaded nature, but I am not sure how it relates to my issue.

Can anyone explain why my requests to this particular route are being processed sequentially? In addition to my primary concern, I am curious whether Node.js creates a separate event loop or call stack for each route in the application?


Solution

  • I assume your tests are being made in a browser. If so, the behaviour has nothing to do with Node.js.

    The browser will recognise that the requests are to the same URL and send them one at a time since the server might respond with cache control headers which would allow it to reuse the response for the first request as the response for the subsequent ones.