I’m learning about node.js clustering from a course where we have the following setup.
process.env.UV_THREADPOOL_SIZE = 1;
const cluster = require("cluster");
if (cluster.isMaster) {
cluster.fork();
cluster.fork();
} else {
const express = require("express");
const crypto = require("crypto");
const app = express();
app.get("/", (req, res) => {
setTimeout(() => {
res.send("Hi There!");
}, 5000);
console.log(`Request handled by ${process.pid}`);
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Listening on ${PORT}`);
});
}
When I hit the endpoint using ab -c 2 -n 2 localhost:3000/
, I get 10 seconds of wait time. Following are my observations:
- Both the requests are being handled by two different child processes of node, which can be verified via the logs when this code is run.
- Both of them are being executed sequentially.
- I have tested the code on another machine as well which makes the chances of this being a cpu specific issue, very minimal.
- To my knowledge, there is no code i.e. blocking.
I don’t think there is anything else I can do at this point. If I can, pls help me with comments.