1

Console log Image

const express = require('express');
const app = express();
const port = 4444;

app.get('/', async (req, res) => {
  console.log('got request');

  await new Promise(resolve => setTimeout(resolve, 10000));

  console.log('done');
  res.send('Hello World!');
});

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

If I hit get request http://localhost:4444 three times concurrently then it is returning logs as below

got request
done
got request
done
got request
done

Shouldn't it return the output in the below way because of nodes event loop and callback queues which are external to the process thread? (Maybe I am wrong, but need some understanding on Nodes internals) and external apis in node please find the attached image Javascript Run time environment

got request
got request
got request
done
done
done
Phani Vvs
  • 61
  • 7
  • Yes it should and It does exactly the way you mentioned, check this in browser, https://ibb.co/g4P40Sc Not only that, it is doing exactly same thing in regular express js app as well! https://ibb.co/YpxNGTj – programcoder19 Apr 06 '21 at 17:46
  • @programcoder19 Weird but when I run the same thing, I am getting this as the output [link]https://ibb.co/mDMR0qf – Phani Vvs Apr 06 '21 at 17:59
  • Is it by any chance the node version? I am using version 12.3.1 – Phani Vvs Apr 06 '21 at 18:12
  • Nope tested with latest node version as well, not returning the expected output – Phani Vvs Apr 06 '21 at 18:24

2 Answers2

5

Thanks to https://stackoverflow.com/users/5330340/phani-kumar

I got the reason why it is blocking. I was testing this in chrome. I am making get requests from chrome browser and when I tried the same in firefox it is working as expected.

Reason is because of this

Chrome locks the cache and waits to see the result of one request before requesting the same resource again.

Chrome stalls when making multiple requests to same resource?

Phani Vvs
  • 61
  • 7
1

It is returning the response like this: enter image description here

Node.js is event driven language. To understand the concurrency, you should look a How node is executing this code. Node is a single thread language(but internally it uses multi-thread) which accepts the request as they come. In this case, Node accepts the request and assign a callback for the promise, however, in the meantime while it is waiting for the eventloop to execute the callback, it will accept as many request as it can handle(ex memory, cpu etc.). As there is setTimeout queue in the eventloop all these callback will be register there and once the timer is completed the eventloop will exhaust its queue.

Single Threaded Event Loop Model Processing Steps:

  • Client Send request to the Node.js Server.

  • Node.js internally maintains a limited(configurable) Thread pool to provide services to the Client Requests.

  • Node.js receives those requests and places them into a Queue that is known as “Event Queue”.

  • Node.js internally has a Component, known as “Event Loop”. Why it got this name is that it uses indefinite loop to receive requests and process them.

  • Event Loop uses Single Thread only. It is main heart of Node JS Platform Processing Model.

  • Event Loop checks any Client Request is placed in Event Queue. If not then wait for incoming requests for indefinitely.

  • If yes, then pick up one Client Request from Event Queue

    • Starts process that Client Request

    • If that Client Request Does Not requires any Blocking IO Operations, then process everything, prepare response and send it back to client.

    • If that Client Request requires some Blocking IO Operations like interacting with Database, File System, External Services then it will follow different approach

  • Checks Threads availability from Internal Thread Pool

  • Picks up one Thread and assign this Client Request to that thread.

  • That Thread is responsible for taking that request, process it, perform Blocking IO operations, prepare response and send it back to the Event Loop

You can check here for more details (very well explained).

Apoorva Chikara
  • 8,277
  • 3
  • 20
  • 35
  • Thanks for the explanation @Apoorva Chikara. I am also agreeing that code should run in asynchronous nature. But it is not happening in my case. Please refer the screenshots above. It is blocking for this code https://ibb.co/mDMR0qf. Is there any way can I check the availability of internal threads? Maybe internal threads are not available somehow. – Phani Vvs Apr 07 '21 at 04:11
  • If that explains the question you can accept and upvote it. – Apoorva Chikara Apr 07 '21 at 05:00
  • 1
    Sorry Apoorva Chikara, My question is why I am getting response in blocking nature. I added the state diagram in my question and I am familiar that it should be in non blocking in nature. The answer is because of chrome's blocking or stalling nature it is working differently. Thanks for the helping the community overall with your answer. I did upvote it but cannot accept it as an answer. – Phani Vvs Apr 07 '21 at 05:26
  • Good to see the answer! – Apoorva Chikara Apr 07 '21 at 05:42