Node.js Developer interview question
When would you use Node.js clustering?
Clustering runs multiple Node.js processes so a service uses multiple CPU cores and isolates failures. Prefer an external orchestrator when available, keep state outside workers, and drain connections during restart.
Direct answer
What a strong answer covers
How it works
The cluster module forks the same server into several processes that share one listening port, so a Node service can use every core and a crash in one worker does not take the others down. It suits bare-metal or VM deployments where the process manager is Node itself. Under Kubernetes or a platform that already runs one container per core and restarts failed ones, clustering inside the container duplicates that job. In either model, workers must hold no in-memory session or cache state that another worker needs, and restarts should drain in-flight connections before exiting.
Example
A monolith on a single 8-core VM ran under PM2 in cluster mode with eight workers, sessions in Redis, and a graceful shutdown that stopped accepting connections and waited up to 10 s for requests to finish. When the same service moved to Kubernetes it ran one process per pod with a CPU request of one core and let the horizontal autoscaler add pods.
Common mistakes
- Storing sessions or rate-limit counters in process memory and getting inconsistent behaviour across workers.
- Killing workers with SIGKILL during deploys and dropping active requests.
- Running cluster inside a container that is already scheduled per core.
A useful answer structure
Answer from your own work.
Do not memorise a script: use only examples you can explain honestly in follow-up questions.
Open the desktop workspace and rehearse a clear answer in your own words.