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
clustermodule 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 dedicatedchild_process/fork-spawn-exec question, applied here specifically for horizontal scaling rather than running an external program. clusteraddresses 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) —
clusteris 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?" —
clusteraddresses 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.