Reputation: 3353
I have created a Node.js cluster following this example.
And I have put console.log(cluster.worker.id)
in my API call to determinate which worker is accepting the request.
All workers have started successfully (I have 4 cores so I've created 4 workers) but the requests are always handled by one worker.
Does anyone know why is this happening? I use Windows 7 operating system and have version 0.8.8 version of Node.
Upvotes: 4
Views: 2094
Reputation: 91619
You aren't seeing any switching of workers because Cluster recognizes that it isn't under high load, and doesn't bother switching workers. Cluster will automatically load-balance when processes are under high load. Here's what the documentation says:
When multiple processes are all
accept()
ing on the same underlying resource, the operating system load-balances across them very efficiently. There is no routing logic in Node.js, or in your program, and no shared state between the workers. Therefore, it is important to design your program such that it does not rely too heavily on in-memory data objects for things like sessions and login.
Upvotes: 3