Why we need async Callback in Node JS Since Event Loop offers Worker Pool to handle expensive task?

378 views Asked by At

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?

0

There are 0 answers