0
0
Node.jsframework~20 mins

When to use workers vs cluster in Node.js - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Node.js Workers vs Cluster Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding the main difference between workers and cluster in Node.js
Which statement best describes the primary difference between Node.js workers and cluster modules?
ACluster runs code in multiple threads sharing the same event loop, workers create separate processes.
BWorkers run in separate threads sharing memory, while cluster creates separate processes with independent memory.
CWorkers and cluster both create separate processes but cluster shares memory between them.
DCluster uses threads inside a single process, workers spawn multiple processes.
Attempts:
2 left
💡 Hint
Think about how memory is shared or isolated between threads and processes.
component_behavior
intermediate
2:00remaining
Choosing between workers and cluster for CPU-intensive tasks
You want to perform CPU-heavy calculations without blocking the main Node.js event loop. Which approach is best?
AUse cluster to spawn multiple processes, each handling a part of the calculation.
BUse cluster to run calculations in threads sharing memory to reduce overhead.
CUse a single process with asynchronous callbacks to handle calculations.
DUse workers to run calculations in separate threads without blocking the main thread.
Attempts:
2 left
💡 Hint
Consider which method allows parallel execution without blocking the main event loop.
🔧 Debug
advanced
2:00remaining
Debugging memory usage with cluster vs workers
You notice high memory usage in your Node.js app using cluster. Which cause is most likely?
Node.js
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) => {
    res.end('Hello World');
  }).listen(8000);
}
AEach cluster worker is a separate process with its own memory, increasing total memory use.
BCluster workers share memory, so memory usage should be low and shared.
CCluster uses threads that share memory, causing memory leaks if not handled.
DCluster automatically frees memory after each request, so memory usage is unrelated.
Attempts:
2 left
💡 Hint
Think about how processes handle memory separately.
📝 Syntax
advanced
2:00remaining
Correct syntax to create a worker thread in Node.js
Which code snippet correctly creates a worker thread in Node.js?
A
const { Worker } = require('worker_threads');
const worker = new Worker('./worker.js');
B
const cluster = require('cluster');
const worker = cluster.fork('./worker.js');
C
const { Worker } = require('worker_threads');
const worker = Worker('./worker.js');
Dconst worker = new WorkerThreads('./worker.js');
Attempts:
2 left
💡 Hint
Check the correct import and constructor usage for worker threads.
state_output
expert
3:00remaining
Output behavior when using cluster with shared server port
Given the following code, what will be the output when multiple cluster workers listen on the same port?
Node.js
const cluster = require('cluster');
const http = require('http');

if (cluster.isMaster) {
  cluster.fork();
  cluster.fork();
} else {
  http.createServer((req, res) => {
    res.end(`Handled by worker ${process.pid}`);
  }).listen(3000);
  console.log(`Worker ${process.pid} started`);
}
AOnly one worker starts successfully; the other throws an error because the port is already in use.
BBoth workers start but only the first handles requests; the second is idle.
CBoth workers start and share the port; requests are load balanced between them, each responding with its own PID.
DThe master process handles requests and workers do not start.
Attempts:
2 left
💡 Hint
Consider how Node.js cluster module manages server ports across workers.