-1

I have a very simple express app which stores information it receives from clients into a database without hardly any processing. Some simplified code to illustrate this:

app.post(ANALYTICS_ENDPOINT, async (req, res) => {
  try {
    await insertToDB(req.body)
    res.status(204).send();
  } catch (err) {
    logger.error(err);
    res.status(500).send();
  }
});

This application is deployed to google cloud run and handles a high volume of requests per second. What we have noticed is that removing the await keyword from the route allows each container to handle more requests, at the downside of not capturing errors - so clients will always receive a 204.

The other use of await in this would be in insertToDB function which again simplified looks like this:

const insertToDB = async (row) => {
  await dbClient.insert(row);
}

As you can see here we are also using await since this action returns a Promise.

So the question is, should we be awaiting when the caller does not expect a response? And can it be the case that avoiding these awaits we can handle more throughput on our app?

EDIT: There is a similar question here. Yet I think this is different as I am mostly interested in understanding why removing the await allows me to handle 2/3x the number of requests per second.

dendog
  • 2,976
  • 5
  • 26
  • 63
  • Does this answer your question? [Does an async function always need to be called with await?](https://stackoverflow.com/questions/59953976/does-an-async-function-always-need-to-be-called-with-await) – tevemadar May 23 '21 at 08:28
  • 1
    A throughput depends on a bottleneck. If it's db, it stays the same, but you have less control over it. It's your decision to respond with success regardless of the result or wait for a promise. If it's not waited, it's obviously harder to detect the problem because it looks ok on client side. In this case the obvious problem is that an error won't be handled. If async job is separated from a request, at least make sure errors are handled and logged – Estus Flask May 23 '21 at 08:31
  • It does not appear the OP has written a clear question that describes what they're really attempting to ask. If you attempt to answer this part of their question ***So the question is, should we be awaiting when the caller does not expect a response?***, you will get downvoted because they don't actually want an answer that discusses that at all. – jfriend00 May 23 '21 at 10:31
  • @jfriend00 what is the point of what you are doing here? I do want answer, just not a paragraph on generic coding best practices which can be summarised as "you should handle errors". I do however thank you for your reply. – dendog May 23 '21 at 10:34

1 Answers1

-1

You should wait because your db call is async, so you must wait the insertion to be made before return to the client.

You can simply use promise resolution to be more concise and clear:

app.post(ANALYTICS_ENDPOINT, async (req, res) => {
    insertToDB(req.body)
    .then(() => res.status(204).send())
    .catch(() => res.status(500).send());
});
Andrea Pollini
  • 292
  • 4
  • 8