简体   繁体   中英

How does Node.js handle simultaneous requests with one thread?

I did some search on the question, but it seems like people only emphasize on Non-blocking IO.

Let's say if I just have a very simple application to respond "Hello World" text to the client, it still needs time to finish the execution, no matter how quick it is. What if there are two request coming in at exactly the same time, how does Node.js make sure both requests will be processed with one thread?

I read the blog Understanding the node.js event loop which says "Of course, on the backend, there are threads and processes for DB access and process execution". That statement is regarding IO, but I also wonder if there is separate thread to handle the request queue. If that's the case, can I say that the Node.js single thread concept only applies to the developers who build applications on Node.js, but Node.js is actually running on multi-threads behind the scene?

The operating system gives each socket connection a send and receive queue. That is where the bytes sit until something at the application layer handles them. If the receive queue fills up no connected client can send information until there is space available in the queue. This is why an application should handle requests as fast as possible.

If you are on a *nix system you can use netstat to view the current number of bytes in the send and receive queues. In this example, there are 0 bytes in the receive queue and 240 bytes in the send queue (waiting to be sent out by the OS).

Proto Recv-Q Send-Q Local Address               Foreign Address             State
tcp        0      240 x.x.x.x:22                 x.x.x.x:*                   LISTEN

On Linux you can check the default size and max allowed size of the send/receive queues with the proc file system:

Receive:

cat /proc/sys/net/core/rmem_default
cat /proc/sys/net/core/rmem_max

Send:

cat /proc/sys/net/core/wmem_max
cat /proc/sys/net/core/wmem_default

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM