Node.js - Cluster ModuleYou want to create a cluster that automatically restarts a worker if it crashes. Which approach correctly implements this behavior?AListen to the 'exit' event on cluster and fork a new worker inside the handlerBUse setInterval to fork new workers every secondCCall cluster.fork() only once at startup and never againDUse cluster.disconnect() inside the worker to restart itselfCheck Answer
Step-by-Step SolutionSolution:Step 1: Understand worker crash handlingThe primary process can listen to the 'exit' event when a worker dies.Step 2: Restart worker on exitInside the 'exit' event handler, calling cluster.fork() creates a new worker to replace the crashed one.Final Answer:Listen to the 'exit' event on cluster and fork a new worker inside the handler -> Option AQuick Check:Restart crashed workers by handling 'exit' event [OK]Quick Trick: Use 'exit' event to restart workers automatically [OK]Common Mistakes:Forking workers repeatedly with setInterval causes overloadNot restarting workers after crash leads to downtimeUsing cluster.disconnect() inside worker does not restart it
Master "Cluster Module" in Node.js9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallPerf
More Node.js Quizzes Child Processes - fork for Node.js child processes - Quiz 6medium Cluster Module - Load balancing between workers - Quiz 5medium Debugging and Profiling - Why debugging skills matter - Quiz 3easy Error Handling Patterns - Why robust error handling matters - Quiz 4medium Error Handling Patterns - Custom error classes - Quiz 11easy HTTP Module - Why building HTTP servers matters - Quiz 12easy HTTP Module - Serving static files - Quiz 12easy HTTP Module - Parsing request body (JSON and form data) - Quiz 2easy URL and Query String Handling - Relative vs absolute URL resolution - Quiz 9hard Worker Threads - Creating worker threads - Quiz 1easy