Bird
Raised Fist0
Node.jsframework~10 mins

How cluster module works in Node.js - Interactive Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the cluster module in Node.js.

Node.js
const cluster = require('[1]');
Drag options to blanks, or click blank then click option'
Anet
Bhttp
Cfs
Dcluster
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'http' instead of 'cluster' to import the cluster module.
Forgetting to use require() to import the module.
2fill in blank
medium

Complete the code to check if the current process is the master in a cluster.

Node.js
if (cluster.[1]) {
  console.log('This is the master process');
}
Drag options to blanks, or click blank then click option'
AisPrimary
BisMaster
CisWorker
DisMain
Attempts:
3 left
💡 Hint
Common Mistakes
Using isMaster which is deprecated in newer Node.js versions.
Using isWorker which is the opposite of master.
3fill in blank
hard

Fix the error in the code to fork a worker process.

Node.js
const worker = cluster.[1]();
Drag options to blanks, or click blank then click option'
Aspawn
Bstart
Cfork
Dcreate
Attempts:
3 left
💡 Hint
Common Mistakes
Using start() which does not exist in cluster.
Using spawn() which is from child_process module, not cluster.
4fill in blank
hard

Fill both blanks to create a simple HTTP server in a worker process.

Node.js
if (cluster.isPrimary) {
  cluster.[1]();
} else {
  const http = require('http');
  http.createServer((req, res) => {
    res.writeHead(200);
    res.end(`Hello from worker ${cluster.worker.[2]`);
  }).listen(8000);
}
Drag options to blanks, or click blank then click option'
Afork
BisMaster
Cid
DisWorker
Attempts:
3 left
💡 Hint
Common Mistakes
Using isMaster instead of fork() to create workers.
Using isWorker instead of id to identify workers.
5fill in blank
hard

Fill all three blanks to handle worker exit and restart a new worker.

Node.js
cluster.on('exit', (worker, code, signal) => {
  console.log(`Worker ${worker.[1] died with code ${code}`);
  console.log('Starting a new worker');
  cluster.{{BLANK_3}}();
});
Drag options to blanks, or click blank then click option'
Aid
Bfork
Ccode
Dpid
Attempts:
3 left
💡 Hint
Common Mistakes
Using pid instead of id for worker identification.
Using signal instead of code for exit code.
Not calling fork() to restart a worker.

Practice

(1/5)
1. What is the main purpose of the cluster module in Node.js?
easy
A. To provide a graphical user interface for Node.js apps
B. To manage database connections efficiently
C. To handle file system operations asynchronously
D. To create multiple worker processes to use all CPU cores

Solution

  1. Step 1: Understand the cluster module role

    The cluster module allows Node.js to create multiple worker processes.
  2. Step 2: Recognize the benefit

    These workers use all CPU cores to improve performance by handling requests in parallel.
  3. Final Answer:

    To create multiple worker processes to use all CPU cores -> Option D
  4. Quick Check:

    cluster module = multiple workers for CPU cores [OK]
Hint: Cluster = multiple processes for CPU cores [OK]
Common Mistakes:
  • Confusing cluster with database or file system modules
  • Thinking cluster manages UI or frontend
  • Assuming cluster runs code in a single process
2. Which of the following is the correct way to check if the current process is the master in a cluster setup?
easy
A. if (cluster.isMaster) { ... }
B. if (cluster.isPrimary) { ... }
C. if (cluster.isWorker) { ... }
D. if (cluster.isMain) { ... }

Solution

  1. Step 1: Recall the updated property name

    In recent Node.js versions, cluster.isPrimary replaces cluster.isMaster.
  2. Step 2: Identify the correct syntax

    Using cluster.isPrimary correctly checks if the process is the primary (master) process.
  3. Final Answer:

    if (cluster.isPrimary) { ... } -> Option B
  4. Quick Check:

    Primary process check = cluster.isPrimary [OK]
Hint: Use cluster.isPrimary, not isMaster [OK]
Common Mistakes:
  • Using deprecated cluster.isMaster instead of cluster.isPrimary
  • Confusing isWorker with isPrimary
  • Using non-existent properties like isMain
3. Consider this Node.js cluster code snippet:
const cluster = require('cluster');
const http = require('http');

if (cluster.isPrimary) {
  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 multiple times?
medium
A. You will see responses from different worker process IDs
B. Only one worker will handle all requests
C. The server will crash because of multiple forks
D. You will get a syntax error on startup

Solution

  1. Step 1: Understand cluster.fork creates workers

    Two workers are created, each running the HTTP server on port 8000.
  2. Step 2: Recognize load balancing behavior

    Requests are distributed between workers, so responses show different process IDs.
  3. Final Answer:

    You will see responses from different worker process IDs -> Option A
  4. Quick Check:

    Multiple workers share port, respond with different PIDs [OK]
Hint: Multiple forks = multiple workers respond differently [OK]
Common Mistakes:
  • Thinking only one worker handles all requests
  • Assuming server crashes due to multiple forks
  • Expecting syntax errors from this code
4. What is wrong with this cluster code snippet?
const cluster = require('cluster');
if (cluster.isPrimary) {
  cluster.fork();
} else {
  console.log('Worker running');
}
medium
A. cluster.isPrimary is deprecated, should use isMaster
B. No call to cluster.fork in the worker process
C. Missing server code inside worker
D. No error, code works fine

Solution

  1. Step 1: Check cluster usage

    The primary forks one worker, which only logs a message but does not start a server.
  2. Step 2: Identify missing functionality

    Without server code, the worker does not handle requests, so the cluster setup is incomplete.
  3. Final Answer:

    Missing server code inside worker -> Option C
  4. Quick Check:

    Worker must run server code to handle requests [OK]
Hint: Workers need server code to handle requests [OK]
Common Mistakes:
  • Confusing isPrimary with deprecated isMaster
  • Expecting cluster.fork in worker process
  • Assuming code runs without server in worker
5. You want to create a cluster that automatically restarts a worker if it crashes. Which approach correctly implements this behavior?
hard
A. Listen to the 'exit' event on cluster and fork a new worker inside the handler
B. Use setInterval to fork new workers every second
C. Call cluster.fork() only once at startup and never again
D. Use cluster.disconnect() inside the worker to restart itself

Solution

  1. Step 1: Understand worker crash handling

    The primary process can listen to the 'exit' event when a worker dies.
  2. Step 2: Restart worker on exit

    Inside the 'exit' event handler, calling cluster.fork() creates a new worker to replace the crashed one.
  3. Final Answer:

    Listen to the 'exit' event on cluster and fork a new worker inside the handler -> Option A
  4. Quick Check:

    Restart crashed workers by handling 'exit' event [OK]
Hint: Use 'exit' event to restart workers automatically [OK]
Common Mistakes:
  • Forking workers repeatedly with setInterval causes overload
  • Not restarting workers after crash leads to downtime
  • Using cluster.disconnect() inside worker does not restart it