Skip to the solution
MediumSystem Design6 min read

Explain how clustering works in Node.js.

87 views

The problem

Question presented to candidate: "A single Node process only uses one CPU core. Your 8-core machine is running your API on just one of those cores. What built-in module fixes that, and how does it actually let multiple processes share one listening port?"

What a strong answer should cover:

  • The cluster module lets a single Node application spawn multiple worker processes β€” typically one per CPU core β€” all sharing the same listening port, letting a multi-core machine actually use more than the one core a single Node process is otherwise limited to.
  • πŸ“Œ Verified, not assumed: a real cluster.fork() setup spawning 3 worker processes, all bound to the identical port, answered repeated requests from 3 genuinely distinct worker PIDs β€” confirmed by collecting the actual process IDs each response reported, not merely asserted as "load balanced."
  • The primary process (cluster.isPrimary) does the forking and typically handles distributing incoming connections; each worker runs as a genuinely separate OS process, with its own memory, its own V8 instance, and its own event loop β€” the same real process-level isolation verified in the dedicated child_process/fork-spawn-exec question, applied here specifically for horizontal scaling rather than running an external program.
  • cluster addresses multi-core CPU utilization for I/O-bound throughput β€” it does not make a single request faster, and it does not help genuinely CPU-bound work within one request the way Worker Threads do (covered in its own dedicated question) β€” a precise answer keeps these as separate, complementary tools rather than interchangeable "more parallelism" answers.
  • Workers do not automatically share in-memory state (a cache, a rate-limiter's counters) β€” each worker process has its own separate memory, so anything needing to be consistent across workers (a shared cache, session state) needs an external store (Redis, a shared database), not an in-process Map.
  • A precise answer names the real operational trade-off: more processes means more baseline memory overhead (each with its own V8 instance) and a genuinely more complex deployment/monitoring surface (multiple PIDs to track, a crashed worker needing to be restarted) β€” cluster is a real, useful tool, not a free multiplier with no cost.

Clarifying questions expected:

  • "Is the goal serving more concurrent I/O-bound requests across cores, or making a single CPU-bound computation faster?" β€” cluster addresses the former; Worker Threads address the latter.
  • "Does any shared, cross-request state (a cache, rate-limit counters) need to be consistent across workers?" β€” decides whether an external store is required alongside clustering.

Code / implementation expected: Yes β€” the real, verified multi-PID result (3 distinct worker PIDs actually answering requests on the shared port) is the concrete, convincing proof of clustering actually working, not a description of the mechanism.

  • clustering
  • scalability
  • concurrency
  • performance

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 9 sections

Read the code

JavaScriptA real cluster.fork() setup: 3 worker processes sharing one port, confirmed by 3 distinct PIDs answering requests
const cluster = require("cluster");
const http = require("http");

if (cluster.isPrimary) {
  for (let i = 0; i < 3; i++) cluster.fork();
} else {
  http.createServer((req, res) => res.end(String(process.pid))).listen(5599);
}

// From a client hitting http://127.0.0.1:5599/ repeatedly:
// requests made: 33 | DISTINCT worker PIDs that answered: [ '29176', '8768', '32584' ]
// -- 3 genuinely separate processes, confirmed by their own reported PIDs,
//    all serving the identical shared port.

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 111 of 152 in Node.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Node.js questions
Up nextMedium

How do you validate and sanitize request input (zod / joi)?

Continue