Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Requests to Node.js server timing out due to multiple requests

So I'm not super experienced with node so bear with me.

I have 2 routes on a node.js server. During a typical day, both routes will be hit with requests at the same time. Route 1 will run smoothly but route 2 is a long running processes that returns several promises, so route 1 will take up the resource causing route 2 to pause (I have determined this is happening via data logs).

Route 1 looks like this:

  app.post('/route1', function (req, res) {
    doStuff().then(function(data){
      res.end();
    })
  }

Route 2 is handling an array of data that needs to be parsed through so 1 record in the array is processed at a time

app.post('/route2', function (req, res){
 async function processArray(array) {
      for (const item of array) { 
         await file.test1()(item, res);
           await file.test2()(item, res);
             //await test3,test4,test5,test6 
      }
  }
  processArray(data).then(function() {
    res.end();
  }
}

So I'm guessing the problem is that the async/await is waiting for resources to become available before it continues to process records.
Is there a way for me to write this to where route1 will not interfere with route2?

like image 250
MMeadows Avatar asked Mar 28 '26 06:03

MMeadows


1 Answers

In Node, almost everything you can await for (or call then on) is asynchronous. It does not block execution thread but rather offloads the task to another layer you don't control, and then just awaits for it to be finished while being free to work on something else. That includes working with filesystem and network requests. There are ways to block the thread still, for example, using synchronous versions of filesystem methods (like readFileSync instead of readFile) or doing heavy computations on javascript (like calculating factorial of 4569485960485096)

Given your route1 doesn't do any of this, it does not take any resources from route2. They are running in parallel. It's hard to tell without seeing the actual code, but I'm pretty sure you are getting connection timeout because your route2 is poorly written and it takes a long time to resolve (or doesn't resolve at all) for reasons not related to Node performance or blocking. Node is just chilling while waiting for your filesystem to run those endless tests 6 times in every array item (or whatever is going on there) and while this happens, browser stops waiting for response and shows you connection timeout. It's most likely that you don't need to await for every test on every array in the data instead of just running them all in parallel

Read more here https://nodejs.org/en/docs/guides/blocking-vs-non-blocking/ and here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all

like image 52
Max Avatar answered Mar 29 '26 20:03

Max