Rawhi
Rawhi

Reputation: 6413

Node.js threadpool

Iv'e been reading a lot about how Node.js works and why it could be a better choice when you dealing with many IO requests, however .. the main advantage is that node.js is a single thread model consists of one main thread (event loop) which use in the background a worker thread for each IO operation so it will always be there to serve more requests all the time .. in contrast to the regular request-response model which assigns a thread for each requests and when there is no more threads in the thread bool the new requests should wait in queue till some thread ends.

So can't Node.js have the same issue when assigning a worker for each IO operation, knowing that the threadpool has a limited number of threads.

Thank you

Upvotes: 0

Views: 297

Answers (1)

jfriend00
jfriend00

Reputation: 707228

node.js does not use threads at all for incoming network requests. Incoming requests are queued by the underlying socket infrastructure and a queued request is serviced through the internal node.js event queue when node.js finishes up a prior operation and then goes to the event queue for the next thing to do.

The limit on how many incoming network requests can be in flight at once will most likely be dictated by the underlying OS/TCP stack and how many requests it will queue before refusing the next incoming connection. The HTTP library in node.js does do some connection pooling (in the interest of increasing performance) when making lots out outbound requests to the same host, but that is different than incoming requests and the connection pooling can be bypassed if it is not desirable.

There are other parts of node.js that do use an internal thread pool to make the async behavior work (such as disk I/O). If you try to run more async disk operations than there are threads in the thread pool, then the thread pool will queue the request to start running when a thread frees up. Since the interface to the requests are async, it can just add the event to an internal queue and then service it later when it has a thread available to allocate for it.

Upvotes: 3

Related Questions