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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With