Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is load balancing between workers in Node.js?
Load balancing between workers means distributing incoming tasks or requests evenly across multiple worker processes to improve performance and reliability.
Click to reveal answer
beginner
Which Node.js module helps create worker processes for load balancing?
The 'cluster' module allows Node.js to create multiple worker processes that share the same server port, enabling load balancing.
Click to reveal answer
intermediate
How does the Node.js cluster module distribute incoming connections by default?
By default, the cluster module uses a round-robin approach on most platforms, sending each new connection to the next available worker in order.
Click to reveal answer
beginner
Why is load balancing important when using multiple workers?
Load balancing prevents any single worker from becoming overloaded, which helps keep the app responsive and stable under heavy traffic.
Click to reveal answer
intermediate
What happens if a worker crashes in a Node.js cluster setup?
The master process can detect the crash and spawn a new worker to replace it, maintaining the load balancing and availability.
Click to reveal answer
Which Node.js module is commonly used for load balancing between workers?
Ahttp
Bcluster
Cfs
Devents
✗ Incorrect
The 'cluster' module is designed to create worker processes and balance load between them.
What load balancing method does Node.js cluster use by default on most platforms?
ARound-robin
BPriority queue
CLeast connections
DRandom
✗ Incorrect
Node.js cluster uses round-robin to distribute connections evenly among workers.
Why should you use multiple workers in a Node.js app?
ATo avoid using the event loop
BTo reduce memory usage
CTo simplify code
DTo use multiple CPU cores and handle more requests
✗ Incorrect
Multiple workers allow Node.js to use multiple CPU cores, improving performance.
What does the master process do if a worker crashes?
ASpawns a new worker to replace it
BDoes nothing
CRestarts the entire server
DLogs an error and stops
✗ Incorrect
The master process can detect worker crashes and create new workers to keep the app running.
Which of these is NOT a benefit of load balancing between workers?
AImproved app responsiveness
BBetter CPU usage
CSingle point of failure
DHigher reliability
✗ Incorrect
Load balancing reduces single points of failure by spreading work across workers.
Explain how the Node.js cluster module helps with load balancing between workers.
Think about how Node.js handles multiple requests using workers.
You got /5 concepts.
Describe what happens when a worker process crashes in a Node.js cluster setup and how the system recovers.
Consider fault tolerance and worker lifecycle.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of using the cluster module in Node.js for load balancing?
easy
A. To spread incoming requests across multiple CPU cores using worker processes
B. To create a single-threaded server that handles all requests
C. To manage database connections efficiently
D. To optimize memory usage by compressing data
Solution
Step 1: Understand the role of the cluster module
The cluster module allows Node.js to create multiple worker processes that share the same server port.
Step 2: Identify the purpose of load balancing
Load balancing means distributing incoming requests evenly across workers to use CPU cores efficiently.
Final Answer:
To spread incoming requests across multiple CPU cores using worker processes -> Option A
Quick Check:
Load balancing = spreading requests across workers [OK]
Hint: Cluster module creates workers to share server load [OK]
Common Mistakes:
Thinking cluster creates a single-threaded server
Confusing load balancing with database management
Assuming cluster compresses data for memory optimization
2. Which of the following is the correct way to check if the current process is the master in a Node.js cluster?
easy
A. if (cluster.isWorker) { /* master code */ }
B. if (cluster.master) { /* master code */ }
C. if (cluster.isMaster) { /* master code */ }
D. if (cluster.worker) { /* master code */ }
Solution
Step 1: Recall the cluster API properties
Node.js cluster module provides isMaster and isWorker boolean properties to identify process roles.
Step 2: Identify the correct property for master check
cluster.isMaster is true if the process is the master, so the condition should use this.
Final Answer:
if (cluster.isMaster) { /* master code */ } -> Option C
Quick Check:
Master check uses cluster.isMaster [OK]
Hint: Use cluster.isMaster to detect master process [OK]
A. The exit event listener is attached to the wrong object
B. The worker code does not handle errors properly
C. The cluster module is not imported correctly
D. The master does not fork a new worker after exit event
Solution
Step 1: Check the exit event handler
The master listens for 'exit' but only logs the death, it does not fork a new worker.
Step 2: Identify missing restart logic
To restart workers after crash, the master must call cluster.fork() inside the exit event handler.
Final Answer:
The master does not fork a new worker after exit event -> Option D
Quick Check:
Restart requires forking new worker on exit [OK]
Hint: Fork new worker inside exit event to restart [OK]
Common Mistakes:
Assuming worker error handling restarts process
Thinking cluster import affects restart
Believing exit event is attached incorrectly
5. You want to implement a Node.js cluster that balances load across all CPU cores and automatically restarts workers if they crash. Which code snippet correctly achieves this?
B. if (cluster.isMaster) {
const cpuCount = require('os').cpus().length;
for (let i = 0; i < cpuCount; i++) {
cluster.fork();
}
cluster.on('exit', (worker) => {
console.log(`Worker ${worker.process.pid} died, restarting...`);
cluster.fork();
});
} else {
require('http').createServer((req, res) => {
res.end(`Handled by worker ${process.pid}`);
}).listen(3000);
}
C. if (cluster.isWorker) {
const cpuCount = require('os').cpus().length;
for (let i = 0; i < cpuCount; i++) {
cluster.fork();
}
} else {
require('http').createServer((req, res) => {
res.end('Worker running');
}).listen(3000);
}
D. if (cluster.isMaster) {
const cpuCount = require('os').cpus().length;
for (let i = 0; i < cpuCount; i++) {
cluster.fork();
}
} else {
require('http').createServer((req, res) => {
res.end('Worker running');
}).listen(3000);
cluster.on('exit', () => {
cluster.fork();
});
}
Solution
Step 1: Fork workers equal to CPU cores in master
if (cluster.isMaster) {
const cpuCount = require('os').cpus().length;
for (let i = 0; i < cpuCount; i++) {
cluster.fork();
}
cluster.on('exit', (worker) => {
console.log(`Worker ${worker.process.pid} died, restarting...`);
cluster.fork();
});
} else {
require('http').createServer((req, res) => {
res.end(`Handled by worker ${process.pid}`);
}).listen(3000);
} correctly uses os.cpus().length to fork that many workers in the master process.
Step 2: Restart workers on exit event in master
if (cluster.isMaster) {
const cpuCount = require('os').cpus().length;
for (let i = 0; i < cpuCount; i++) {
cluster.fork();
}
cluster.on('exit', (worker) => {
console.log(`Worker ${worker.process.pid} died, restarting...`);
cluster.fork();
});
} else {
require('http').createServer((req, res) => {
res.end(`Handled by worker ${process.pid}`);
}).listen(3000);
} listens to the 'exit' event on cluster and forks a new worker to replace the dead one.
Step 3: Worker creates HTTP server listening on port 3000
if (cluster.isMaster) {
const cpuCount = require('os').cpus().length;
for (let i = 0; i < cpuCount; i++) {
cluster.fork();
}
cluster.on('exit', (worker) => {
console.log(`Worker ${worker.process.pid} died, restarting...`);
cluster.fork();
});
} else {
require('http').createServer((req, res) => {
res.end(`Handled by worker ${process.pid}`);
}).listen(3000);
}'s else block creates the server in workers, which is correct for load balancing.
Final Answer:
Forks workers across all CPU cores and automatically restarts workers if they crash -> Option B
Quick Check:
Fork all CPUs + restart on exit = if (cluster.isMaster) {
const cpuCount = require('os').cpus().length;
for (let i = 0; i < cpuCount; i++) {
cluster.fork();
}
cluster.on('exit', (worker) => {
console.log(`Worker ${worker.process.pid} died, restarting...`);
cluster.fork();
});
} else {
require('http').createServer((req, res) => {
res.end(`Handled by worker ${process.pid}`);
}).listen(3000);
} [OK]
Hint: Fork all CPUs in master and restart on exit event [OK]
Common Mistakes:
Not forking all CPU cores
Not restarting workers after crash
Forking workers inside worker process
Attaching exit listener inside worker instead of master