Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js clustering vs multiple Docker containers

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.

like image 725
Kevin Ghadyani Avatar asked Oct 30 '17 07:10

Kevin Ghadyani


People also ask

Is it possible to cluster multiple node processes?

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.

Can you run 2 Docker containers at the same time?

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.

Which Docker tool manage clustering and scaling of multiple container?

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.

Which is better PM2 or Docker?

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.


2 Answers

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.

like image 139
stefanobaldo Avatar answered Sep 21 '22 06:09

stefanobaldo


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.

like image 25
Avinash Avatar answered Sep 17 '22 06:09

Avinash