CodePark/ questions / Workers, cluster & child processes
START PRACTICINGPRACTICE
Backend question · Node.js › Workers, cluster & child processes

In a cluster setup with 4 workers all calling server.listen(3000), what happens?

The workers share port 3000, and incoming connections are distributed among them

The cluster module lets all workers share port 3000. By default, the primary process accepts each connection and hands it to the workers in turn (round-robin). This is the default on every platform except Windows.

"Only the first worker gets the port" is wrong: cluster shares the port, so there is no
EADDRINUSE error. "Each worker listens on its own port" is also wrong.

"The workers share one event loop and one heap" is wrong too. Workers are separate processes, each with its own memory and event loop. So in-memory state, like sessions, is not shared between them.