How does NodeJ handle so many inbound requests, does it use a thread pool?

When a request arrives at a nodejs server, how does it process the request?

I understand that it has a different way of handling requests, since it does not spawn a new thread for each request (or, I think, it does not use the traditional thread pool).

Can someone explain to me what is happening under the hood and does linux make sense here?

+7
source share
2 answers

Node tells the operating system (via epoll , kqueue , /dev/poll or select ) that it should be notified when a new connection is created, and then it goes into sleep mode. If someone new connects, he makes a callback. Each connection represents only a small heap distribution.

It is event driven, where it processes IO in asynchronous (non-blocking I / O) mode. It internally executes the threads necessary to process epoll , kqueue , /dev/poll or select , but for you as a user / client it is completely transparent.

eg. epoll is actually not a thread pool, but an OS I / O event notification tool that node.js sits on top of.

+3
source

No, it does asynchronous I / O. There is only one thread that blocks until something happens somewhere, and then processes this event. This means that one thread in one process can serve many parallel connections. Something like

 endless loop { event = next_event() dispatch_event(event) } 

The only exception is the file system, it uses a thread pool for this under the hood.

+6
source

All Articles