# Horizontal scaling

**URL:** <https://mediasoup.discourse.group/t/horizontal-scaling/969>\
**Category:** Deployment & Scalability\
**Created:** [May 9, 2020, 2:36am UTC](https://mediasoup.discourse.group/t/horizontal-scaling/969 "2020-05-09T02:36:09Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![gurupras](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/gurupras/32/738_2.png) [@gurupras](https://mediasoup.discourse.group/u/gurupras)\
**Post date:** [May 9, 2020, 2:36am UTC](https://mediasoup.discourse.group/t/horizontal-scaling/969/1 "2020-05-09T02:36:09Z")

</div>

Hello,

**tl;dr** : Could someone provide a template project that implements scaling across machines _correctly_ using _any_ signaling mechanism? Is it really _just_ the content of `router.pipeToRouter` that needs to be implemented using some signaling mechanism?

I’ve already seen a few topics and related code-snippets on this subject, but I’m having a lot of trouble implementing my own version of `pipeToRouter` across multiple machines. At a very basic level, I’m at a point where I have a stream that is being sent from one node to another. But I’m running into a lot of trouble with implementing signaling (`pause`/`resume`/`close`) correctly.

One example of my confusions is this: `router.pipeToRouter()` does the following:

```auto
pipeProducer = await remotePipeTransport.produce({
    id: producer.id,
    kind: pipeConsumer.kind,
    rtpParameters: pipeConsumer.rtpParameters,
    paused: pipeConsumer.producerPaused,
    appData: producer.appData
});
...
pipeConsumer.observer.on('close', () => pipeProducer.close());

```

When I blindly translate this snippet within my signaling mechanism, I end up closing the original producer instead of the `pipeProducer`. This occurs because `producer.id` is being ‘reused’ in some sense. Obviously, the right thing to do would be to maintain state of the `pipeProducer` and close that instead.

Similarly, for some reason, I had to pass `router.rtpCapabilities` instead of `rtpParameters` in my call to `localPipeTransport.consume()` to get my stream working across nodes. Again, it’s most likely something I’m doing wrong, but I guess that’s my point…

`router.pipeToRouter` already does a great job of being a template to get developers to scale mediasoup across machine. But I strongly feel it would greatly benefit me, and maybe the community as a whole, to have some sort of reference implementation of scaling across machines that guarantees correctness.

---

<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 9, 2020, 5:16pm UTC](https://mediasoup.discourse.group/t/horizontal-scaling/969/2 "2020-05-09T17:16:29Z")

</div>

> [@gurupras](#):
>
> When I blindly translate this snippet within my signaling mechanism, I end up closing the original producer instead of the `pipeProducer` . This occurs because `producer.id` is being ‘reused’ in some sense.

I don’t understand. Original `producer` lives in HOST\_1 while `pipeProducer` lives in HOST\_2, so the fact that they share `id` should not make your app close the original one.

> [@gurupras](#):
>
> Similarly, for some reason, I had to pass `router.rtpCapabilities` instead of `rtpParameters` in my call to `localPipeTransport.consume()` to get my stream working across nodes.

Neither I understand this. Are you really using a `pipeTransport` to communicate HOST\_1 and HOST\_2 or not? And of course, I assume you are using same `mediaCodecs` in `workers` in HOST\_1 and HOST\_2, right?

---

<div class="post-metadata">

**Author:** ![gurupras](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/gurupras/32/738_2.png) [@gurupras](https://mediasoup.discourse.group/u/gurupras)\
**Post date:** [May 27, 2020, 10:23am UTC](https://mediasoup.discourse.group/t/horizontal-scaling/969/3 "2020-05-27T10:23:27Z")

</div>

As expected, I was doing something stupid. I think I’ve fixed most of my bugs, and am able to scale across nodes without issues. Mediasoup has been delightfully simple to work with so far.

I still thing it would help the community to look at a reference mediasoup implementation that scales across nodes with proper initialization and cleanup, and be able to translate it to their own architecture. I’m happy to try and put my code up for this purpose once I clean it up and confirm that it works at a reasonable scale.

Are there any tools or scripts you are aware of and would recommend for testing mediasoup scaling?

---

<div class="post-metadata">

**Author:** ![aetheon](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/aetheon/32/39_2.png) [@aetheon](https://mediasoup.discourse.group/u/aetheon)\
**Post date:** [May 27, 2020, 10:40am UTC](https://mediasoup.discourse.group/t/horizontal-scaling/969/4 "2020-05-27T10:40:20Z")

</div>

> [@gurupras](#):
>
> I still thing it would help the community to look at a reference mediasoup implementation

You could add a github gist with the basic concepts and usage. Would love to take a look and comment on it.

> [@gurupras](#):
>
> Are there any tools or scripts you are aware of and would recommend for testing mediasoup scaling?

There’s a chrome functionality that allows to mock videos and create fake users, the problem is that it must be hosted in kubernetes or something similar and in different geographies to simulate real world conditions. Can share the gist of it if you interested.  
That I’m aware there’s no ready to use tool to do stress/load test.

---

<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 27, 2020, 11:41am UTC](https://mediasoup.discourse.group/t/horizontal-scaling/969/5 "2020-05-27T11:41:13Z")

</div>

> [@aetheon](#):
>
> You could add a github gist with the basic concepts and usage. Would love to take a look and comment on it.

And if you do it will reference it in the website.

---

<div class="post-metadata">

**Author:** ![enterux](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/enterux/32/612_2.png) [@enterux](https://mediasoup.discourse.group/u/enterux)\
**Post date:** [June 1, 2020, 8:40pm UTC](https://mediasoup.discourse.group/t/horizontal-scaling/969/6 "2020-06-01T20:40:14Z")

</div>

I would love to have a look at the potential solution and give it a try.

---

<div class="post-metadata">

**Author:** ![lordarach](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@lordarach](https://mediasoup.discourse.group/u/lordarach)\
**Post date:** [September 24, 2020, 1:13am UTC](https://mediasoup.discourse.group/t/horizontal-scaling/969/7 "2020-09-24T01:13:27Z")

</div>

Hi, gurupras. Were you ever able to put together an example for your solution? I’d love some advice.

---

<div class="post-metadata">

**Author:** ![gurupras](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/gurupras/32/738_2.png) [@gurupras](https://mediasoup.discourse.group/u/gurupras)\
**Post date:** [September 24, 2020, 1:33am UTC](https://mediasoup.discourse.group/t/horizontal-scaling/969/8 "2020-09-24T01:33:49Z")

</div>

I was working on a gist a while back, but I gave up mid way ☹. I’ll get back to finishing it at some point.  
[https://gist.github.com/gurupras/c9ac6609f4c22a515b3aadea8d65bad3](https://gist.github.com/gurupras/c9ac6609f4c22a515b3aadea8d65bad3) If anyone’s interested.

---

<div class="post-metadata">

**Author:** ![lordarach](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@lordarach](https://mediasoup.discourse.group/u/lordarach)\
**Post date:** [September 24, 2020, 1:48am UTC](https://mediasoup.discourse.group/t/horizontal-scaling/969/9 "2020-09-24T01:48:00Z")

</div>

Thank you so much for sharing that, gurupras!

---

<div class="post-metadata">

**Author:** ![dblencowe](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/dblencowe/32/429_2.png) [@dblencowe](https://mediasoup.discourse.group/u/dblencowe)\
**Post date:** [September 24, 2020, 7:15am UTC](https://mediasoup.discourse.group/t/horizontal-scaling/969/10 "2020-09-24T07:15:15Z")

</div>

Be keen to see any additions to this too @gurupras! I’m doing (struggling) with some very similar issues right now and want to see if I can get Mediasoup to 3k consumers at some point in the near future!

For load testing, we’ve been using KITE WebRTC framework, [https://github.com/webrtc/KITE](https://github.com/webrtc/KITE).  
It allows you to write the test in Javascript and has been alright so far at connecting to an instance and running the test.

Sometime in the next week or so I want to try and load a Selenium Grid up on AWS Fargate, I will hopefully be able to connect to this with KITE to run larger-scale load tests. If I get anywhere with this I’ll report back here but tag me if you’ve got any questions about the above!

---

<div class="post-metadata">

**Author:** ![kobvel](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/kobvel/32/1047_2.png) [@kobvel](https://mediasoup.discourse.group/u/kobvel)\
**Post date:** [June 17, 2021, 11:28pm UTC](https://mediasoup.discourse.group/t/horizontal-scaling/969/11 "2021-06-17T23:28:18Z")

</div>

How did your load test plan go?  
Curious to see if that worked out.

---

<div class="post-metadata">

**Author:** ![bwoodlt](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/bwoodlt/32/626_2.png) [@bwoodlt](https://mediasoup.discourse.group/u/bwoodlt)\
**Post date:** [June 22, 2021, 6:26am UTC](https://mediasoup.discourse.group/t/horizontal-scaling/969/12 "2021-06-22T06:26:41Z")

</div>

I’m equally interested in this, let us know how your test goes

---

<div class="post-metadata">

**Author:** ![kdaggubati](https://avatars.discourse-cdn.com/v4/letter/k/f07891/32.png) [@kdaggubati](https://mediasoup.discourse.group/u/kdaggubati)\
**Post date:** [March 16, 2023, 4:45pm UTC](https://mediasoup.discourse.group/t/horizontal-scaling/969/13 "2023-03-16T16:45:11Z")

</div>

Hi All, We implemented **distributed architecture** where participants are able to join in a common meeting room from different machines. This is working fine.

However, our problem right now is that we are **getting static in the audio**. Any suggestions will be highly appreciated. We are also looking for any **professional help** in this matter asap.

Note - We implemented the distributed architecture using - “[mediasoup horizontal scaling · GitHub](https://gist.github.com/gurupras/c9ac6609f4c22a515b3aadea8d65bad3)”

In the non-distributed architecture, we don’t experience any issue.

---

<div class="post-metadata">

**Author:** ![piranna](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/piranna/32/934_2.png) [@piranna](https://mediasoup.discourse.group/u/piranna)\
**Post date:** [March 29, 2023, 4:39am UTC](https://mediasoup.discourse.group/t/horizontal-scaling/969/14 "2023-03-29T04:39:43Z")

</div>

Hi @kdaggubati, I think I can help you, can you write me to [jesus.leganes.combarro@gmail.com](mailto:jesus.leganes.combarro@gmail.com)?

---

<div class="post-metadata">

**Author:** ![damien](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/damien/32/2306_2.png) [@damien](https://mediasoup.discourse.group/u/damien)\
**Post date:** [September 6, 2023, 11:16am UTC](https://mediasoup.discourse.group/t/horizontal-scaling/969/15 "2023-09-06T11:16:58Z")

</div>

If you are ok to deploy mediasoup on Kubernetes with Stunner as turn server you can use the built-it horizontal scalability of Kubernetes by simply having multiple medisoup pod.  
You just need to be sure that your ingress will load balance all the participants of a room to the same media server (this can be done with the annotation [nginx.ingress.kubernetes.io/upstream-hash-by:](http://nginx.ingress.kubernetes.io/upstream-hash-by:) “$arg\_roomId” if you use Nginx ingress and your application implementation has the roomId in the url parameters of the client).
