CodeBlanks Logo Icon CodeBlanks

Summary - Clustering

Combine fork, IPC and isMaster to write a cluster server.

Summary - Clustering

In this group you learned the cluster module: the master starts workers with cluster.fork(), workers share the port, and IPC carries messages. cluster.isMaster separates master from worker.

Graceful reload gives zero-downtime updates, and the exit event drives auto-restart. These patterns are the foundation of scaling Node.js servers in production.

When to use cluster

Cluster is most useful for CPU-heavy apps and HTTP servers. A single process leaves multi-core machines idle; cluster uses all cores.

For I/O-heavy workloads (network, disk) the gain is smaller, because Node.js's event loop already handles concurrency. Still, the multi-core advantage always applies.

Practice Log master or worker

If cluster.isMaster is true log "master", otherwise log "worker".

Starter code

index.js
// Import the cluster module

// log "master" or "worker" based on isMaster

Sample solution

index.js
const cluster = require("cluster");
console.log(cluster.isMaster ? "master" : "worker");

Expected output

master

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the main purpose of the cluster module?

  • Speeding up file reads
  • Using multiple CPU cores Correct answer
  • Rendering a web page
  • Caching database queries

Explanation: Cluster lets you use all CPU cores on a multi-core machine.

2 Match each piece to its role

Term Meaning
cluster.fork() Starts a worker
cluster.isMaster Main flag
cluster.on("exit") On worker exit
require("cluster") Imports the module

All lessons in Advanced