0
0
Node.jsframework~20 mins

Handling worker crashes and restart in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Node.js Cluster Crash Handler
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a worker crashes in this Node.js cluster code?

Consider this Node.js cluster code snippet. What will be the output behavior when a worker process crashes?

Node.js
import cluster from 'cluster';
import os from 'os';

if (cluster.isPrimary) {
  const numCPUs = os.cpus().length;
  for (let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('exit', (worker, code, signal) => {
    console.log(`Worker ${worker.process.pid} died.`);
  });
} else {
  process.exit(1); // Worker crashes immediately
}
AThe primary ignores worker crashes and no logs are shown.
BThe primary automatically restarts each worker after it crashes.
CThe primary logs each worker's death but does not restart any workers.
DThe primary crashes as well when any worker crashes.
Attempts:
2 left
💡 Hint

Look at the cluster event listeners and what happens on 'exit'.

📝 Syntax
intermediate
2:00remaining
Which option correctly restarts a worker after it crashes?

Given this cluster setup, which code snippet correctly restarts a worker when it crashes?

Node.js
import cluster from 'cluster';
import os from 'os';

if (cluster.isPrimary) {
  const numCPUs = os.cpus().length;
  for (let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('exit', (worker, code, signal) => {
    // Restart worker here
  });
} else {
  // Worker code
}
Acluster.on('exit', (worker) => { cluster.fork(); });
Bcluster.on('exit', (worker) => { worker.fork(); });
Ccluster.on('exit', () => { cluster.restart(); });
Dcluster.on('exit', (worker) => { cluster.fork(worker.id); });
Attempts:
2 left
💡 Hint

Remember how to create new workers in Node.js cluster.

🔧 Debug
advanced
2:00remaining
Why does this cluster code fail to restart workers properly?

Examine this code snippet. Why does it fail to restart workers after they crash?

Node.js
import cluster from 'cluster';
import os from 'os';

if (cluster.isPrimary) {
  const numCPUs = os.cpus().length;
  for (let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('exit', (worker, code, signal) => {
    console.log(`Worker ${worker.process.pid} died.`);
    cluster.fork(worker.id);
  });
} else {
  process.exit(1);
}
Acluster.fork() does not accept any arguments, so passing worker.id causes an error and no restart.
BThe 'exit' event is not emitted on worker crashes, so the handler never runs.
CThe worker process exits with code 1, which prevents cluster from restarting it.
DThe code forks too many workers causing resource exhaustion.
Attempts:
2 left
💡 Hint

Check the cluster.fork() method signature.

state_output
advanced
2:00remaining
What is the number of active workers after a crash and restart?

Given this cluster code, how many worker processes will be active after one worker crashes and the restart code runs?

Node.js
import cluster from 'cluster';
import os from 'os';

if (cluster.isPrimary) {
  const numCPUs = os.cpus().length;
  for (let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('exit', (worker, code, signal) => {
    console.log(`Worker ${worker.process.pid} died.`);
    cluster.fork();
  });
} else {
  if (process.env.CRASH) process.exit(1);
  else setTimeout(() => {}, 10000);
}
ANo workers remain active after the crash.
BThe number of active workers decreases by one and never recovers.
CThe number of active workers doubles after the restart.
DThe number of active workers remains equal to the number of CPUs.
Attempts:
2 left
💡 Hint

Think about how cluster.fork() replaces crashed workers.

🧠 Conceptual
expert
2:00remaining
Why is it important to handle worker crashes and restart in Node.js clusters?

Choose the best explanation for why managing worker crashes and restarts is critical in Node.js cluster applications.

ATo reduce CPU usage by stopping workers that crash frequently.
BTo ensure the application remains available and responsive by replacing failed workers automatically.
CTo prevent the primary process from using too much memory by restarting workers manually.
DTo allow workers to share memory directly without restarting.
Attempts:
2 left
💡 Hint

Think about what happens if workers crash and are not restarted.