4

I am researching use cases to introduce cluster computing with nodejs to a few friends.

My goal is to explain a few real life nodejs cluster cases, and why nodejs is helpful at creating those particular cases.

So far, really one case appeals to me:

  • clustering a webserver (like an API server by REST) (reason: load balancing)

I saw examples about doing expensive calculations, mentioning the advantages on doing so on a different thread/processor.

That lead me to thinking about about having worker pools, each pool doing different parts like handling solely the connection to a database, generating PDFs and similar.

Trying to understand the ties from the worker to the master, I fiddled around and I am still not sure:

  • are nodejs clusters are only suited for having the same process be able to run in parallel (documentation suggests it)
  • or does it makes sense to have nodejs cluster workers like separate tasks in pools.

.. resulting in: what use cases are actually real world use cases with nodejs?

Hopefully it is understandable. Opinions are very welcomed.

BananaAcid
  • 3,221
  • 35
  • 38
  • Also, you might find my answer here helpful: https://stackoverflow.com/a/19324665/362536 I wrote it years ago, but I think it's still mostly up-to-date. – Brad Mar 17 '19 at 04:20
  • @Brad "Node.js is a bit different. It is single-threaded from a JavaScript perspective, but other tasks like disk and network IO are handled on separate threads automatically." This is interesting, thank you – BananaAcid Mar 17 '19 at 07:48

1 Answers1

4

are nodejs clusters are only suited for having the same process be able to run in parallel (documentation suggests it)

Sort of. The built-in cluster module is just spawning child processes that happen to be the same script that's running. You could always just execute child processes other ways.

or does it makes sense to have nodejs cluster workers like separate tasks in pools

Whatever works for you and your application! There's no reason to follow convention if you have a reason not to.

resulting in: what [cluster] use cases are actually real world with nodejs?

(I assume you were asking about real world use cases for clusters.)

For most applications (the ones I've seen and use anyway), it's possible to fully saturate/utilize a system's resources with a single Node.js processes. Just because your code is single threaded doesn't mean all the native code you're calling out to is. A lot of folks use the cluster module because they believe it's the only way to fully utilize all the CPUs on the host. This is from a common misunderstanding of the Node.js documentation.

I'm sure there are some applications that are just CPU-bound in that single thread for running JavaScript, but for many, I/O is a much bigger blocker.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • Hi, thanks. That use of the cluster module (using all CPUs) is also what I thought would be the only way - misunderstanding: what else is there? – BananaAcid Mar 17 '19 at 07:44