1

I have a Node.js app and noticed that when I madke 2 requests to my Node Api at the same time, they appear to be queued. I.e the time for the second request is the time for the first request plus the time for the second request (if I just fired that request on its own).

So I did a simple test:

app.get('/api/testpromise', function(req, res) {

    sess = req.session;

    var controller = new Controller(req, res, sess);

    controller.testPromise()
    .then(function(){
        controller.testPromise()
        .then(function(){
             res.status(200);
             res.json({});
        });
    })
})

The testPromise() method just does a loop:

Controller.prototype.testPromise = function(callback){

    return new Promise(function(resolve, reject) {

        var count = 0;
        for (i = 0; i < 500000000; i++) {
            count++;
        }

      if(count) {
        resolve(count);
      } else {
        reject(count);
      }
    });

}

I fire off 2 requests to 'testpromise' at the same time from my frontend and the first takes approximately 5 seconds to complete and the approximately 10 seconds. So the second one appears to be queued behind the first.

I thought from what I had read about Node I wouldn't see behaviour like this? Node is said to be really good at taking lots of concurrent requests and handing them off. So how does this actually work?

1
  • 4
    Well, it boils down to: JS is single threaded, and although you use Promises, your loop blocks the execution of other code in this thread. Take a look at this: How to create threads in nodejs Commented Mar 23, 2017 at 9:16

1 Answer 1

2

Your test is using blocking, synchronous code. That for-loop for (i = 0; i < 500000000; i++) will run until it is complete, so the first request in will not release control of the event loop until it is complete. It is like a "busy wait"

A better test for you would be to just delay

return new Promise(function(resolve, reject) {
    setTimeout( function () {
      resolve(5000) 
    }, 5000 ) // run the given function in 5000ms
});

In this way, the event loop will start the second request while the first is waiting. So if you request them at the same time, both will finish at the same time.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. In reality my API endpoints make queries to MongoDb and they are queueing up...
You could post actual code for more help, but a call to a Mongo DB should function like a setTimeout() to Node, in that it will start processing the next request while Mongo is getting that data.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.