From what I understand, Docker containers use multiple threads, and Node.js applications use only a single thread. By running something like pm2, which handles Node.js clustering, I could utilize all cores available to the Docker container.
Would this mean I could have greater utilization of each Docker instance by clustering Node.js processes? If so, wouldn't this be preferred to simply running a single threaded Node.js instance per container?
A concern would be if this has side-effects caused by the possibility of a Docker container scaling up or down the number of CPUs it's running.
When process isolation is not needed, use the worker_threads module instead, which allows running multiple application threads within a single Node. js instance. The cluster module allows easy creation of child processes that all share server ports.
It's ok to have multiple processes, but to get the most benefit out of Docker, avoid one container being responsible for multiple aspects of your overall application. You can connect multiple containers using user-defined networks and shared volumes.
Kubernetes orchestration allows you to build application services that span multiple containers, schedule containers across a cluster, scale those containers, and manage their health over time. Kubernetes eliminates many of the manual processes involved in deploying and scaling containerized applications.
The difference with PM2 is that it uses the Node. js cluster module. PM2 creates multiple processes and the cluster module is responsible for distributing incoming traffic to each process. With Docker, distribution of traffic is handled by a load balancer, which we'll talk about in a bit.
Going straight to the point, it looks like multiple containers perform far better than a single container with cluster support. It's not conclusive, but check this out: Performance and reliability when using multiple Docker containers VS standard Node cluster.
Firstly, you don't essentially need pm2 to run node clusters. node comes with an inbuilt clusters module to take advantage of a multi core machine. Also, scaling down an instance doesn't really have an impact unless you hardcode the number of workers. Refer to the example code in the clusters module to understand how you can spawn workers using clusters.
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