Nodejs single thread? how does it work if there are many simultaneous requests

I am new to nodejs, currently studying it. I know that nodejs is single-threaded, however I wonder: if I use a non-blocking method, for example, there are 20,000 simultaneous requests, and one request takes a long time, other threads take a shorter time. Therefore, as soon as we meet a request with a longer time, nodejs will say “hi, please pause, another request needs to be used”, is there a default time for nodejs to determine if it needs to be suspended? Correct me if I use the wrong word "puase" (because I think it is single-threaded, so it should stop computing one request if another request wants to be processed.). In the situation that I suggested, then a longer request takes a lot of time,so that it can be processed?

Thanks for the help.

+4
source share
1 answer

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

For example, let's say you have a node.js application that is a priori rest, and each query results in a database call, with the exception of endpoint C. The rest of the api has 3 endpoints.

Endpoint A: It takes 3 seconds to work with the database

Endpoint B: It takes 2 seconds to talk to the database

Endpoint C: Doesn't talk to the database, it just returns static text.

. , , .

, , 10 , , :

ABCBCCAABC

, :

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 , . B, A, B , A.

B A , , , .

D, , 6 , :

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

D javascript, . 1, , , - api.

+7

All Articles