Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does NodeJs handle so many incoming requests, does it use a thread pool?

When a request comes into a nodejs server, how does it handle the request?

I understand it has a different way of handling requests, as it doesn't spawn a new thread for each request (or I guess it doesn't use a traditional thread pool either).

Can someone explain to me what is going on under the hood, and does the flavour of linux matter here?

like image 240
codecompleting Avatar asked Nov 08 '11 19:11

codecompleting


2 Answers

No, it does async IO. There's only one thread that blocks until something happens somewhere, and then it handles that event. This means that one thread in one process can serve many concurrent connections. Somewhat like

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

The only exception is filesystem stuff, it uses a thread pool for that under the hood.

like image 92
thejh Avatar answered Sep 28 '22 18:09

thejh


Node tells the operating system (through epoll, kqueue, /dev/poll, or select) that it should be notified when a new connection is made, and then it goes to sleep. If someone new connects, then it executes the callback. Each connection is only a small heap allocation

It is "event driven" where it handles IO in an async fashion (non blocking I/O). It internally does threading needed to do epoll, kqueue, /dev/poll, or select handling, but for you as a user/client it is absolutely transparent.

e.g. epoll is not really a thread pool, but an OS' I/O event notification facility, that node.js sits on top of.

like image 28
tolitius Avatar answered Sep 28 '22 16:09

tolitius