0

What will happen if there is no IO in current scenario? Will the requests get executed in synchronous way and each request will have to wait for previous request?

Are all node.js request initially sent to callback queue? Otherwise wont the stack throw stack overflow in case all requests are being served by stack...

I have just started using node and completely not sure about how things are working? Every online site says think of event loop as a waiter in a restaurant taking order but I do not get how node handles requests in case there are hundreds of request burst load. Will these request be kept on hold in some sort of queue?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 2
    Yes, the event loop keeps a queue of events that need to be handled (including opened connections) – Bergi Dec 10 '17 at 11:52
  • Can you please share a link of node js architecture?online their are number of images of architecture but they all differ.. – a.developer Dec 11 '17 at 07:38
  • [These](https://medium.com/the-node-js-collection/what-you-should-know-to-really-understand-the-node-js-event-loop-and-its-metrics-c4907b19da4c) [links](https://stackoverflow.com/questions/10680601/nodejs-event-loop) are the first I could find in my search engine – Bergi Dec 11 '17 at 11:12
  • Possible duplicate of [Nodejs Event Loop](https://stackoverflow.com/questions/10680601/nodejs-event-loop) – Milan Velebit Jun 29 '19 at 20:01

1 Answers1

1

The Event demultiplexer is a notification-issuing interface within the Node JS. It is used to gather every request from watched sources in form of an event and queues each event in a queue. It is the demultiplexer that forms the Event Queue. Event demultiplexer is an API run by Libuv. enter image description here

All the collected requests are distributed in those different event queues. The queue at the top takes highest priority, and the queue at the bottom takes the least.

So, event loop checks timers queue first and if there are events that are ready to be passed to call stack with its callback to be executed, event loop will handle it. After event loop is done with timers queues, however before jumping to next queue, event loop will look at 2 other queues that run by node itself, and will handle those queues first. those are:

Next Ticks Queue — Callbacks added using process.nextTick function Other Microtasks Queue — Includes other microtasks such as resolved promise callbacks, console.log etc.

After event loop is done with those 2 queues, then it will move to next queue which is I/O callbacks.

Note that Node event loop is single threaded. However for computationally intensive or time consuming tasks libuv has thread pool which by default has 4 pools, not to block the event loop. There are only four things that use this thread pool - DNS lookup, fs, crypto and zlib. Everything else executes in the main thread irrespective or blocking or non blocking.

So far we had event loop which handles events in event queues and thread pool that run by libuv for some other functions. Node standard library also has some functions that make use of code that is built into the underlying operating system through libuv. All modern operating systems provide APIs to interact with libuv.

Libuv delegates some tasks like network I/O and fs module's functions to operating system, so OS runs those tasks and brings the results to Libuv.

Yilmaz
  • 35,338
  • 10
  • 157
  • 202