I understand that cluster.fork will allow for multiple processes to listen on the same port(s), what I also want to know is how much additional overhead is there in supporting this when some of your workers are not listeners/handlers for the tcp service?
I have a service that I also want to launch a couple of workers.. ex: 2 web service listener processes, and 3 worker instances. Is it best to use cluster for them all, or would cluster for the 2 web services, and child_process for the workers be better?
I don't know the internals in node, but think it would be nice for myself and others to have a better understanding of which route to take given different needs. For now, I'm using cluster for all the processes.
In a single thread, the individual instance of node. js runs specifically and to take advantage of various ecosystems, a cluster of node. js is launched, to distribute the load. With the help of a cluster module, child processes can be created very easily sharing the server ports.
Worker threads can be used to solve the above-mentioned caveats with child processes because worker threads share memory and communication between threads is possible. Essentially the difference between worker threads and child processes is same as the difference between a thread and a process.
3.3. Cluster-Fork. Cluster-Fork runs a command on compute nodes of your cluster. Often we want to execute parallel jobs consisting of standard UNIX commands. By "parallel" we mean the same command runs on multiple nodes of the cluster.
cluser.fork
is implemented on top of child_process.fork
. The extra stuff that cluster.fork
brings is that, it will enable you to listen on a shared port. If you don't want it, just use child_process.fork
. So yeah, use cluster for web servers and child_process for workers.
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