# Clustering of Workers.

**URL:** <https://mediasoup.discourse.group/t/clustering-of-workers/510>\
**Category:** mediasoup libraries\
**Created:** [January 8, 2020, 5:46am UTC](https://mediasoup.discourse.group/t/clustering-of-workers/510 "2020-01-08T05:46:46Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![madabhaviamit](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/madabhaviamit/32/1418_2.png) [@madabhaviamit](https://mediasoup.discourse.group/u/madabhaviamit)\
**Post date:** [January 8, 2020, 5:46am UTC](https://mediasoup.discourse.group/t/clustering-of-workers/510/1 "2020-01-08T05:46:46Z")

</div>

Hi Team,

This query is related to more of Clustering of workers rather than mediasoup.  
So here it is, Nodejs runs in single thread and to take advantage of all of its cores we can use the clustering of workers. Since we use mediasoup.createWorker() api to create a worker and we create as many workers as the length of the CPU cores. Does the createWorker() API internally do the clustering of workers so the number of threads running the server would be equal to the number of workers?

---

<div class="post-metadata">

**Author:** ![ibc](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/ibc/32/1540_2.png) [@ibc](https://mediasoup.discourse.group/u/ibc)\
**Post date:** [January 8, 2020, 1:11pm UTC](https://mediasoup.discourse.group/t/clustering-of-workers/510/2 "2020-01-08T13:11:42Z")

</div>

What do you mean with “clustering of workers”? Do you mean “mediasoup Worker” or Node Workers?

mediasoup does not launch N threads but N mediasoup-worker subprocesses. The OS is the one of running each subprocess in the available CPU cores. We cannot decide that.

---

<div class="post-metadata">

**Author:** ![madabhaviamit](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/madabhaviamit/32/1418_2.png) [@madabhaviamit](https://mediasoup.discourse.group/u/madabhaviamit)\
**Post date:** [January 9, 2020, 5:19am UTC](https://mediasoup.discourse.group/t/clustering-of-workers/510/3 "2020-01-09T05:19:13Z")

</div>

I was referring to Node workers here…

I wanted to have cluster of node workers so that the application’s performance would improve a lot if more threads are spawn, i was assuming when a mediasoup worker is created it internally does the cluster fork operation… Now, i believe that PM2 would solve everything for me.

Thanks for the reply☺

---

<div class="post-metadata">

**Author:** ![ibc](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/ibc/32/1540_2.png) [@ibc](https://mediasoup.discourse.group/u/ibc)\
**Post date:** [January 9, 2020, 5:03pm UTC](https://mediasoup.discourse.group/t/clustering-of-workers/510/4 "2020-01-09T17:03:53Z")

</div>

mediasoup does not deal with that. You can use a single Node thread or many threads (Workers) and call `mediasoup.createWorker()` in each. That will work.

---

<div class="post-metadata">

**Author:** ![sachin-codeplaylabs](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/sachin-codeplaylabs/32/949_2.png) [@sachin-codeplaylabs](https://mediasoup.discourse.group/u/sachin-codeplaylabs)\
**Post date:** [May 30, 2021, 11:52am UTC](https://mediasoup.discourse.group/t/clustering-of-workers/510/5 "2021-05-30T11:52:52Z")

</div>

Suppose i have 4 core CPU. So PM2 will create 4 node worker .

My Question is that each node worker will have 4 subprocess of mediasoup’s worker or each node worker will have only one mediasoup’s worker.

---

<div class="post-metadata">

**Author:** ![ibc](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/ibc/32/1540_2.png) [@ibc](https://mediasoup.discourse.group/u/ibc)\
**Post date:** [May 30, 2021, 12:19pm UTC](https://mediasoup.discourse.group/t/clustering-of-workers/510/6 "2021-05-30T12:19:01Z")

</div>

> [@sachin-codeplaylabs](#):
>
> Suppose i have 4 core CPU. So PM2 will create 4 node worker .

I don’t know why you are mentioning PM2. I can just answer in behalf of mediasoup.

---

<div class="post-metadata">

**Author:** ![sachin-codeplaylabs](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/sachin-codeplaylabs/32/949_2.png) [@sachin-codeplaylabs](https://mediasoup.discourse.group/u/sachin-codeplaylabs)\
**Post date:** [May 30, 2021, 12:22pm UTC](https://mediasoup.discourse.group/t/clustering-of-workers/510/7 "2021-05-30T12:22:42Z")

</div>

Just want to understand how it work

Should i have 1 mediasoup worker for each node JS worker process or each node js worker should have multiple mediasoup worker.

---

<div class="post-metadata">

**Author:** ![ibc](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/ibc/32/1540_2.png) [@ibc](https://mediasoup.discourse.group/u/ibc)\
**Post date:** [May 30, 2021, 12:58pm UTC](https://mediasoup.discourse.group/t/clustering-of-workers/510/8 "2021-05-30T12:58:34Z")

</div>

You can do both. In theory a single Node process/worker should be enough but if your app does a lot of work in Node you may be different Node processes.

---

<div class="post-metadata">

**Author:** ![sachin-codeplaylabs](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/sachin-codeplaylabs/32/949_2.png) [@sachin-codeplaylabs](https://mediasoup.discourse.group/u/sachin-codeplaylabs)\
**Post date:** [May 30, 2021, 12:59pm UTC](https://mediasoup.discourse.group/t/clustering-of-workers/510/9 "2021-05-30T12:59:47Z")

</div>

OK Thanks 🙂

---

<div class="post-metadata">

**Author:** ![BronzedBroth](https://avatars.discourse-cdn.com/v4/letter/b/ad7895/32.png) [@BronzedBroth](https://mediasoup.discourse.group/u/BronzedBroth)\
**Post date:** [June 1, 2021, 7:02am UTC](https://mediasoup.discourse.group/t/clustering-of-workers/510/10 "2021-06-01T07:02:50Z")

</div>

If you run the project via PM2 it’ll help if unexpected crashes happen with your code. If you’re trying to utilize cluster you’ll mess things up…Long story short, you may fork the application multiple times till you have reached core-level as long as it has a means of signaling for either process running.

Your best bet is to utilize the workers via mediasoup to not re-run your code like that and rely on master to instruct routes/workers. Been there and it’s not ideal, for a 64 core machine that’s 64 more concurrent connections not needed. Cheers.

---

<div class="post-metadata">

**Author:** ![todbapi](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/todbapi/32/2570_2.png) [@todbapi](https://mediasoup.discourse.group/u/todbapi)\
**Post date:** [January 3, 2025, 2:50pm UTC](https://mediasoup.discourse.group/t/clustering-of-workers/510/11 "2025-01-03T14:50:07Z")

</div>

Is mediasoup’s sub process able to utilise all available CPU cores?

> Depending on the needed capability, the server side application using mediasoup should launch as many workers as required (no more than the number of CPU cores in the host) and distribute “rooms” (mediasoup routers) across them.

Ans is - A worker represents a mediasoup C++ thread that runs on a single CPU core and handles Router instances.
