Skip to content

Load balancing in Flussonic

Load balancing—process of distributing client requests among servers in a cluster according to some particular algorithm.

Load balancing is aimed to:

  • prevent server overload in a cluster;
  • optimize the usage of the resources in a server cluster;
  • maximize the throughput.

It also provides scalability and redundancy, e.g., you can add servers in a cluster when needed and in case one of the servers is down, the balancer ensures that the load is distributed evenly among other working servers so that it minimizes impact on users.

Flussonic can balance users between multiple Flussonic Media Server nodes. Load balancing is achieved by redirecting client requests to other servers in a cluster. Balancing is supported both for play and publish requests.

There are various load balancing algorithms, which suit best for a particular situation. The choice of the method depends on your needs and goals. Flussonic uses the following load balancing modes:

  • Least number of connections (clients mode):

Directs traffic to the server with the fewest active client connections. Most useful when there is a large number of persistent connections in the traffic distributed unevenly among the servers. No parameters are needed.

  • Least output bitrate (bitrate mode):

Directs client requests to the less loaded server according to the output bitrate value. You can optionally specify max_bitrate (in bit/s, you can also specify it in Mbit/s 40M or Kbit/s 40K) — a maximum bitrate for each peer. Load balancer will direct traffic to the peer with the least current output bitrate.

  • Least bandwidth usage (usage mode):

Distributes client requests according to the bandwidth usage of the server. In this case the required parameter is max_bitrate (in bit/s, you can also specify it in Mbit/s 40M or Kbit/s 40K) — a maximum bitrate for each peer. Load balancer will calculate bandwidth usage as a percent of maximum bitrate (current bitrate / max_bitrate * 100) and direct traffic to the peer with the least bandwidth usage.

Warning

You should specify the max_bitrate value for every peer in a cluster, otherwise, a balancer will not work properly.

  • Least number of active streams (streams mode):

Distributes client requests according to the number of active streams. This mode can be used for handling published streams received via m4s:// from other Flussonic servers (ingest servers). For example, if you publish streams via WebRTC, RTMP, or SRT in the pool of ingest servers and push them to a pool of transcoder servers, each published stream will be redirected to a transcoder with the least number of active streams.

How do you know you need a balancer?

If your streaming platform or service has more than 10 000 viewers, then you should consider it.

To use the balancer add it to the configuration file (/etc/flussonic/flussonic.conf):

cluster_key SOME_CLUSTER_KEY;
balancer lb0 {
  mode bitrate;
  server p1 max_bitrate=60M;
  server p2 max_bitrate=40M;
  server p3 max_bitrate=30M;
}

Parameters:

  • lb is a balancer name.

  • server is a peer (like peer1.example.com).

  • mode is a balancing mode (bitrate, usage, clients, streams). mode bitrate is used by default.

Example of a URL for a stream channel1 request:

  • http://FLUSSONIC-IP/lb/channel1/index.m3u8

You can configure a few balancers at a time if needed.

Warning

Load balancer works with HTTP-based protocols, such as HLS, MPEG-DASH, and also WebRTC WHIP / WHEP.

Balancer configuration

  1. Define a set of servers to include in the load balancing scheme and specify the same cluster_key in their configuration files to connect them. Configure your streams on these servers.
  2. Choose one or more servers for load balancing. Run as many balancers in your cluster as you need for the required fault tolerance level. You may allot whole servers or use Edge servers as balancers.
  3. Configure a balancer with balancer option (do not forget to specify the cluster_key as well):
cluster_key SOME_CLUSTER_KEY;
balancer lb0 {
  mode bitrate;
  server stream.example.com max_bitrate=60M;
  server stream.example.tv max_bitrate=40M;
  server stream.exmpl.com max_bitrate=30M;
}

In the example above we defined a balancer lb0, including 3 servers to balance among by the bitrate value (mode bitrate).

Here is an example of configuration with balancing among active client connections (mode clients):

cluster_key SOME_CLUSTER_KEY;
balancer lb0 {
  mode clients;
  server stream.example.com;
  server stream.example.tv;
  server stream.exmpl.com;
}

Here is an example of configuration with balancing among the bandwidth usage (mode usage):

cluster_key SOME_CLUSTER_KEY;
balancer lb0 {
  mode usage;
  server stream.example.com max_bitrate=60M;
  server stream.example.tv max_bitrate=40M;
  server stream.exmpl.com max_bitrate=30M;
}

Here is an example of configuration with balancing among the number of active streams (mode streams):

cluster_key SOME_CLUSTER_KEY;
balancer lb0 {
  mode streams;
  server trancoder1;
  server trancoder2;
  server trancoder3;
}

GeoIP balancing

Load balancer can distribute client requests among servers in a cluster with consideration to a region of a client. This works as follows:

  1. For each peer in a cluster, you can specify one or several country codes in the countries parameter and/or the country_default=true option that means "all non-specified countries".
  2. Flussonic detects countries using the free file-based MaxMind GeoLite2 geolocation databases.
  3. For each client request, the load balancer looks for a peer with the country code of the client’s region. If it is found, the client's request is redirected to this peer. If several servers with the corresponding country code are found, the balancer selects the server according to the mode (for example, bitrate).
  4. If there is no server with the corresponding country code, the request is redirected to the server with country_default=true. If there are several such servers, the balancer selects the server according to the mode (for example, bitrate). If there is no such a server, the request is rejected.

Example:

cluster_key SOME_CLUSTER_KEY;
balancer lb0 {
  mode bitrate;
  server p1 max_bitrate=60M countries=RU;
  server p2 max_bitrate=40M countries=RU,CN;
  server p3 max_bitrate=30M countries=CN countries_default=true;
  server p4 max_bitrate=30M countries_default=true;
}

This configuration works as follows:

  • If a request comes from China, it will be redirected to P2 or P3 (depending on bitrate).
  • If a request comes from Russia, it will be redirected to P1 or P2.
  • If a request comes from any other country, it will be redirected to P3 or P4.