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
How cluster module works
📖 Scenario: You want to create a simple Node.js server that uses the cluster module to run multiple worker processes. This helps your server handle more requests by using all CPU cores.
🎯 Goal: Build a Node.js script that uses the cluster module to create worker processes equal to the number of CPU cores. Each worker runs a simple HTTP server that responds with 'Hello from worker'.
📋 What You'll Learn
Create a variable to import the cluster module
Create a variable to import the http module
Create a variable to import the os module
Use cluster.isMaster to check if the current process is the master
In the master process, fork workers equal to the number of CPU cores
In each worker process, create an HTTP server that responds with 'Hello from worker'
Listen on port 8000 in each worker
💡 Why This Matters
🌍 Real World
Using the cluster module helps Node.js servers use all CPU cores, improving performance and handling more users at the same time.
💼 Career
Understanding clustering is important for backend developers to build scalable and efficient Node.js applications.
Progress0 / 4 steps
1
Import required modules
Create three variables called cluster, http, and os. Import the Node.js modules cluster, http, and os respectively using require.
Node.js
Hint
Use const cluster = require('cluster'); to import the cluster module.
2
Check if current process is master
Add an if statement that checks if cluster.isMaster is true. This will help you run code only in the master process.
Node.js
Hint
Use if (cluster.isMaster) { } to check the master process.
3
Fork workers for each CPU core
Inside the if (cluster.isMaster) block, create a for loop that runs from 0 to os.cpus().length. Inside the loop, call cluster.fork() to create worker processes.
Node.js
Hint
Use for (let i = 0; i < os.cpus().length; i++) { cluster.fork(); } inside the master block.
4
Create HTTP server in worker processes
Add an else block after the if (cluster.isMaster). Inside the else, create an HTTP server using http.createServer() that responds with 'Hello from worker'. Make the server listen on port 8000.
Node.js
Hint
Use http.createServer((req, res) => { res.end('Hello from worker'); }).listen(8000); inside the else block.
Practice
(1/5)
1. What is the main purpose of the cluster module in Node.js?
easy
A. To provide a graphical user interface for Node.js apps
B. To manage database connections efficiently
C. To handle file system operations asynchronously
D. To create multiple worker processes to use all CPU cores
Solution
Step 1: Understand the cluster module role
The cluster module allows Node.js to create multiple worker processes.
Step 2: Recognize the benefit
These workers use all CPU cores to improve performance by handling requests in parallel.
Final Answer:
To create multiple worker processes to use all CPU cores -> Option D
Quick Check:
cluster module = multiple workers for CPU cores [OK]
Hint: Cluster = multiple processes for CPU cores [OK]
Common Mistakes:
Confusing cluster with database or file system modules
Thinking cluster manages UI or frontend
Assuming cluster runs code in a single process
2. Which of the following is the correct way to check if the current process is the master in a cluster setup?
easy
A. if (cluster.isMaster) { ... }
B. if (cluster.isPrimary) { ... }
C. if (cluster.isWorker) { ... }
D. if (cluster.isMain) { ... }
Solution
Step 1: Recall the updated property name
In recent Node.js versions, cluster.isPrimary replaces cluster.isMaster.
Step 2: Identify the correct syntax
Using cluster.isPrimary correctly checks if the process is the primary (master) process.
Final Answer:
if (cluster.isPrimary) { ... } -> Option B
Quick Check:
Primary process check = cluster.isPrimary [OK]
Hint: Use cluster.isPrimary, not isMaster [OK]
Common Mistakes:
Using deprecated cluster.isMaster instead of cluster.isPrimary