Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Master and Worker Processes in Node.js
📖 Scenario: You are building a simple Node.js application that uses multiple processes to handle tasks efficiently. The main process (master) will create worker processes to perform work in parallel.
🎯 Goal: Create a Node.js script that sets up a master process and two worker processes using the cluster module. The master will send a message to each worker, and each worker will respond back.
📋 What You'll Learn
Use the cluster module to create worker processes
Create exactly two worker processes
Master process sends a message to each worker
Each worker sends a message back to the master
Use process.on('message') to handle messages
💡 Why This Matters
🌍 Real World
Using master and worker processes helps build fast and scalable Node.js servers that can handle many tasks at once by using multiple CPU cores.
💼 Career
Understanding process management and inter-process communication is important for backend developers working with Node.js in production environments.
Progress0 / 4 steps
1
Setup the cluster module and check if current process is master
Write code to import the cluster module and the os module. Then create a condition that checks if cluster.isMaster is true.
Node.js
Hint
Use require('cluster') and require('os'). Then check cluster.isMaster to know if the current process is the master.
2
Create two worker processes inside the master block
Inside the if (cluster.isMaster) block, write code to create exactly two worker processes using cluster.fork() twice.
Node.js
Hint
Call cluster.fork() two times inside the master block to create two workers.
3
Send a message from master to each worker and listen for messages from workers
Inside the if (cluster.isMaster) block, add a for loop that iterates over Object.values(cluster.workers). For each worker, send a message { cmd: 'start' } using worker.send(). Also, add a listener on each worker for 'message' events that logs the received message with console.log.
Node.js
Hint
Use Object.values(cluster.workers) to get all workers. Send a message with worker.send() and listen with worker.on('message').
4
Add worker process code to receive message and reply to master
Outside the if (cluster.isMaster) block, write code for the worker processes. Use process.on('message') to listen for messages. When a message with cmd === 'start' is received, send back a message to the master with process.send({ reply: 'Worker is ready' }).
Node.js
Hint
Use else for worker code. Listen with process.on('message') and reply with process.send().
Practice
(1/5)
1. What is the main role of the master process in Node.js cluster module?
easy
A. To create and manage worker processes
B. To handle HTTP requests directly
C. To run the application code
D. To listen on network ports
Solution
Step 1: Understand the master process role
The master process is responsible for creating and managing worker processes in the cluster module.
Step 2: Differentiate master from worker tasks
Workers run the app code and handle requests, while the master only manages them.
Final Answer:
To create and manage worker processes -> Option A
Quick Check:
Master manages workers [OK]
Hint: Master only manages workers, does not run app code [OK]
Common Mistakes:
Thinking master handles requests directly
Confusing master with worker process
Assuming master runs app code
2. Which of the following is the correct way to check if the current process is the master in a Node.js cluster?
easy
A. if (process.isWorker) { ... }
B. if (cluster.isWorker) { ... }
C. if (process.isMaster) { ... }
D. if (cluster.isMaster) { ... }
Solution
Step 1: Recall cluster module properties
The cluster module provides isMaster and isWorker boolean properties to identify process roles.
Step 2: Identify correct syntax
To check if current process is master, use cluster.isMaster, not process properties.
Final Answer:
if (cluster.isMaster) { ... } -> Option D
Quick Check:
Use cluster.isMaster to check master process [OK]
Hint: Use cluster.isMaster, not process properties [OK]