A single instance of Node.js runs in a single thread. The cluster module allows you to easily create child processes that all share server ports, multiplying throughput across CPU cores.
Node.js Cluster Module
Scale across multi-core CPUs by spawning workers on a shared server port.
Key Takeaways: Node.js Cluster Module
Mastering Node.js Cluster Module helps you write clean and reliable Node.js code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
Node.js Cluster Module Code Example
Node.jsconst cluster = require("cluster");
const os = require("os");
if (cluster.isPrimary) {
const numCPUs = os.cpus().length;
console.log(`Master spinning up ${numCPUs} CPU worker processes.`);
// for (let i = 0; i < numCPUs; i++) cluster.fork();
}
Output
Master spinning up 8 CPU worker processes.
Run this example on your own computer. The "Get Started" lesson shows how to set it up.
Finished this lesson?
Mark it complete to track your progress.
Stuck on something?
Ask the community in the CodesInTech forum.
Need a developer?
Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.
Explore Our Services