0
0
Node.jsframework~10 mins

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

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a cluster that listens on port 3000.

Node.js
const cluster = require('cluster');
const http = require('http');

if (cluster.isMaster) {
  cluster.fork();
} else {
  http.createServer((req, res) => {
    res.writeHead(200);
    res.end('Hello from worker [1]');
  }).listen(3000);
}
Drag options to blanks, or click blank then click option'
Acluster.workerId
Bprocess.pid
Ccluster.id
Dworker.id
Attempts:
3 left
💡 Hint
Common Mistakes
Using cluster.id which does not exist
Using worker.id which is undefined in this context
2fill in blank
medium

Complete the code to create a worker thread that runs a function.

Node.js
const { Worker } = require('worker_threads');

const worker = new Worker(`
  const { parentPort } = require('worker_threads');
  parentPort.postMessage('Hello from [1]');
`, { eval: true });
Drag options to blanks, or click blank then click option'
Achild process
Bmain thread
Ccluster worker
Dworker thread
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing main thread with worker thread
Using cluster worker which is a different concept
3fill in blank
hard

Fix the error in the cluster setup to properly fork workers equal to CPU cores.

Node.js
const cluster = require('cluster');
const os = require('os');

if (cluster.isMaster) {
  const cpuCount = os.[1]().length;
  for (let i = 0; i < cpuCount; i++) {
    cluster.fork();
  }
}
Drag options to blanks, or click blank then click option'
Acpus
BcpuCores
CcpuCount
Dcpu
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like cpuCount or cpuCores
Trying to use a property instead of a method
4fill in blank
hard

Fill both blanks to create a worker thread that sends a message back to the main thread.

Node.js
const { Worker, isMainThread, parentPort } = require('worker_threads');

if (isMainThread) {
  const worker = new Worker(__filename);
  worker.on('message', (msg) => {
    console.log('Received:', msg);
  });
} else {
  parentPort.[1]('Hello from worker');
  parentPort.[2]('close', () => process.exit());
}
Drag options to blanks, or click blank then click option'
ApostMessage
Bsend
Con
Dexit
Attempts:
3 left
💡 Hint
Common Mistakes
Using send instead of postMessage
Using exit instead of on for event listening
5fill in blank
hard

Fill all three blanks to create a cluster that restarts a worker when it exits.

Node.js
const cluster = require('cluster');
const http = require('http');

if (cluster.isMaster) {
  cluster.fork();
  cluster.on('[1]', (worker, code, signal) => {
    console.log(`Worker [2] died. Restarting...`);
    cluster.[3]();
  });
} else {
  http.createServer((req, res) => {
    res.end('Hello from worker');
  }).listen(3000);
}
Drag options to blanks, or click blank then click option'
Aexit
Bid
Cfork
Ddisconnect
Attempts:
3 left
💡 Hint
Common Mistakes
Listening for 'disconnect' instead of 'exit'
Using worker.processId instead of worker.id
Calling cluster.disconnect() instead of cluster.fork()