I have read over this article pretty thoroughly and as well have spent a few hours researching the subject of clustering (forking processes) in Node.js.
What I can't seem to understand from the article, is what determines which worker process gets request X, if they are all listening on the same port?
Is there a way for the master process to channel the requests, or is it just random?
To configure load balancing, you first create a named “upstream group,” which lists the backend servers. You then set up NGINX Open Source or NGINX Plus as a reverse proxy and load balancer by referring to the upstream group in one or more proxy_pass directives. Configure an upstream group called nodejs with two Node.
The load balancing process makes use of the concept of a cluster switch. What this means is that it distributes parallel tasks to computing units in a system. Proper load balancing ensures that a system is stable and can receive large amounts of traffic.
Load balancing distributes the workload amongst multiple servers to improve the performances meanwhile server clustering, combines multiple servers to work as a single entity.
Clustering means you run a program on several machines (nodes). One reason why you want to do this is: Load balancing. If you have too much load/ work to do for a single machine you can use a cluster of machines instead. A load balancer then can distribute the load over the nodes in the cluster.
There a good explanation here. Long story short, there are 2 different behaviors depending on your node version:
node 0.8-0.10 (and 0.12+ on Windows): Each process listens on the port. The OS decides which one to wake up when a new connection comes in. In some applications under some OSs this doesn't work very well and leaves a few processes with a strong majority of the connections; in most it works just fine.
node 0.12+ (except on Windows): The master process listens on the port. As they come in, it hands them off to workers in a round-robin fashion.
In either of these cases, your application should treat it as random (although you can probably assume reasonable load-balancing characteristics). However, if you for some reason need finer control, one sentence in that article (note that it was written by a node.js core contributor, so there's some authority here):
Turning the selection algorithm into something that is configurable or pluggable by the developer is a change that is under consideration.
says that you might get what you're looking for. There appears to be an issue on Github pertaining to this option.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With