Search code examples
node.jsmultithreadingevent-loop

Does a node server have multiple event loops?


Does a single node server have multiple event loops? want to know how multiple threads of a server machine gets used.

Is it like if 10 threads are available in a machine and so ten event loops are created?

New to javascript, server-side development and want to understand how things work under the hood.


Solution

  • Does a single node server have multiple event loops? [..] Is it like if 10 threads are available in a machine and so ten event loops are created?

    No, the NodeJS Event Loop is only one.

    want to know how multiple threads of a server machine gets used.

    Citing from NodeJS site event-loop-timers-and-nexttick

    Since most modern kernels are multi-threaded, they can handle multiple operations executing in the background. When one of these operations completes, the kernel tells Node.js so that the appropriate callback may be added to the poll queue to eventually be executed [by the event loop]

    Since NodeJS 10.5 you can use threads, their results are managed by the same event loop that manage all async operations like IO readings.