Challenge - 5 Problems
Load Balancing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
How does Node.js cluster module distribute incoming connections by default?
Consider a Node.js server using the cluster module with multiple worker processes. How are incoming connections distributed among workers by default?
Attempts:
2 left
💡 Hint
Think about how the cluster module balances load to avoid overloading a single worker.
✗ Incorrect
By default, Node.js cluster module uses a round-robin approach in the master process to distribute incoming connections among workers.
📝 Syntax
intermediate1:30remaining
Identify the correct way to create a worker in Node.js cluster
Which code snippet correctly creates a worker process using Node.js cluster module?
Node.js
const cluster = require('cluster'); if (cluster.isMaster) { // create worker }
Attempts:
2 left
💡 Hint
Check the official cluster module API for the method to create a worker.
✗ Incorrect
The correct method to create a worker is cluster.fork(). Other methods do not exist in the cluster API.
🔧 Debug
advanced2:30remaining
Why does this cluster setup cause uneven load distribution?
Given the code below, why might the load not be balanced evenly across workers?
const cluster = require('cluster');
const http = require('http');
if (cluster.isMaster) {
for (let i = 0; i < 4; i++) {
cluster.fork();
}
} else {
http.createServer((req, res) => {
// simulate heavy work
const start = Date.now();
while (Date.now() - start < 100) {}
res.end('done');
}).listen(8000);
}
Attempts:
2 left
💡 Hint
Consider how blocking code affects worker availability and load distribution.
✗ Incorrect
The blocking while loop in each worker delays response handling, causing some workers to be busy longer. This leads to uneven load because requests queue up on busy workers.
❓ state_output
advanced1:30remaining
What is the output of this cluster worker count code?
What will be printed when running this code?
const cluster = require('cluster');
if (cluster.isMaster) {
cluster.fork();
cluster.fork();
console.log(Object.keys(cluster.workers).length);
} else {
console.log('worker running');
}
Attempts:
2 left
💡 Hint
Remember that cluster.workers is an object with worker IDs as keys.
✗ Incorrect
The master process forks two workers, so cluster.workers has two keys. The length of these keys is 2.
🧠 Conceptual
expert3:00remaining
Why might you choose a custom load balancing strategy over Node.js default cluster behavior?
Which reason best explains why a developer might implement a custom load balancing strategy instead of relying on Node.js cluster's default round-robin distribution?
Attempts:
2 left
💡 Hint
Think about real-world scenarios where some workers might be busier or slower than others.
✗ Incorrect
Custom load balancing can improve performance by sending requests to the least busy workers, optimizing CPU and memory usage beyond simple round-robin.