Load balancing between 4 SIP gateways

Hi Guys,

Kinda new here so i will try to explain what i am trying to achieve.

I have a Voip server that will SIP register to the Asterisk.
That asterisk device will have 4 gateways to forward the calls to.

I want that every time the voip will send a call to the asterisk, a round robin load balancing will occur.

First call, if it fails, it will jump to gateway2, if that fails gateway 3 and so on (after the 4th, it should return to 1).

If the first call was success, than the second call should go directly to 2, and not 1.
If the second call was success, than the third call sh ould go directly to 3, and not 2.
and so on.

I know i can set a global variable, and will have +1 on each call, and when that variable = 4 it should reset it back to 1.

Hope you could understand what i meant, my knowledge here is very limited.


What you want is achievable using dialplan and some global variables (or Asterisk’s internal DB).
Do you see any issues in what you have done so far?

Thank you Satish.

I haven’t done much yet.

Until now i worked with simple dialing plan, nothing too complicated - but what i’m trying to do now i think is totally different.

I have made some readings, and i understand that i need a [Globals] Variable, that every incoming call the dialing plan will add +1 to it, and a GotoIf that when it reaches 5, it will reset to 0.

I will use that variable as my dialing extension.

This is the idea but i’m not sure how to do it.
Also, is the variables under [Globals] will reset each call? if so - that’s not good.

Hopefully this will help you understand more my idea:

Voip sending first call to PBX, PBX sends the call to “Extension1” and add +1 to the variable
so next call will be sent to “Extension2” (and again, add +1 for the 3rd call).
Extensions cannot reach 5, as i have only 4. so a GotoIf extensions = 5 - reset back to 0.

As i said i’m not very good with Asterisk, but this is the way i think it should work.
if you know better way - please.
If this is the right way, i will be glad to get some help with the syntax.



Yes you are on a right track. However try to think think of some edge cases before you implement your solution. i.e. where you’ll have multiple calls in system

While you can certainly set this up in the dialplan as @satish4asterisk is describing, you can also rely on DNS SRV records to round-robin between outbound destinations. In recent versions of Asterisk, Asterisk’s PJSIP stack supports DNS SRV records and will properly use the returned SRV records when making outbound calls. This obviously makes the assumption that you control the DNS records for the SIP gateways you’re describing, but if that’s the case, then it certainly makes the dialplan simpler.


For my point of view that’s the best solution to manage it with DNS SRV with PJSIP as described by @mjordan. You can simply install on your Asterisk server “bind” application and resolve DNS locally, just create a new zone within bind folder and add inside DNS SRV entries for the protocol that you are using tcp / udp… and add all servers with weight for each and you’ll a great load balancing solution. You can add DNS SRV on Windows DNS server too.

Below an exemple on how to resolve it locally:

Within named.conf:

zone “mysipdomain.local” {
type master;
file “/etc/bind/mysipdomain.local.zone”;

within mysipdomain.local.zone file

$TTL 1800
@ IN SOA ns1.mysipdomain.local. root.mysipdomain.local. (
2016101813 ; serial#
7200 ; refresh, seconds
120 ; retry, seconds
14400 ; expire, seconds
1800 ) ; minimum TTL, seconds

; DNS Servers for ‘mysipdomain.local’

mysipdomain.local. IN NS FIRSTSIPSERVER
mysipdomain.local. IN NS SECONDSIPSERVER

; Call Routing for SIP domain ‘mysipdomain.local’

; NAPTR record for SIP TCP mysipdomain.local
; priority: 2 weight: 0
; protocol: “SIP+D2T” regex: “” uri: _sip._tcp.mysipdomain.local
mysipdomain.local. IN NAPTR 2 0 “s” “SIP+D2T” “” _sip._tcp.mysipdomain.local.

; NAPTR record for SIP UDP mysipdomain.local
; priority: 2 weight: 0
; protocol: “SIP+D2U” regex: “” uri: _sip._udp.mysipdomain.local
mysipdomain.local. IN NAPTR 2 0 “s” “SIP+D2U” “” _sip._udp.mysipdomain.local.

_sip._tcp.mysipdomain.local. IN SRV 1 50 5060 FIRSTSIPSERVER
_sip._tcp.mysipdomain.local. IN SRV 1 50 5060 SECONDSIPSERVER

_sip._udp.mysipdomain.local. IN SRV 1 50 5060 FIRSTSIPSERVER
_sip._udp.mysipdomain.local. IN SRV 1 50 5060 SECONDSIPSERVER

;IP Addresses

; A record for your first SIP server

; A record for your second SIP server

Within /etc/resolv.conf add an entry like below:


Just replace name and IP in example above and it should to the trick.

Good luck !