NodeJS

NodeJS is based on Chrome's V8 Engine. Initially, lang.js (Private) was intended for use in browser. In the browser, V8 was the compiler for JS. The creator of nodeJS simply made a runtime on top of this V8 engine.

Even though it is single-threaded, nodeJS still allows Asynchronous execution via Event Driven architecture. It uses the Main Loop or event loop to stay in the memory, while spinning up seperate worker threads for blocking operations. The threads are not created on-demand, but rather fetched from a Thread Pool.

Non-blocking operations are executed in the main execution itself. The blocking operations are moved to an event queue.

Flow of a request coming to a node JS server

sequenceDiagram Client ->> Event Queue: Non-blocking request Event Loop ->> Event Queue: New request aaya kya? Event Queue ->> Event Loop: Yes Event Loop ->> Event Loop: Blocking hai ya non blocking? Event Loop ->> Event Loop: Non-Blocking Event Loop ->> Event Loop: Ye to main khud hi kar lunga Client ->> Event Queue: Blocking request Event Loop ->> Event Queue: New request aaya kya? Event Queue ->> Event Loop: Yes Event Loop ->> Event Loop: Blocking hai ya non blocking? Event Loop ->> Event Loop: Blocking Event Loop ->> Thread Pool: Dekh le bhai. Call karna fir Thread Pool ->> Event Loop: Ho gaya. Dekh lo Event Loop ->> Event Loop: Call back function

Backlinks