Bird
0
0

What happens if a worker process crashes in a Node.js cluster setup with the following code?

medium📝 component behavior Q5 of 15
Node.js - Cluster Module
What happens if a worker process crashes in a Node.js cluster setup with the following code?
const cluster = require('cluster');
if (cluster.isPrimary) {
  cluster.fork();
  cluster.on('exit', (worker) => {
    console.log(`Worker ${worker.process.pid} died`);
    cluster.fork();
  });
}
ANo new worker is created after crash
BA new worker is automatically forked to replace the crashed one
CThe cluster stops all workers and exits
DThe primary process crashes as well
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the exit event handler

    The code listens for 'exit' events on workers and forks a new worker when one dies.
  2. Step 2: Understand cluster fault tolerance

    This pattern ensures the cluster keeps the number of workers constant by replacing crashed ones.
  3. Final Answer:

    A new worker is automatically forked to replace the crashed one -> Option B
  4. Quick Check:

    Worker crash = new worker forked [OK]
Quick Trick: Use exit event to respawn workers [OK]
Common Mistakes:
  • Assuming cluster stops on worker crash
  • Thinking no replacement worker is created
  • Believing primary crashes with worker

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes