What if your app could use all your computer's power without breaking under pressure?
Why Master and worker processes in Node.js? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a busy restaurant kitchen where one chef tries to cook all dishes alone, handling every order from start to finish.
When one chef does everything, orders pile up, mistakes happen, and customers wait too long. The chef gets overwhelmed and slows down.
Master and worker processes split the work: the master assigns tasks, and workers cook orders in parallel, making the kitchen faster and more reliable.
const http = require('http'); http.createServer((req, res) => { // handle all requests here res.end('Hello World'); }).listen(3000);
const cluster = require('cluster'); if (cluster.isMaster) { cluster.fork(); cluster.fork(); } else { require('http').createServer((req, res) => { // handle requests res.end('Hello World'); }).listen(3000); }
This lets your app handle many tasks at once, using all CPU cores efficiently without crashing everything if one part fails.
A web server uses master and worker processes to serve thousands of users simultaneously, keeping the site fast and stable.
One process doing all work is slow and fragile.
Master assigns tasks, workers do the work in parallel.
Improves speed, reliability, and resource use.
Practice
Solution
Step 1: Understand the master process role
The master process is responsible for creating and managing worker processes in the cluster module.Step 2: Differentiate master from worker tasks
Workers run the app code and handle requests, while the master only manages them.Final Answer:
To create and manage worker processes -> Option AQuick Check:
Master manages workers [OK]
- Thinking master handles requests directly
- Confusing master with worker process
- Assuming master runs app code
Solution
Step 1: Recall cluster module properties
The cluster module provides isMaster and isWorker boolean properties to identify process roles.Step 2: Identify correct syntax
To check if current process is master, use cluster.isMaster, not process properties.Final Answer:
if (cluster.isMaster) { ... } -> Option DQuick Check:
Use cluster.isMaster to check master process [OK]
- Using process.isMaster which does not exist
- Confusing isWorker with isMaster
- Using wrong object for the check
const cluster = require('cluster');
const http = require('http');
if (cluster.isMaster) {
cluster.fork();
cluster.fork();
} else {
http.createServer((req, res) => {
res.end('Worker ' + process.pid);
}).listen(8000);
}What will happen when you visit
http://localhost:8000?Solution
Step 1: Identify which process creates the server
The else block runs in worker processes, which create the HTTP server listening on port 8000.Step 2: Understand request handling
Requests are handled by one of the two worker processes forked by the master, each responding with its process ID.Final Answer:
You get response 'Worker <pid>' from one of the two workers -> Option AQuick Check:
Workers handle requests, master does not listen [OK]
- Thinking master handles requests
- Assuming master listens on port
- Believing no server is created
const cluster = require('cluster');
if (cluster.isMaster) {
cluster.fork();
cluster.fork();
} else {
console.log('Worker started');
}Why might the workers never start properly?
Solution
Step 1: Analyze worker code behavior
The workers only log 'Worker started' and then exit immediately because no server or event loop keeps them alive.Step 2: Understand cluster.fork usage
cluster.fork() is correctly called in master block, so workers start but exit quickly.Final Answer:
Because the workers have no code to keep them alive -> Option BQuick Check:
Workers exit if no server or event loop runs [OK]
- Thinking missing http require stops workers
- Confusing cluster.fork placement
- Assuming cluster.isMaster is always false
Solution
Step 1: Use all CPU cores with cluster.fork()
Fork one worker per CPU core by looping over the number of CPUs.Step 2: Restart crashed workers by listening to 'exit'
Listen to the 'exit' event on cluster to detect worker crashes and fork a new worker to replace it.Final Answer:
Use cluster.fork() for each CPU core and listen to 'exit' event to fork a new worker -> Option CQuick Check:
Fork per CPU + restart on exit [OK]
- Forking only once and expecting auto-restart
- Restarting workers inside workers themselves
- Not handling worker crashes properly
