Clustering of Workers.

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?

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.

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☺

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.

1 Like

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.

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

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.

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.

OK Thanks :slight_smile:

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.