09 / 17

How do workers in a Node.js cluster share a port without conflict?

Difficulty: 5/10
cluster module, port sharing, load balancing

Workers do not directly bind to the port. The master process is the one that binds to the server port and then passes incoming connection handles to workers via IPC, so there is no port conflict.

This is one of the most commonly misunderstood aspects of Node.js clustering. When multiple workers all call server.listen(3000), only the master process actually binds to port 3000 at the OS level. Workers send a request to the master saying they want to listen on port 3000, and the master accepts connections on their behalf, then delegates each connection to one of the workers.

Port Sharing Flow
  1. 1

    Master process starts and forks N workers

  2. 2

    Each worker calls server.listen(3000)

  3. 3

    Workers intercept this call and send a message to the master via IPC

  4. 4

    Master binds to port 3000 at the OS level (only once)

  5. 5

    Client connects — master receives the TCP connection

  6. 6

    Master picks a worker (round-robin or OS) and sends the connection handle

  7. 7

    Worker accepts the handle and processes the HTTP request

Port Sharing in Action

Scenario Questions

0-2 years experience

  1. 1You need to write a simple HTTP server that runs on four CPU cores using Node's cluster module. How would you set it up so all workers accept traffic on port 3000 without each trying to bind separately?
  2. 2If you start two worker processes that both call server.listen(8080) without a master, what error will you see and why?

2-5 years experience

  1. 1During a rollout, you notice that after adding a new worker, some requests start failing with ECONNREFUSED. Walk me through how you would debug the port sharing in the cluster.
  2. 2Explain the trade‑offs between letting the master process listen on the port and passing the handle to workers versus each worker calling listen on the same port.

5-8 years experience

  1. 1Your service needs to handle 10k concurrent connections and you want to use sticky sessions for WebSocket connections. How would you modify the default cluster port‑sharing mechanism to support session affinity while still avoiding port conflicts?
  2. 2At scale, what are the performance implications of the OS load‑balancing approach used by Node's cluster when many workers share a single listening socket? How would you mitigate any bottlenecks?

8+ years experience

  1. 1A legacy monolith runs on a single Node process listening on port 80. The team wants to migrate to a multi‑process architecture using cluster across multiple servers behind a load balancer. Describe the architectural changes, how you’d handle port sharing across machines, and the operational considerations.
  2. 2If you need to roll out a zero‑downtime upgrade of a clustered Node service that shares a port, what deployment strategy would you use to avoid port conflicts and ensure uninterrupted traffic?

Follow-up Questions

  • What happens to existing connections if a worker process exits unexpectedly?
  • How does the operating system decide which worker receives a new connection?
  • Can you achieve session affinity with the default cluster behavior, and if not, how would you implement it?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.