Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs single thread? how does it works if there are lots of concurrent requests

I am new to nodejs, currently learning it. I know nodejs is single-threaded, however I am wondering: if I use non-blocking method, for example, there are 20000 concurrent requests, and there is one request need to take a long time, other threads takes shorter time.

So once we met the longer time request, nodejs will say "hi, please pause, other request need to use", is there a default time for nodejs to detect whether this need to be paused? Correct me if I use the wrong word "puase" (because I think since it is single-thread, so it should stop computing for one request, if another request wants to be processed).

In the situation I assumed, then the longer request need lots of time that it can be processed?

like image 264
adiggo Avatar asked Feb 12 '23 18:02

adiggo


1 Answers

It is true that node.js can only receive or respond to a single request at a time, however, it can process multiple requests at the same time.

For example, lets say you have a node.js application that is a rest api, and every request results in a call to the database except for endpoint C. The rest api has 3 endpoints.

Endpoint A: takes 3 seconds to talk to the database

Endpoint B: takes 2 seconds to talk to the database

Endpoint C: does not talk to the database, it just returns static text.

It isn't possible for more than 1 request to happen at the same time. One is always first, regardless of how close the timestamps are.

Now, lets say we have 10 requests happening at the same time, in this order:

ABCBCCAABC

Here's how they will be received and responded to:

REC:A
REC:B
REC:C
RESP:C
REC:B
REC:C
RESP:C
REC:C
RESP:C
REC:A
REC:A
REC:B
REC:C
RESP:C
// 2 seconds later
RESP:B
RESP:B
RESP:B
// 1 second later
RESP:A
RESP:A
RESP:A

C always happens immediately because it doesn't do any asynchronous actions. You then receive all of B, and then all of A because B is faster than A.

The order of B and A may differ though based on database setup, for example, if it's set to only handle x queries at a time.

If we introduced and endpoint D that performs a synchronous action that takes 6 seconds, you'll get this result:

ADABC

REQ:A
REQ:D
// 6 long seconds later...
RESP:D
REQ:A
REQ:B
REQ:C
RESP:C
// 2 seconds later...
RESP:B
// 1 second later...
RESP:A
RESP:A

because D stops all javascript processing while it's synchronous action takes place. 1 endpoint that performs a synchronous action is an easy point of failure for someone to bring your api to a halt.

like image 193
Kevin B Avatar answered Feb 15 '23 11:02

Kevin B