Node.js clustering vs multiple Docker containers Node.js clustering vs multiple Docker containers docker docker

Node.js clustering vs multiple Docker containers


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 are talking about clustering NodeJS inside another docker component I don't think that will give you an extra boost. NodeJS like you said runs in a single thread so either pm2 (I actually use it) or docker will have the same effect in terms of distributing the load.

NodeJS has a concept of web workers to handle how it handles the incoming requests but even a not so fast server will handle hundreds of requests because is a non-blocking architecture.

I recognize I still need to dig into Docker which I want to but I don't have the time but what I would do is to have 1 docker image per server (either virtual or physical) and then have pm2 run several instances of node inside it.

The reason for this is that I want Docker to handle the dependencies for me and I want pm2 to handle the cluster management since it has cool features like restart automatically when the memory reaches a target or to restart an errored node