How does node process concurrent requests?
Asked Answered
E

6

35

I have been reading up on nodejs lately, trying to understand how it handles multiple concurrent requests. I know NodeJs is a single threaded event loop based architecture, and at a given point in time only one statement is going to be executing, i.e. on the main thread and that blocking code/IO calls are handled by the worker threads (default is 4).

Now my question is, what happens when a web server built using NodeJs receives multiple requests? I know that there are lots of similar questions here, but haven't found a concrete answer to my question.

So as an example, let's say we have following code inside a route like /index:

app.use('/index', function(req, res, next) {
    
    console.log("hello index routes was invoked");
    
    readImage("path", function(err, content) {
        status = "Success";
        if(err) {
            console.log("err :", err);
            status = "Error"
        }
        else {
            console.log("Image read");
        }
        return res.send({ status: status });
    });

    var a = 4, b = 5;
    console.log("sum =", a + b);
});

Let's assume that the readImage() function takes around 1 min to read that Image.
If two requests, T1, and T2 come in concurrently, how is NodeJs going to process these request ?

Does it going to take first request T1, process it while queueing the request T2? I assume that if any async/blocking stuff is encountered like readImage, it then sends that to a worker thread (then some point later when async stuff is done that thread notifies the main thread and main thread starts executing the callback?), and continues by executing the next line of code?

When it is done with T1, it then processes the T2 request? Is that correct? Or it can process T2 in between (meaning whilethe code for readImage is running, it can start processing T2)?

Is that right?

Eldredge answered 24/7, 2018 at 10:7 Comment(12)
infoq.com/interviews/node-ryan-dahlKilburn
quora.com/How-many-connections-can-a-node-js-server-handleKilburn
Those articles don't quite explain how, but they talk around it, this one talks a little bit more indepth medium.com/the-node-js-collection/…Kilburn
@CallumLinington thanks! for sharing the links. I really appreciate your help, will go through them. But what I am trying to understand is that whether node process T1 first and when it is done with T1, moves to T2 or there is something else going on behind the scene.Eldredge
Finally, this #34855852 answer on stackoverflow seems like what you're askingKilburn
I have been through that article as well :). but it doesn''t say anything about the time when exactly the second request is picked by main thread to be executed. considering the code i have shared above, is it after T1 has processed or after the async request(readImage) is sent to worker thread. I am not sure but guessing like the answer lies in between the different phases of event loop. Would be really nice if someone can explain this with respect to my code.Eldredge
It actually does, "Let's assume that readImage() takes around 1 min to read that Image, so If two request T1, and T2 came concurently How nodejs is gonna process this ?" that is your question, it says "So our single-threaded app is actually leveraging the multi-threaded behaviour of another process"Kilburn
Yep, but when does it starts executing T2, after it processes T1? Or something else?Eldredge
docs.google.com/presentation/d/… is example of list of relevant results from search 'event loop' on hacker news. have u done such a search?Hath
Yes, I did. But it doesn't say anything about concurrent requests. I am curious to know if two concurrent requests came, is it likely that node is gonna queue one and process it after the other request has been process completely Here is a link #36542904 that describe the same thing, the answer say same thing.Eldredge
So at this point my thinking is that node will pick the first request, then start processing it, if it is a blocking process it gonna send it to worker thread (depending on the kind of tasks because some blocking stuff use system kernel) which will notify the main thread some point later to execute callback if any, and after it processes the first request, it is gonna move on to second one. this is where my confusion lies. If it processes requests on by one(excluding the worker pool task) then how the requests have same latency time.Eldredge
I think node blog will give you better idea. nodejs.org/en/docs/guides/blocking-vs-non-blockingBurger
D
31

Your confusion might be coming from not focusing on the event loop enough. Clearly you have an idea of how this works, but maybe you do not have the full picture yet.

Part 1, Event Loop Basics

When you call the use method, what happens behind the scenes is another thread is created to listen for connections.

However, when a request comes in, because we're in a different thread than the V8 engine (and cannot directly invoke the route function), a serialized call to the function is appended onto the shared event loop, for it to be called later. ('event loop' is a poor name in this context, as it operates more like a queue or stack)

At the end of the JavaScript file, the V8 engine will check if there are any running theads or messages in the event loop. If there are none, it will exit with a code of 0 (this is why server code keeps the process running). So the first Timing nuance to understand is that no request will be processed until the synchronous end of the JavaScript file is reached.

If the event loop was appended to while the process was starting up, each function call on the event loop will be handled one by one, in its entirety, synchronously.

For simplicity, let me break down your example into something more expressive.

function callback() {
    setTimeout(function inner() {
        console.log('hello inner!');
    }, 0); // †
    console.log('hello callback!');
}

setTimeout(callback, 0);
setTimeout(callback, 0);

setTimeout with a time of 0, is a quick and easy way to put something on the event loop without any timer complications, since no matter what, it has always been at least 0ms.

In this example, the output will always be:

hello callback!
hello callback!
hello inner!
hello inner!

Both serialized calls to callback are appended to the event loop before either of them is called. This is guaranteed. That happens because nothing can be invoked from the event loop until after the full synchronous execution of the file.

It can be helpful to think of the execution of your file, as the first thing on the event loop. Because each invocation from the event loop can only happen in series, it becomes a logical consequence, that no other event loop invocation can occur during its execution; Only when the previous invocation is finished, can the next event loop function be invoked.

Part 2, The inner Callback

The same logic applies to the inner callback as well, and can be used to explain why the program will never output:

hello callback!
hello inner!
hello callback!
hello inner!

Like you might expect.

By the end of the execution of the file, two serialized function calls will be on the event loop, both for callback. As the Event loop is a FIFO (first in, first out), the setTimeout that came first, will be be invoked first.

The first thing callback does is perform another setTimeout. As before, this will append a serialized call, this time to the inner function, to the event loop. setTimeout immediately returns, and execution will move on to the first console.log.

At this time, the event loop looks like this:

1 [callback] (executing)
2 [callback] (next in line)
3 [inner]    (just added by callback)

The return of callback is the signal for the event loop to remove that invocation from itself. This leaves 2 things in the event loop now: 1 more call to callback, and 1 call to inner.

Now callback is the next function in line, so it will be invoked next. The process repeats itself. A call to inner is appended to the event loop. A console.log prints Hello Callback! and we finish by removing this invocation of callback from the event loop.

This leaves the event loop with 2 more functions:

1 [inner]    (next in line)
2 [inner]    (added by most recent callback)

Neither of these functions mess with the event loop any further. They execute one after the other, the second one waiting for the first one's return. Then when the second one returns, the event loop is left empty. This fact, combined with the fact that there are no other threads currently running, triggers the end of the process, which exits with a return code of 0.

Part 3, Relating to the Original Example

The first thing that happens in your example, is that a thread is created within the process which will create a server bound to a particular port. Note, this is happening in precompiled C++ code, not JavaScript, and is not a separate process, it's a thread within the same process. see: C++ Thread Tutorial.

So now, whenever a request comes in, the execution of your original code won't be disturbed. Instead, incoming connection requests will be opened, held onto, and appended to the event loop.

The use function, is the gateway into catching the events for incoming requests. Its an abstraction layer, but for the sake of simplicity, it's helpful to think of the use function like you would a setTimeout. Except, instead of waiting a set amount of time, it appends the callback to the event loop upon incoming http requests.

So, let's assume that there are two requests coming in to the server: T1 and T2. In your question you say they come in concurrently, since this is technically impossible, I'm going to assume they are one after the other, with a negligible time in between them.

Whichever request comes in first, will be handled first by the secondary thread from earlier. Once that connection has been opened, it's appended to the event loop, and we move on to the next request, and repeat.

At any point after the first request is added to the event loop, V8 can begin execution of the use callback.


A quick aside about readImage

Since its unclear whether readImage is from a particular library, something you wrote or otherwise, it's impossible to tell exactly what it will do in this case. There are only 2 possibilities though, so here they are:

  1. It's entirely synchronous, never using an alternate thread or the event loop
    function readImage (path, callback) {
        let image = fs.readFileSync(path);
        callback(null, image);
        // a definition like this will force the callback to
        // fully return before readImage returns. This means
        // means readImage will block any subsequent calls.
    }
  1. It's entirely asynchronous, and takes advantage of fs' async callback.
    function readImage (path, callback) {
        fs.readFile(path, (err, data) => {
            callback(err, data);
        });
        // a definition like this will force the readImage
        // to immediately return, and allow exectution
        // to continue.
    }

For the purposes of explanation, I'll be operating under the assumption that readImage will immediately return, as proper asynchronous functions should.


Once the use callback execution is started, the following will happen:

  1. The first console log will print.
  2. readImage will kick off a worker thread and immediately return.
  3. The second console log will print.

During all of this, its important to note, these operations are happening synchronously; No other event loop invocation can start until these are finished. readImage may be asynchronous, but calling it is not, the callback and usage of a worker thread is what makes it asynchronous.

After this use callback returns, the next request has probably already finished parsing and was added to the event loop, while V8 was busy doing our console logs and readImage call.

So the next use callback is invoked, and repeats the same process: log, kick off a readImage thread, log again, return.

After this point, the readImage functions (depending on how long they take) have probably already retrieved what they needed and appended their callback to the event loop. So they will get executed next, in order of whichever one retrieved its data first. Remember, these operations were happening in separate threads, so they happened not only in parallel to the main javascript thread, but also parallel to each other, so here, it doesn't matter which one got called first, it matters which one finished first, and got 'dibs' on the event loop.

Whichever readImage completed first will be the first one to execute. So, assuming no errors occured, we'll print out to the console, then write to the response for the corresponding request, held in lexical scope.

When that send returns, the next readImage callback will begin execution: console log, and writing to the response.

At this point, both readImage threads have died, and the event loop is empty, but the thread that holds the server port binding is keeping the process alive, waiting for something else to add to the event loop, and the cycle to continue.

I hope this helps you understand the mechanics behind the asynchronous nature of the example you provided.

Dede answered 30/7, 2018 at 17:26 Comment(9)
If you want to go into detail about how the threading works behind the scenes, I'd at least mention Node's usage of libuv for implementing its asynchronous APIs.Kiersten
@PatrickRoberts I agree, though I'm not extremely knowledgeable in node's particular implementation, using lubuv. You're more than welcome to edit in additional informationDede
@Marcus you said "When you call the use method, what happens behind the scenes is another thread is created to listen for connections", I don't get it. If that is the case then let's say i have 10 use statement, will it create 10 threads?Eldredge
@Eldredge not quite, you never showed the part of the code where you actually open the server to listen on a port, but that would be the line where the thread is officially created. The use statement is the interface to that thread. So, if you have 10, you will still only have one server thread that will call the appropriate use callback when needed.Dede
sorry, but what "a serialized call to the function " really is?Commonwealth
@MuhammadAtif What I meant by "serialized call" is a piece of data which describes the intention to invoke a function. Serialized may not be the precise word to use here, as I'm not sure about the underlying implementation, however, the idea was to point out that there is some simple message describing the intention to invoke a function, that is passed between threads.Dede
This answer is not only misleading but completely wrong. Node does not and has never ever created a new thread for listening for network requests. Node only use additional threads for handling disk I/O but all network I/O are not threaded at all. What node uses behind the scenes are OS event queues (separate from node's or libuv's own event queeu - basically libuv's event queue uses OS event queue) and what threads use behind the scenes are also OS event queues. Basically node uses an OS feature that is lower level than threads - a feature the OS uses to implement threads themselves...Goatskin
... the OS event queue exists to service interrupts. And this is where I/O processing can happen in parallel in the hardware and the hardware itself (not the OS and not even the CPU) will interrupt the CPU when the I/O operation is complete. The OS (or rather the device driver) will handle the interrupt and process pending events in the OS event queue (mouse events, GPU update event, network card events etc.). Interrupts are not a software feature and therefore does not need any additional code to execute in additional threads to process them.Goatskin
... Also remember, no matter how many threads languages like C# or Java use to handle network requests your network card (or wifi hardware) and your router connected to your ISP will only send one bit at a time - which means all the multiple threads can only send one bit at a time to the internet and therefore only one thread at any time is acutally doing anything - the rest are simply using 0% CPU cycles to do nothing. All threads do is waste RAM. This is what makes node.js more efficient at handling network requests though not more efficient at doing things like encoding mp3Goatskin
N
7

For each incoming request, node will handle it one by one. That means there must be order, just like the queue, first in first serve. When node starts processing request, all synchronous code will execute, and asynchronous will pass to work thread, so node can start to process the next request. When the asynchrous part is done, it will go back to main thread and keep going.

So when your synchronous code takes too long, you block the main thread, node won't be able to handle other request, it's easy to test.

app.use('/index', function(req, res, next) {
    // synchronous part
    console.log("hello index routes was invoked");
    var sum = 0;
    // useless heavy task to keep running and block the main thread
    for (var i = 0; i < 100000000000000000; i++) {
        sum += i;
    }
    // asynchronous part, pass to work thread
    readImage("path", function(err, content) {
        // when work thread finishes, add this to the end of the event loop and wait to be processed by main thread
        status = "Success";
        if(err) {
            console.log("err :", err);
            status = "Error"
        }
        else {
            console.log("Image read");
        }
        return res.send({ status: status });
    });
    // continue synchronous part at the same time.
    var a = 4, b = 5;
    console.log("sum =", a + b);
});

Node won't start processing the next request until finish all synchronous part. So people said don't block the main thread.

Naraka answered 28/7, 2018 at 8:11 Comment(2)
Are there multiple threads? I thought nodejs had one single thread.Lamellate
@Lamellate there is one process, multiple threads, but only one thread dedicated to parsing and executing javascript code. the other threads are started from C++ bindings called from the js.Dede
E
3

There are a number of articles that explain this such as this one

The long and the short of it is that nodejs is not really a single threaded application, its an illusion. The diagram at the top of the above link explains it reasonably well, however as a summary

  • NodeJS event-loop runs in a single thread
  • When it gets a request, it hands that request off to a new thread

So, in your code, your running application will have a PID of 1 for example. When you get request T1 it creates PID 2 that processes that request (taking 1 minute). While thats running you get request T2 which spawns PID 3 also taking 1 minute. Both PID 2 and 3 will end after their task is completed, however PID 1 will continue listening and handing off events as and when they come in.

In summary, NodeJS being 'single threaded' is true, however its just an event-loop listener. When events are heard (requests), it passes them off to a pool of threads that execute asynchronously, meaning its not blocking other requests.

Extensity answered 27/7, 2018 at 5:49 Comment(3)
you said "When it gets a request, it hands that request off to a new thread" this isn't exactly true, every blocking request doesn't go to thread pool.Eldredge
every statement except the readImage() are synchronous statement, so it's gonna execute on main thread. ReadImage() let's say reading a binary file or something from your local path. This is async and takes 1 min let's say. So my question is if we have recieved another request T2, so is it gonna execute after all the sync operation for T1 is done or something?Eldredge
No, the only thing on the main thread is the listener. You ask something to happen, anything to happen, it will hand that off. All code you write is sitting behind a listener. In your context, readImage() is part of the request. That request in its self has already been passed to a threadpool. Each time someone hits /index the request is passed to the thread pool and all the code is run in that pool for that specific request / user.Extensity
A
2

You can simply create child process by shifting readImage() function in a different file using fork().

The parent file, parent.js:

const { fork } = require('child_process');
const forked = fork('child.js');
forked.on('message', (msg) => {
   console.log('Message from child', msg);
});

forked.send({ hello: 'world' });

The child file, child.js:

process.on('message', (msg) => {
  console.log('Message from parent:', msg);
});

let counter = 0;

setInterval(() => {
  process.send({ counter: counter++ });
}, 1000);

Above article might be useful to you .

In the parent file above, we fork child.js (which will execute the file with the node command) and then we listen for the message event. The message event will be emitted whenever the child uses process.send, which we’re doing every second.

To pass down messages from the parent to the child, we can execute the send function on the forked object itself, and then, in the child script, we can listen to the message event on the global process object.

When executing the parent.js file above, it’ll first send down the { hello: 'world' } object to be printed by the forked child process and then the forked child process will send an incremented counter value every second to be printed by the parent process.

Alaster answered 24/7, 2018 at 10:38 Comment(2)
Thanks for the article, But I just want to understand how it processes that two requests concurrently. Actually we don't need child process for this because this readImage() is an async task which can be easily handled by worker thread.Eldredge
Why do we need a child process here?Eldredge
H
0

The V8 JS interpeter (ie: Node) is basically single threaded. But, the processes it kicks off can be async, example: 'fs.readFile'.

As the express server runs, it will open new processes as it needs to complete the requests. So the 'readImage' function will be kicked off (usually asynchronously) meaning that they will return in any order. However the server will manage which response goes to which request automatically.

So you will NOT have to manage which readImage response goes to which request.

So basically, T1 and T2, will not return concurrently, this is virtually impossible. They are both heavily reliant on the Filesystem to complete the 'read' and they may finish in ANY ORDER (this cannot be predicted). Note that processes are handled by the OS layer and are by nature multithreaded (in a modern computer).

If you are looking for a queue system, it should not be too hard to implement/ensure that images are read/returned in the exact order that they are requested.

Hicks answered 2/8, 2018 at 16:15 Comment(0)
L
-1

Since there's not really more to add to the previous answer from Marcus - here's a graphic that explains the single threaded event-loop mechanism:

enter image description here

Leukemia answered 2/8, 2018 at 16:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.