0
0
Node.jsframework~20 mins

How cluster module works in Node.js - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cluster Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a worker process dies in Node.js cluster?
Consider a Node.js cluster setup where the master forks several worker processes. What is the default behavior when one worker process unexpectedly exits?
AThe master restarts the entire cluster including all workers.
BThe master shuts down all other workers and exits.
CThe master ignores the dead worker and does not replace it.
DThe master automatically forks a new worker to replace the dead one.
Attempts:
2 left
💡 Hint
Think about how Node.js cluster maintains availability by managing worker processes.
📝 Syntax
intermediate
2:00remaining
Identify the correct way to create a cluster master and fork workers
Which code snippet correctly creates a cluster master that forks 2 worker processes?
A
const cluster = require('cluster');
if (cluster.isWorker) {
  cluster.fork();
  cluster.fork();
} else {
  console.log('Master running');
}
B
const cluster = require('cluster');
if (cluster.isMaster) {
  cluster.fork();
  cluster.fork();
} else {
  console.log('Worker running');
}
C
const cluster = require('cluster');
if (cluster.isMaster) {
  cluster.start(2);
} else {
  console.log('Worker running');
}
D
const cluster = require('cluster');
if (cluster.isMaster) {
  cluster.createWorkers(2);
} else {
  console.log('Worker running');
}
Attempts:
2 left
💡 Hint
Check the official cluster API properties and methods for forking workers.
🔧 Debug
advanced
2:00remaining
Why does this cluster code cause workers not to start?
Given the code below, why do no worker processes start? const cluster = require('cluster'); if (cluster.isMaster) { for (let i = 0; i < 2; i++) { cluster.fork; } } else { console.log('Worker started'); }
Node.js
const cluster = require('cluster');

if (cluster.isMaster) {
  for (let i = 0; i < 2; i++) {
    cluster.fork;
  }
} else {
  console.log('Worker started');
}
Acluster.isMaster is deprecated and should be replaced with cluster.isPrimary.
BThe else block should be inside the for loop to start workers.
Ccluster.fork is missing parentheses, so the function is not called.
DThe cluster module requires an explicit call to cluster.start() to begin.
Attempts:
2 left
💡 Hint
Look carefully at how functions are called in JavaScript.
state_output
advanced
2:00remaining
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) { console.log('Master process'); cluster.fork(); cluster.fork(); console.log('Workers count:', Object.keys(cluster.workers).length); } else { console.log('Worker process'); }
Node.js
const cluster = require('cluster');

if (cluster.isMaster) {
  console.log('Master process');
  cluster.fork();
  cluster.fork();
  console.log('Workers count:', Object.keys(cluster.workers).length);
} else {
  console.log('Worker process');
}
A
Master process
Workers count: 0
Worker process
Worker process
B
Master process
Worker process
Worker process
Workers count: 2
C
Master process
Workers count: 2
Worker process
Worker process
D
Master process
Workers count: 2
Attempts:
2 left
💡 Hint
Consider when the workers are fully registered in the cluster.workers object.
🧠 Conceptual
expert
2:00remaining
How does Node.js cluster module distribute incoming connections?
In a Node.js cluster with multiple worker processes, how are incoming TCP connections distributed among workers by default?
AThe master process uses a round-robin algorithm to distribute connections evenly to workers.
BEach worker listens on the same port independently and the OS load balances connections.
CThe first worker accepts all connections until it crashes, then the next worker takes over.
DConnections are distributed randomly without any specific order or balancing.
Attempts:
2 left
💡 Hint
Think about how Node.js cluster manages load balancing internally.