I was recently asked by an interviewer whether Nodejs is single-threaded or multithreaded.
I know the answer that Nodejs is single threaded but the follow up question was how node application can handle multiple request than? why it is not blocking behavior? How Nodejs can achieve concurrency?
I was left blank even I worked with Nodejs with the 2 years.
I tried to look for answers and found great articles. I would like to share the concept of concurrency in simple terms and you can check the detailed articles for gaining better knowledge.
NodeJS is single threaded.
It uses the Event demultiplexing to enable non blocking requests.
The major steps of this process are:
- The resources will be added to a watchlist.
- The watchlist of this resource has the callback assigned to it which will be notified once the request got completed
- Callbacks is assigned to Event demultiplexer.
- Once the demultiplexer finish the requests it will pick the new set of event available for processing.
The process of checking the event, processing them and ready to check new request is known as event loop and this forms the concurrency in NodeJS.
Analogy
think of waiter which is NodeJS who is taking order from the customers which are the requests and send the I/O chefs to prepare in the kitchen.
Example
- Http request sent from UI to the server
- NodeJS maintains limited multiplexer to provide the services.
- Requests are placed in the “Event Queue”
- Event loop is the component of the NodeJS, uses indefinite loop to receive requests and process them.
- Event loop continuously checks for new request and wait until the new request comes.
- Once they got request:
- Client request process and send the response back to server.
The Interviewer might ask how to achieve concurrency in the NodeJS app if we require to do so?
Cluster
clustering is basically creating multiple process , where each worker process has it own event loop to take the requests.
It helps to achieve parallel process and concurrent multi threaded behavior as it helps to run to multiple process.
Example of implantation
const cluster = require('cluster'); const os = require('os'); const express = require('express'); const cpu = os.cpus().length; if (cluster.isMaster) { console.log(`process ${process.pid}`); for (let index = 0; index < cpu; index++) { cluster.fork(); } cluster.on('exit', (worker, code, signal) => { console.log(`process ${worker.process.pid} killed. Restarting...`); cluster.fork(); }); } else { const app = express(); // Configure your Express app // ... const server = app.listen(3000, () => { console.log(`Worker process ${process.pid} is listening on port 3000`); }); }
Worker threads
It helps to offload the CPU intensive operations from the loop so that multiple threads can be run for the specific tasks.
const { Worker, isMainThread, parentPort workerData } = require("worker_threads"); if (isMainThread) { const worker = new Worker(__filename, {workerData: "worker is enabled"}); worker.on("message", msg => console.log(`Worker got message: ${msg}`)); worker.on("error", err => console.error(error)); worker.on("exit", code => console.log(`Worker got exited ${code}.`)); } else { const data = workerData; parentPort.postMessage(`message passed "${data}".`); }
child processes
It is different concept than worker threads. When worker threads provide a separate loop, child process are separate instances of Nodejs runtime. Each child process has it’s own memory and it communicated with main process.
const express = require("express") const app = express() const { spawn } = require("child_process") app.get("/ls", (request, response) => { const lsval = spawn("lsval", ["-lash", request.query.directory]) lsval.stdout.on("data", data => { response.write(data.toString()) }) lsval.on("close", code => { console.log(`process exited ${code}`) response.end() }) }) app.listen(3000, () => console.log("listening on port 3000"))
Articles to understand in depth:
https://amplication.com/blog/nodejs-worker-threads-vs-child-processes-which-one-should-you-use
In Plain English 🚀
Thank you for being a part of the In Plain English community! Before you go:
- Be sure to clap and follow the writer ️👏️️
- Follow us: X | LinkedIn | YouTube | Discord | Newsletter
- Visit our other platforms: Stackademic | CoFeed | Venture | Cubed
- More content at PlainEnglish.io