3

I just created a NodeJS Express server to understand the working of NodeJS. I have learned that NodeJS can handle a large number of API requests simultaneously given that CPU intensive tasks are not done by the same thread.

However, my server is not even able to handle 2 requests simultaneously, that too without any CPU intensive tasks. I am pretty sure that I am missing something here. Here is my code:

const http = require('http');
const express = require('express');
const app = express();

const waitForSomeTime = () => new Promise((resolve) => {
  setTimeout(() => resolve(), 5000);
});

app.use(async (req, res) => {
  console.log('Request received');
  await waitForSomeTime();
  console.log('Response sending')
  return res.send('DONE');
})

app.set('port', 3000);

const server = http.createServer(app);

server.listen(3000, () => { });

Currently, when I hit the API with 2 requests parallelly, the server resolves the first one after 5 seconds and then accepts the 2nd request. The total time taken is 10 seconds. I tried this application in my local Mac system and in a Linux VM, which had 4 cores.

Ideally, shouldn't NodeJS accept the 2nd request as soon as it initiates the timer of the 1st request. Thereby resolving both the requests in 5 seconds

Is there something I need to configure in the machine settings? or anywhere?

  • Can you try with cluster mode https://stackoverflow.com/questions/29163839/how-to-start-node-js-app-with-pm2-in-cluster-mode And article https://pm2.keymetrics.io/docs/usage/cluster-mode/ – prasanth Sep 01 '20 at 05:29
  • 2
    Are you sure it is node that is not handling parallel requests or your testing code waiting for the response before sending a second request? – slebetman Sep 01 '20 at 05:30
  • @prasanth I have not tried with pm2 cluster mode. I was trying to understand the working of NodeJS – Zeeshan Shamsuddeen Sep 01 '20 at 05:31
  • @ZeeshanShamsuddeen pm2 also one of the node package.. Mostly referred tool for nodejs production deployment is pm2. it default have cluster mode. [node js single threaded one](https://medium.com/better-programming/is-node-js-really-single-threaded-7ea59bcc8d64). so its not handling parallel request – prasanth Sep 01 '20 at 05:33
  • @slebetman Currently I tried using Google Chrome browser, let me try using Postman – Zeeshan Shamsuddeen Sep 01 '20 at 05:34
  • @slebetman Thank you. You were right. Chrome was waiting for the first request before sending in the second request. Thank you. https://developers.google.com/web/tools/chrome-devtools/network/understanding-resource-timing – Zeeshan Shamsuddeen Sep 01 '20 at 05:38
  • Here's a youtube video of your code demonstrating parallel requests and parallel response: https://youtu.be/xib_YwfScsk – slebetman Sep 01 '20 at 05:38
  • Somebody should write an answer since the problem/solution is now known. – jfriend00 Sep 01 '20 at 06:20
  • See also [NodeJS Express Async Not Handling More Requests](https://stackoverflow.com/questions/67475279/nodejs-express-async-not-handling-more-requests?noredirect=1) which is more specific/transparent about the method used to test, making multiple requests from different tabs in the same browser. – ggorlen May 12 '21 at 14:00

1 Answers1

10

The issue was because I had used the browser to stimulate simulatenous hits to the server. Google chrome stalls subsequent requests if they are done to the same API which was the reason I thought my server had issues.