1

I was studying how Node JS improves performance for multiple concurrent request! After reading couple of blogs I found out that:

  1. When any request come an event is triggered and corresponding callback function is placed in Event Queue.
  2. An event Loop(Main Thread) is responsible for handling all requests in Event Queue. Event Loop processes the request and send back the response if request uses Non-Blocking I/O.
  3. If request contains Blocking I/O Event Loop internally assigns this request to an idle worker from Work Pool and when worker send back the result Event Loop sends the response.

My Question is since Event Loop is passing heavy blocking work internally to Work Pool using libuv library, why we need Asynchronous callback?

For Better Understanding please see the below code:

const express = require('express')
const app = express()
const port = 3000

function readUserSync(miliseconds) {
    var currentTime = new Date().getTime();
 
    while (currentTime + miliseconds >= new Date().getTime()) {
    }

    return "User"
 }

 async function readUserAsync(miliseconds) {
    var currentTime = new Date().getTime();
 
    while (currentTime + miliseconds >= new Date().getTime()) {
    }

    return "User"
 }

app.get('/sync', (req, res) => {
    const user = readUserSync(80)
    res.send(user)
})

  app.get('/async', async (req, res) => {
    const user = await readUserAsync(80)
    res.send(user)
  })

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

I checked performance for both endpoints using apache benchmark tool, assuming each I/O operation takes 80ms.

ab -c 10 -t 5 "http://127.0.0.1:3000/async/"
ab -c 10 -t 5 "http://127.0.0.1:3000/sync/"

And surprisingly for endpoint with async callback had higher number of request per second.

So how Event Loop, Thread Pool and async await works internally to handle more concurrent requests?

Bishal Imtiaz
  • 479
  • 1
  • 5
  • 10
  • 1
    What's the exact difference? These should be quite close to each other, yes? – freakish Oct 06 '21 at 19:13
  • Blocking, time consuming code inside an `async` function does nobody any good. It's' still just blocking, time consuming code whether it's in an `async` function or in a regular function. In case you were under some mistaken idea that the Javascript in `async` functions runs in another thread, that is NOT the case. They run in the same thread so blocking Javascript code in an `async` still blocks the event loop. – jfriend00 Oct 06 '21 at 19:21
  • @jfriend00 Assuming ```readUserSync``` and ```readUserAsync``` performs IO operations(e.g., getting user from mysql db) then what would be the scenario for two endpoints? – Bishal Imtiaz Oct 06 '21 at 19:55
  • That's a completely different scenario. If you are doing truly non-blocking, asynchronous I/O operations, then those don't block the event loop like the examples you show here. The non-async version of your functions that does actual non-blocking, I/O would have to use a callback or event to communicate completion since they would be non-blocking and would return BEFORE the I/O operations were done. – jfriend00 Oct 06 '21 at 19:58

0 Answers0