Challenge - 5 Problems
Cluster Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when forking workers equal to CPU cores?
Consider this Node.js code snippet that forks workers equal to the number of CPU cores. What will be logged to the console when the master process runs?
Node.js
import cluster from 'cluster'; import os from 'os'; if (cluster.isPrimary) { const cpuCount = os.cpus().length; console.log(`Master process running with ${cpuCount} CPUs`); for (let i = 0; i < cpuCount; i++) { cluster.fork(); } } else { console.log(`Worker ${process.pid} started`); }
Attempts:
2 left
💡 Hint
Remember that the master process logs first, then forks workers equal to CPU cores, each logging their start.
✗ Incorrect
The master process logs the number of CPUs, then forks one worker per CPU. Each worker logs its start message. So the console shows the master message followed by one message per worker.
📝 Syntax
intermediate2:00remaining
Which option correctly forks workers per CPU core?
Which code snippet correctly forks a worker process for each CPU core using Node.js cluster module?
Attempts:
2 left
💡 Hint
Check the correct property name for the primary process in Node.js cluster module in recent versions.
✗ Incorrect
In Node.js 20+, the cluster module uses 'isPrimary' instead of 'isMaster'. Also, ES module import syntax is preferred. Option B uses correct import and 'isPrimary'.
🔧 Debug
advanced2:00remaining
Why does this cluster code fork only one worker despite multiple CPUs?
Given this code, why does it fork only one worker even if the machine has multiple CPU cores?
import cluster from 'cluster';
import os from 'os';
if (cluster.isPrimary) {
cluster.fork();
} else {
console.log('Worker started');
}
Node.js
import cluster from 'cluster'; import os from 'os'; if (cluster.isPrimary) { cluster.fork(); } else { console.log('Worker started'); }
Attempts:
2 left
💡 Hint
Count how many times cluster.fork() is called in the master process.
✗ Incorrect
The code calls cluster.fork() only once inside the if block, so only one worker is created. To fork per CPU core, a loop over os.cpus().length is needed.
❓ state_output
advanced2:00remaining
What is the value of workers count after forking per CPU core?
In this code, what is the value of Object.keys(cluster.workers).length after the master forks workers equal to CPU cores?
Node.js
import cluster from 'cluster'; import os from 'os'; if (cluster.isPrimary) { const cpuCount = os.cpus().length; for (let i = 0; i < cpuCount; i++) { cluster.fork(); } console.log(Object.keys(cluster.workers).length); }
Attempts:
2 left
💡 Hint
cluster.workers holds all forked worker processes indexed by their ids.
✗ Incorrect
After forking once per CPU core, cluster.workers contains one entry per worker. So its keys length equals the CPU count.
🧠 Conceptual
expert2:00remaining
What happens if a worker crashes in a cluster with workers per CPU core?
In a Node.js cluster setup where one worker is forked per CPU core, what is the typical behavior when a worker process crashes unexpectedly?
Attempts:
2 left
💡 Hint
Think about how cluster module handles worker exits by default.
✗ Incorrect
The cluster module emits an 'exit' event on the master when a worker dies. The master can listen and fork a new worker to maintain the desired number.