Currently you are not logged in.

 Login | Register 

Main

Resources

Training

Events

Development

Resources -> Documentation -> Tutorials -> Load Balancing

This page has been visited 32309 times.


1.  Load Balancing in OpenSIPS

The "load-balancing" module comes to provide traffic routing based on load. Shortly, when OpenSIPS routes calls to a set of destinations, it is able to keep the load status (as number of ongoing calls) of each destination and to choose to route to the less loaded destination (at that moment). OpenSIPS is aware of the capacity of each destination - it is preconfigured with the maximum load accepted by the destinations. To be more precise, when routing, OpenSIPS will consider the less loaded destination not the destination with the smallest number of ongoing calls, but the destination with the largest available slot.

Also, the "load-balancing" (LB) module is able to receive feedback from the destinations (if they are capable of). This mechanism is used for notifying OpenSIPS when the maximum capacity of a destination changed (like a GW with more or less E1 cards).

The "load-balancing" functionality comes to enhance the "dispatcher" one. The difference comes in having or not load information about the destinations where you are routing to:

  • Dispatcher has no load information - it just blindly forwards calls to the destinations based on a probabilistic dispersion logic. It gets no feedback about the load of the destination (like how many calls that were sent actually were established or how many are still going).
  • Load-balancer is load driven - LB routing logic is based primary on the load information. The LB module is using the DIALOG module in order to keep trace of the load (ongoing calls).

A Workshop on this topic was held at Amoocon (former AsteriskTag) in Rostock, 4-5 May 2009


2.  Load Balancing - how it works

When looking at the LB implementation in OpenSIPS, we have 3 aspects:

2.1  Destination set

A destination is defined by its address (a SIP URI) and its description as capacity.

Form the LB module perspective, the destinations are not homogeneous - they are not alike; and not only from capacity point of view, but also from what kind of services/resources they offer. For example, you may have a set of Yate/Asterisk boxes for media-related services -some of them are doing transcoding, other voicemail or conference, other simple announcement , other PSTN termination. But you may have mixed boxes - one box may do PSTN and voicemail in the same time. So each destination from the set may offer a different set of services/resources.

So, for each destination, the LB module defines the offered resources, and for each resource, it defines the capacity / maximum load as number of concurrent calls the destination can handle for that resource.

Example:

4 destinations/boxes in the LB set
    1) offers 30 channels for transcoding and 32 for PSTN
    2) offers 100 voicemail channels and 10 for transcoding
    3) offers 50 voicemail channels and 300 for conference
    4) offers 10 voicemail, 10 conference, 10 transcoding and 32 PSTN

This translated into the following setup:

+----+----------+------------------------+---------------------------------+
| id | group_id | dst_uri                | resources                       |
+----+----------+------------------------+---------------------------------+
|  1 |        1 | sip:yate1.mycluset.net | transc=30; pstn=32              |
|  2 |        1 | sip:yate2.mycluset.net | vm=100; transc=10               |
|  3 |        1 | sip:yate3.mycluset.net | vm=50; conf=300                 |
|  4 |        1 | sip:yate4.mycluset.net | vm=10;conf=10;transc=10;pstn=32 |
+----+----------+------------------------+---------------------------------+

For runtime, the LB module provides MI commands for:

  • reloading the definition of destination sets
  • changing the capacity for a resource for a destination

2.2  Invoking Load-balancing

Using the LB functionality is very simple - you just have to pass to the LB module what kind of resources the call requires.

The resource detection is done in the OpenSIPS routing script, based on whatever information is appropriated. For example, looking at the RURI (dialed number) you can see if the call must go to PSTN or if it a voicemail or conference number; also, by looking at the codecs advertised in the SDP, you can figure out if transcoding is or not also required.


  if (!load_balance("1","transc;pstn")) {
      sl_send_reply("500","Service full");
      exit;
  }

The first parameter of the function identifies the LB set to be used (see the group_id column in the above DB snapshot). Second parameter is list of the required resource for the call.

The load_balance() will automatically create the dialog state for the call (in order to monitor it) and will also allocate the requested resources for it (from the selected box).
The function will set as destination URI ($du) the address of the selected destination/box.

The resources will be automatically released when the call terminates.

The LB module provides an MI function that allows the admin to inspect the current load over the destinations.

2.3  The LB logic

The logic used by the LB module to select the destination is:

  1. gets the destination set based on the group_id (first parameter of the load_balance() function)
  2. selects from the set only the destinations that are able to provide the requested resources (second parameter of the load_balance() function)
  3. for the selected destinations, it evaluated the current load for each requested resource
  4. the winning destination is the one with the biggest value for the minimum available load per resources.
Example:

4 destinations/boxes in the LB set
    1) offers 30 channels for transcoding and 32 for PSTN
    2) offers 100 voicemail channels and 10 for transcoding
    3) offers 50 voicemail channels and 300 for conference
    4) offers 10 voicemail, 10 conference, 10 transcoding and 32 PSTN

when calling load_balance("1","transc;pstn") ->

1) only boxes (1) and (4) will be selected at as they offer both transcoding and pstn

2) evaluating the load  :
    (1) transcoding - 10 channels used; PSTN - 18 used 
    (4) transcoding - 9 channels used; PSTN - 16 used 

   evaluating available load (capacity-load) :
    (1) transcoding - 20 channels used; PSTN - 14 used 
    (4) transcoding - 1 channels used; PSTN - 16 used 

3) for each box, the minimum available load (through all resources)
    (1) 14 (PSTN)
    (2) 1 (transcoding) 

4) final selected box in (1) as it has the the biggest (=14) available load for the most loaded resource.

The selection algorithm tries to avoid the intensive usage of a resource per box.


3.  Study Case: routing the media gateways

Here is the full configuration and script for performing LB between media peers.

3.1  Configuration

Let's consider the case previously described:

4 destinations/boxes in the LB set
    1) offers 30 channels for transcoding and 32 for PSTN
    2) offers 100 voicemail channels and 10 for transcoding
    3) offers 50 voicemail channels and 300 for conference
    4) offers 10 voicemail, 10 conference, 10 transcoding and 32 PSTN

This translated into the following setup:

+----+----------+------------------------+---------------------------------+
| id | group_id | dst_uri                | resources                       |
+----+----------+------------------------+---------------------------------+
|  1 |        1 | sip:yate1.mycluset.net | transc=30; pstn=32              |
|  2 |        1 | sip:yate2.mycluset.net | vm=100; transc=10               |
|  3 |        1 | sip:yate3.mycluset.net | vm=50; conf=300                 |
|  4 |        1 | sip:yate4.mycluset.net | vm=10;conf=10;transc=10;pstn=32 |
+----+----------+------------------------+---------------------------------+

3.2  OpenSIPS Scripting

debug=1
memlog=1

fork=yes
children=2
log_stderror=no
log_facility=LOG_LOCAL0

disable_tcp=yes
disable_dns_blacklist = yes

auto_aliases=no

check_via=no
dns=off
rev_dns=off

listen=udp:xxx.xxx.xxx.xxx:5060



loadmodule "modules/maxfwd/maxfwd.so"
loadmodule "modules/sl/sl.so"
loadmodule "modules/db_mysql/db_mysql.so"
loadmodule "modules/tm/tm.so"
loadmodule "modules/xlog/xlog.so"
loadmodule "modules/uri/uri.so"
loadmodule "modules/rr/rr.so"
loadmodule "modules/dialog/dialog.so"
loadmodule "modules/mi_fifo/mi_fifo.so"
loadmodule "modules/mi_xmlrpc/mi_xmlrpc.so"
loadmodule "modules/signaling/signaling.so"
loadmodule "modules/textops/textops.so"
loadmodule "modules/load_balancer/load_balancer.so"



modparam("mi_fifo", "fifo_name", "/tmp/opensips_fifo")

modparam("dialog", "dlg_flag", 13)
modparam("dialog", "db_mode", 1)
modparam("dialog", "db_url", "mysql://opensips:opensipsrw@localhost/opensips")

modparam("rr","enable_double_rr",1)
modparam("rr","append_fromtag",1)

modparam("load_balancer", "db_url","mysql://opensips:opensipsrw@localhost/opensips")


route{
	if (!mf_process_maxfwd_header("3")) {
		sl_send_reply("483","looping");
		exit;
	}


	if (!has_totag()) {
                # initial request
		record_route();
	} else {
		# sequential request -> obey Route indication
		loose_route();
                t_relay();
                exit;
        }

        # handle cancel and re-transmissions
	if ( is_method("CANCEL") ) {
		if ( t_check_trans() )
			t_relay();
		exit;
	}


        # from now on we have only the initial requests
        if (!is_method("INVITE")) {
                send_reply("405","Method Not Allowed");
                exit;
        }

        # detect resources and do balancing
        if ($rU=~"^1") {
                # looks like a Conference call
                load_balance("1","conf");
        } else if ($rU=~"^2") {
                # looks like a VoiceMail call
                load_balance("1","vm");
        } else {
                # PSTN call, but the GWs supports only G711
                # for calls without G711, transcoding will be used on the GW
                if ( !search_body("G711") ) {
                       load_balance("1","transc;pstn");
                } else {
                       load_balance("1","pstn");
                }
        }

        # LB function returns negative if no suitable destination (for requested resources) is found,
        # or if all destinations are full
        if ($retcode<0) {
             sl_send_reply("500","Service full");
             exit;
        }

	xlog("Selected destination is: $du\n");

        # send it out
	if (!t_relay()) {
		sl_reply_error();
	}
}



4.  Comments

Gavin?15 March 2009, 23:41

Does this have to use Yate? Can Asterisk or FreeSwitch be used?

[bogdan] - there is no dependency (regarding the application you LB) - so you can have Yate, Asterisk, FreeSwitch, Cisco GW, Audiocodec GW, etc

20 March 2009, 02:41

This is nice and simple. I am still learning OpenSIPs, but is it possible to combine this with a registrar function? Looking how to combine Load Balancing to a few Asterisk servers with OpenSIP also performing the registration.

[bogdan] - Of course it is possible - if you look at the default opensips cfg (that has the registrar capabilities), for processing the INVITE requests you can use load_balancing() function. If you have issues, use the mailing list for getting help

haidavila?04 April 2009, 04:21

Hi. I´ve installed the new version (1.5) and I'm using load_balancer module but I'm having a problem... When a SIP server goes down and if it had the best attributes, load_balancer keeps sending the calls to it and all my calls drop. Is this normal? Can Opensips notices that this server is not working and pass it to the next sip server?

Thank you!

[bogdan]- unfortunately in the current version (1.5) you can not disable from script a destination; but you can do it from outside, via MI command (lb_resize to 0); you can use exec() from script to run the opensipsctl and resize the 0 the guilty server. Hope it will help. - btw, we can do disable from script with 1.6

haidavila?11 April 2009, 03:21

Thank you bogdan, it really helped me.

Regards!

elred?14 April 2009, 19:01

What if you have, let's say, 3 phones. You want each phone being able to contact each others. BUT, if you don't fall on the same server where the REGISTER was broadcasted, the PABX won't know the IP of the callee, and call won't be achieved. Is there a way to bounce REGISTER to multiple PABX ? Thanks !

[bogdan] - I do not think this is a typical case of LB (as there is some relation between the PBX you balance). What you can do is to separately deal with REGISTER and fork it (from LB server, with no LB) to all PBX and to do do LB only for INVITEs. Just an idea

asterisktech?21 April 2009, 14:25

Hi,

I also would appreciate if someone could post here a howto of integration of opensips with asterisk.

asterisktech

[bogdan] - what kind of "integration" you have in mind? I mean what opensips to do and what asterisk to do.

geejee?10 May 2009, 10:35

Hi,

what I like to do is to connect more than one client (e.g. desktop phone and soft phone) to one asterisk sip account. Asterisk cannot handle this because only one client can register. So opensips should work as sort of client inbetween. Username/password verification should be done by Asterisk. Does anyone know how to do this and give me a sample configuration for opensips?

Thanks

sriram?09 June 2009, 18:15

HI,

I am load balancing the calls between gateways with different port values. Say I have total of 15 ports, the load balance modules works fine till 15th call. At 16th call load balance fails(this is the only call made,means all 15 ports are free ) and I am not getting calls on the gateway if I don't issue lb_reload command. I would appreciate if someone could let me know any other method to call get the current load status automatically set without entering the lb_reload comman statement.

thanks

[bogdan] - upgrade from svn to the latest version and if you still have issue, pleas post on the users mailing list

Javier?31 July 2009, 02:25

Hi,

I'm getting the error: ERROR:core:yyparse: module 'mi_xmlrpc.so' not found in '/usr/local/lib/opensips/modules/' all the time whenever I try to use the load_balancer module. This is something I got since 1.5.1 and thought was a bug in that version and used dispatcher instead. Today, I made a fresh install of opensips 1.5.2 over a Centos 5.1 and got the same error. Could somebody point me in the right direction to solve this problem ?

thanks!

vaitek?01 August 2009, 10:17

Hello Bogdan, Great work... what if one of balanced server has ended the call in case of the limit call time ? - OpenSIPS doesn't release resources as I realised... - how can I tell LB module that it should release resources? Is there any way to shere information about resources between LB and VOIP(switches,gateways,etc) ?

[bogdan] - if the call is release properly (from signalling point of view, with a BYE) the LB will see the call as terminated and release the resources.

plimaye?02 August 2009, 20:33

Javier,

You need to uncomment mi_xmlrpc in Makefile to compile the module , but you will require xmlrpc-c and xmlrpc-c-devel installed ( I am using xmlrpc-c-1.16.6-1.1582 on Centos 5.3) to compile 1.5.2. So far its working but its not supported. Will update this if I find issues with xmlrpc-c version.

Javier?04 August 2009, 17:40

Plimaye, Thank you very much for your help. I did that and certainly, it was in the exclude modules at makefile. I commented it. After that, I ran make clean, make and make install again and compiled ok (no errors) but I can't find xmlrpc-c and xmlrpc-c-devel to install.

and every time I try to run opensips, I get:

ERROR:core:sr_load_module: could not open module <mi_xmlrpc.so>: mi_xmlrpc.so: cannot open shared object file: No such file or directory

and really mi_xmlrpc.so is there in the modules folder!!!

If you have any other suggestion, please let me know.

Thanks a lot again!

[bogdan] - check if your cfg file is pointing to the right directory for loading the modules

Alp?06 August 2009, 16:49

Hello, I am getting this error while initializing LB module. I couldnt find anything wrong why its getting me this error.

Aug 6 17:44:49 ubuntu /sbin/opensips[7696]: INFO:load_balancer:mod_init: Load-Balancer module - initializing Aug 6 17:44:49 ubuntu /sbin/opensips[7696]: ERROR:load_balancer:mod_init: Can't load dialog hooks Aug 6 17:44:49 ubuntu /sbin/opensips[7696]: ERROR:core:init_mod: failed to initialize module load_balancer Aug 6 17:44:49 ubuntu /sbin/opensips[7696]: ERROR:core:main: error while initializing modules

Alp?06 August 2009, 17:32

I added module Dialog.so and add modparam("dialog", "dlg_flag", 4) in opensips.cfg file and it works!!

Alp?06 August 2009, 18:04

There is one more bracket ')' in line if (!search_body("G711") )) {. It gives error.

[bogdan] - fixed, thank you

motto?08 August 2009, 00:52

Hey,

 default load_balance configuration doesn't process "ACK" SIP packet from the Asterisk END, so the Asterisk after SIP ACK retransmiting several times hangup a call.
 Any idea on that ?

[bogdan] - the ACK (if properly formed by caller device) should contain the Route header and OpenSIPS will route it as sequential request (via loose_route() )

ravi?14 August 2009, 17:04

is there anyway, loadbalancer can get destination ip address dynamically. Ex: i have 3 asterisk servers to be load balanced but some time i want to upgrade one asterisk server that case i want to remove that asterisk server ip address from load balancer database is that possible?

[bogdan] - update the IPs in DB and do via MI a "lb_reload"

Javier?18 August 2009, 15:24

Hi Bogdan,

Thank you for your suggestion "Javier, check if your cfg file is pointing to the right directory for loading the modules." Really, the opensips.cfg is pointing properly to the modules directory.

Any other suggestion ?

Thank you,

Javier.

[bogdan] - check if the mi_xmlrpc.so file really exists in the directory you configured

Ricardo?18 August 2009, 20:27

Hi Bogdan. Is possible to mix the load_balancer module with the LCR module?... i need the features from the LCR to route the calls, but also i need to know the status of a GW with E1/T1 and balance the load between several Gw's. Is this possible? Thanks

Ricardo.-

[bogdan] - that is a bit tricky as you have two engines for doing routing (but on different criteria) DR - prefix routing, LB - load routing; only if you chain them, like first DR to select a set of GWs(per prefix) and after that LB to choose one of the GWs from the DR set

ravi?26 August 2009, 17:47

Hi Bogdan, is possible to assign variable in load_balance("$var(a0)","vm") or load_balance("$avp(i:55)","vm") function?

Ravi

[bogdan] - starting with 1.6 yes

Raj?30 October 2009, 10:14

Hi Bogdam, I am using OpenSIPSv1.5.3 on centOS. If I load load_balancer.so the server start is failed. loadmodule "modules/load_balancer/load_balancer.so" modparam("load_balancer", "db_url","mysql://opensips:opensipsw@localhost/opensips"), I have created opensips user and password in db. Please suggest me with a solution if possible.

Raj

[bogdan] - see http://www.opensips.org/Resources/DocsTsStart ; it must be an error reported

JG?12 November 2009, 21:41

I just installed OpenSips but i just don't understand the configuration part for the LB, i just want to do something like this:

prefix 044 redirect to ZAP Channel 37 and ZAP Channel 38

One call to 37 and the next to 38 and if both are busy to the default unicall

Can you guys help me out...

Thanks in advanced

JG

[bogdan] - post your question on the user mailing list

LouisFox?11 March 2010, 09:10

I have a PSTN VoIP Gateway that is sending and receiving VoIP SIP calls to a SIP Gateway on the internet. I have 3 WAN connections. Can I use the load balancer to do call load balancing between the VoIP Gateway and the SIP Proxy. Can the load balancer distribute the VoIP load between the 3 connections equally and then also to receive and forward calls from the SIP Gateway to the VoIP PSTN Gateway. It is a single Internet SIP Proxy Service. 8 Calls per link

16 July 2010, 19:49

is there a way to load balance based on calls per second (CPS) instead of total concurrent call? i have two sip carrier with 50 cps each and my auto dealers are dialing @ 60 calls per second.

Anh Ha?22 July 2010, 08:43

In the Load_balancer tables :

 4  	 2  	 sip:192.168.1.1  	 pstn=300  	 0  	 VNP #1

In opensips.cfg:

if (uri=~"^sip:.....9[0-2]*@")

     {
   xlog("route to Vinaphone");
          load_balance("2","pstn");
        # route(4);
     }

when i call 99984917xxxx , it matches the condition but it doesn't route the call to 192.168.1.1 If i have "route(4);" bellow "load_balance("2","pstn");", then it routes follow route(4)

Please help me Tks

Add Comment 
Sign as Author 
Enter code 522

Your VoIP Account

News

OpenSIPS LiveDVD

04th of August 2010 OpenSIPS Virtual Machine is now available ...
Read more...

OpenSIPS 1.6.3

02nd of August 2010 OpenSIPS 1.6.3 major release gets better...
Read more...

OpenSIPS @ ClueCon

29th of July 2010 OpenSIPS 2.0 @ ClueCon 2010.
Read more...

OpenSIPS eBootcamp

12th of July 2010 Remote OpenSIPS learning with Ebootcamp program.
Read more...

OpenSIPS @ Amoocon 2010

4th of May 2010 OpenSIPS had 2 papers at Amoocon 2010.
Read more...

SIMPLE Aggregation

14th of April 2010 Presence and BLF state aggregation.
Read more...

OpenSIPS Certified Professional

13th of April 2010 OpenSIPS certification program launched.
Read more...

OpenSIPS webinar

30th of March 2010 Next webinars is Variables in OpenSIPS scripting
Read more...

OpenSIPS 1.6.2

11th of March 2010 OpenSIPS 1.6.2 is brings new features...
Read more...

OpenSIPS Control Panel 4.0

08th of March 2010 OpenSIPS CP 4.0 comes with user provisioning...
Read more...

Conference on "OpenSIPS 2.0"

5th of March 2010 VoIP Users Conference will host an audio conference
Read more...

OpenSIPS webinar

25th of February 2010 Next webinars is Explaining the default script
Read more...

OpenSIPS 2.0 Design

15th of February 2010 Design of OpenSIPS 2.0 is unveiled
Read more...

OpenSIPS webinar

28th of January 2010 Next OpenSIPS webinars is SIP Introduction
Read more...

Building Telephony Systems with OpenSIPS 1.6

21st of January 2010 New edition is available...
Read more...

OpenSIPS 1.6.1 is released

21st of December 2009 OpenSIPS 1.6.1 minor release is out...
Read more...

OpenSIPS Development Course

17th of December 2009 OpenSIPS Devel Course for 2010...
Read more...

OpenSIPS Bootcamps 2010

09th of December 2009 2010 Schedule for Bootcamp events...
Read more...

User Location is faster

13th of November 2009 USRLOC is 3 time faster than before...
Read more...

OpenSIPS Control Panel 3.0

30th of October 2009 OpenSIPS CP 3.0 major release is out...
Read more...

OpenSIPS 1.6.0 is released

16th of October 2009 OpenSIPS 1.6.0 major release is out...
Read more...

OpenSIPS VoIP Service

21th of September 2009 OpenSIPS project offers free VoIP services...
Read more...

OpenSIPS & Astricon

21th of September 2009 OpenSIPS talks and exhibits at Astricon 2009...
Read more...

SVN freeze

17th of September 2009 SVN trunk gets frozen to prepare 1.6 release...
Read more...

New types of script routes

10th of September 2009 New additions to configuration file routes...
Read more...

STUN server

7th of September 2009 OpenSIPS has now a built-in STUN server...
Read more...

Pseudovariable implementation extended

3rd of September 2009 Added new operations for pvars to give more power to the script writer...
Read more...

OpenSIPS Asterisk Integration

30th of August 2009 Tutorial for realtime integration...
Read more...

OpenSIPS 1.5.3 is released

27th of August 2009 OpenSIPS 1.5.3 minor release is out...
Read more...

AAA and RADIUS support

18th of April 2009 New AAA API and RADIUS enhancements in OpenSIPS
Read more...

OpenSIPS webinar

4th of April 2009 Next OpenSIPS webinars is Types of Routs in OpenSIPS
Read more...

B2BUA

3rd of August 2009 A B2BUA signaling implementation in OpenSIPS
Read more...

DB virtual

23th of July 2009 A DB conn mixer for failover, parallel and LB
Read more...

Codec manipulation

23th of July 2009 SDP codecs and priorities manipulation
Read more...

Memcached interfacing

16th of July 2009 memcached support for memory caching API
Read more...

OpenSIPS 1.5.2 is released

15th of July 2009 OpenSIPS 1.5.2 minor release is out...
Read more...

OpenSIPS@ClueCon

14th of July 2009 OpenSIPS talks at ClueCon
Read more...

OpenSIPS webinar

30th of June 2009 Next OpenSIPS webinars is Routing in SIP
Read more...

REGISTRAR enhancements

29th of June 2009 REGISTRAR module becomes more flexible
Read more...

OpenSIPS free webinars

01st of June 2009 OpenSIPS webinars program was launched
Read more...

OpenSIPS-CP 2.0 is released

13rd of April 2009 OpenSIPS Control Panel 2.0 major release is out...
Read more...

OpenSIPS 1.5.1 is released

13rd of April 2009 OpenSIPS 1.5.1 minor release is out...
Read more...

OpenSIPS 1.5.0 is released

23rd of March 2009 OpenSIPS 1.5.0 major release is out...
Read more...

OpenSIPS 1.4.5 is released

23rd of March 2009 OpenSIPS 1.4.5 minor release is out...
Read more...

OpenSIPS as Load Balancer

4th of March 2009
How to do real Load-Balancing for media servers ...
Read more...

OpenSIPS at Amoocon (AsteriskTAG)

24th of February 2009
3 hot topics presented at Amoocon ...
Read more...


Edit | History | Print | Recent Changes | Search
Page last modified on July 22, 2010, at 08:43 AM