Bird
Raised Fist0
Node.jsframework~20 mins

Master and worker processes in Node.js - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
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 does this Node.js cluster master process code output?
Consider this Node.js code using the cluster module. What will be printed to the console when you run it?
Node.js
import cluster from 'cluster';
import os from 'os';

if (cluster.isPrimary) {
  console.log('Master process is running');
  const worker = cluster.fork();
  worker.on('online', () => {
    console.log('Worker is online');
  });
} else {
  console.log('Worker process started');
}
AMaster process is running\nWorker is online\nWorker process started
BWorker process started\nMaster process is running\nWorker is online
CMaster process is running\nWorker process started\nWorker is online
DWorker is online\nMaster process is running\nWorker process started
Attempts:
2 left
💡 Hint
Remember that the master process runs first and forks the worker. The worker prints its message after it starts.
📝 Syntax
intermediate
1:30remaining
Which option correctly creates a worker process using Node.js cluster?
Select the code snippet that correctly forks a worker process using the cluster module in Node.js.
Aconst worker = cluster.spawn();
Bconst worker = cluster.fork();
Cconst worker = cluster.createWorker();
Dconst worker = cluster.newWorker();
Attempts:
2 left
💡 Hint
Check the official cluster module API for the method to create a worker.
🔧 Debug
advanced
2:30remaining
Why does this cluster worker never start?
Given this code, the worker process never logs 'Worker started'. What is the cause?
Node.js
import cluster from 'cluster';

if (cluster.isPrimary) {
  cluster.fork();
} else if (cluster.isWorker) {
  console.log('Worker started');
}
AThe worker code runs only if 'cluster.isWorker' is true, but the correct property is 'cluster.isWorker'.
BThe property 'cluster.isWorker' does not exist; it should be 'cluster.isWorkerProcess'.
CThe worker code is unreachable because 'cluster.isPrimary' is always true.
DThe condition should be 'if (cluster.isWorker)' instead of 'else if'.
Attempts:
2 left
💡 Hint
Check the cluster module properties for identifying master and worker processes.
state_output
advanced
2:00remaining
What is the value of 'workerCount' after this code runs?
This code counts active workers. What is the value of 'workerCount' after execution?
Node.js
import cluster from 'cluster';

let workerCount = 0;

if (cluster.isPrimary) {
  cluster.fork();
  cluster.fork();
  workerCount = Object.keys(cluster.workers).length;
} else {
  // worker code
}
A2
B0
C1
Dundefined
Attempts:
2 left
💡 Hint
cluster.workers is an object with keys for each worker process.
🧠 Conceptual
expert
3:00remaining
Which statement about Node.js cluster master and worker processes is true?
Select the correct statement about how master and worker processes communicate in Node.js cluster.
AWorkers automatically inherit all open network connections from the master without explicit code.
BMaster and workers communicate only through shared memory variables.
CWorkers can send messages to the master using process.send(), and master listens with worker.on('message').
DMaster and workers share the same event loop and memory space.
Attempts:
2 left
💡 Hint
Think about how separate processes exchange data in Node.js cluster.

Practice

(1/5)
1. What is the main role of the master process in Node.js cluster module?
easy
A. To create and manage worker processes
B. To handle HTTP requests directly
C. To run the application code
D. To listen on network ports

Solution

  1. Step 1: Understand the master process role

    The master process is responsible for creating and managing worker processes in the cluster module.
  2. Step 2: Differentiate master from worker tasks

    Workers run the app code and handle requests, while the master only manages them.
  3. Final Answer:

    To create and manage worker processes -> Option A
  4. Quick Check:

    Master manages workers [OK]
Hint: Master only manages workers, does not run app code [OK]
Common Mistakes:
  • Thinking master handles requests directly
  • Confusing master with worker process
  • Assuming master runs app code
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 (process.isWorker) { ... }
B. if (cluster.isWorker) { ... }
C. if (process.isMaster) { ... }
D. if (cluster.isMaster) { ... }

Solution

  1. Step 1: Recall cluster module properties

    The cluster module provides isMaster and isWorker boolean properties to identify process roles.
  2. Step 2: Identify correct syntax

    To check if current process is master, use cluster.isMaster, not process properties.
  3. Final Answer:

    if (cluster.isMaster) { ... } -> Option D
  4. Quick Check:

    Use cluster.isMaster to check master process [OK]
Hint: Use cluster.isMaster, not process properties [OK]
Common Mistakes:
  • Using process.isMaster which does not exist
  • Confusing isWorker with isMaster
  • Using wrong object for the check
3. Consider this Node.js cluster code snippet:
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?
medium
A. You get response 'Worker <pid>' from one of the two workers
B. You get response 'Worker <pid>' from the master process
C. The server crashes because master listens on port
D. No response because no server is created

Solution

  1. Step 1: Identify which process creates the server

    The else block runs in worker processes, which create the HTTP server listening on port 8000.
  2. 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.
  3. Final Answer:

    You get response 'Worker <pid>' from one of the two workers -> Option A
  4. Quick Check:

    Workers handle requests, master does not listen [OK]
Hint: Only workers create servers and respond [OK]
Common Mistakes:
  • Thinking master handles requests
  • Assuming master listens on port
  • Believing no server is created
4. Given this code snippet:
const cluster = require('cluster');
if (cluster.isMaster) {
  cluster.fork();
  cluster.fork();
} else {
  console.log('Worker started');
}

Why might the workers never start properly?
medium
A. Because the master process forgot to require('http')
B. Because the workers have no code to keep them alive
C. Because cluster.fork() is called inside the else block
D. Because cluster.isMaster is always false

Solution

  1. 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.
  2. Step 2: Understand cluster.fork usage

    cluster.fork() is correctly called in master block, so workers start but exit quickly.
  3. Final Answer:

    Because the workers have no code to keep them alive -> Option B
  4. Quick Check:

    Workers exit if no server or event loop runs [OK]
Hint: Workers need active code (like server) to stay alive [OK]
Common Mistakes:
  • Thinking missing http require stops workers
  • Confusing cluster.fork placement
  • Assuming cluster.isMaster is always false
5. You want to create a Node.js cluster that uses all CPU cores and restarts any worker that crashes. Which approach correctly implements this?
hard
A. Create workers manually without cluster module and restart them with setInterval
B. Use cluster.fork() once and rely on master to restart automatically
C. Use cluster.fork() for each CPU core and listen to 'exit' event to fork a new worker
D. Use cluster.isWorker to fork new workers inside each worker process

Solution

  1. Step 1: Use all CPU cores with cluster.fork()

    Fork one worker per CPU core by looping over the number of CPUs.
  2. 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.
  3. Final Answer:

    Use cluster.fork() for each CPU core and listen to 'exit' event to fork a new worker -> Option C
  4. Quick Check:

    Fork per CPU + restart on exit [OK]
Hint: Fork per CPU and restart on 'exit' event [OK]
Common Mistakes:
  • Forking only once and expecting auto-restart
  • Restarting workers inside workers themselves
  • Not handling worker crashes properly