martinho
martinho

Reputation: 3409

In Node js, what happens if a new request arrives and event loop is already busy processing a request?

I have this file named index.js:

const express = require('express')
const app = express()
const port = 3000

app.get('/home', (req, res) => {
    res.send('Hello World!')
})

app.get('/route1', (req, res) => {
    var num = 0;
    for(var i=0; i<1000000; i++) {
        num = num+1;
        console.log(num);
    }
    res.send('This is Route1 '+ num)
})

app.listen(port, () => console.log(`Example app listening on port ${port}!`))

I first call the endpoint /route1 and then immediately the endpoint /home. The /route1 has for loop and takes some time to finish and then /home runs and finishes. My question is while app was busy processing /route1, how was the request to /home handled, given node js is single threaded?

Upvotes: 4

Views: 2510

Answers (3)

Venkatesh A
Venkatesh A

Reputation: 2504

Node has an internal thread pool from which a thread is assigned when a blocking(io or memeory or network) request is sent. If not, then the request is processed and sent back as such. If the thread pool is full, the request waits in the queue. Refer How, in general, does Node.js handle 10,000 concurrent requests? for more clear answers.

Upvotes: 0

mohit
mohit

Reputation: 1968

Nodejs has event loop and event loop allows nodejs to perform non blocking I/O operation. Each event loop iteration is called a tick. There are different phases of the event loop.

First is timer phase, since there are no timers in your script event loop will go further to check I/O script.

  1. When you hit route /route1, Node JS Web Server internally maintains a Limited Thread pool to provide services to the Client Requests. It will be placed in FIFO queue then event loop will go further to polling phase.

  2. Polling phase will wait for pending I/O, which is route /route1. Even Loop checks any Client Request is placed in Event Queue. If no, then wait for incoming requests for indefinitely.

  3. Meanwhile next I/O script arrives in FIFO queue which is route /home.

FIFO means, first in first out. Therefore first /route1 will get execute the route /home

Below you can see this via diagram.

enter image description here

A Node.js application runs on single thread and the event loop also runs on the same thread

Node.js internally uses the libuv library which is responsible for handling operating system related tasks, like asynchronous I/O based operation systems, networking, concurrency.

More info

Upvotes: 0

jfriend00
jfriend00

Reputation: 708026

The incoming request will be queued in the nodejs event queue until nodejs gets a chance to process the next event (when your long running event handler is done).

Since nodejs is an event-driven system, it gets an event from the event queue, runs that event's callback until completion, then gets the next event, runs it to completion and so on. The internals of nodejs add things that are waiting to be run to the event queue so they are queued up ready for the next cycle of the event loop.

Depending upon the internals of how nodejs does networking, the incoming request might be queued in the OS for a bit and then later moved to the event queue until nodejs gets a chance to serve that event.

My question is while app was busy processing /route1, how was the request to /home handled, given node js is single threaded?

Keep in mind that node.js runs your Javascript as single threaded (though we do now have Worker Threads if you want), but it does use threads internally to manage things like file I/O and some other types of asynchronous operations. It does not need threads for networking, though. That is managed with actual asynchronous interfaces from the OS.

Upvotes: 2

Related Questions