Kamailio Bytes – Gotchas with Kamailio as an Asterisk Load Balancer

How do I make Kamailio work with Asterisk?

It’s a seemingly simple question, the answer to which is – however you want, sorry if that’s not a simple answer.

I’ve talked about the strengths and weaknesses of Kamailio and Asterisk in my post Kamailio vs Asterisk, so how about we use them to work together?

The State of Play

So before we go into the nitty gritty, let’s imagine we’ve got an Asterisk box with a call queue with Alice and Bob in it, set to ring those users if they’re not already on a call.

Each time a call comes in, Asterisk looks at who in the queue is not already on a call, and rings their phone.

Now let’s imagine we’re facing a scenario where the single Asterisk box we’ve got is struggling, and we want to add a second to share the load.

We do it, and now we’ve got two Asterisk boxes and a Kamailio load balancer to split the traffic between the two boxes.

Now each time a call comes in, Kamailio sends the SIP INVITE to one of the two Asterisk boxes, and when it does, that Asterisk box looks at who is in the queue and not already on a call, and then rings their phone.

Let’s imagine a scenario where a Alice & Bob are both on calls on Asterisk box A, and another call comes in this call is routed to Asterisk box B. Asterisk box B looks at who is in the queue and who is already on a call, the problem is Alice and Bob are on calls on Asterisk box A, so Asterisk box B doesn’t know they’re both on a call and tries to ring them.

We have a problem.

Scaling stateful apps is a real headache,

So have a good long hard think about how to handle these issues before going down this path!

8 thoughts on “Kamailio Bytes – Gotchas with Kamailio as an Asterisk Load Balancer

  1. Saving state is necessary in such case. It should be fast. The answer (usually) is Redis. Another option could be changing Asterisk to Freeswitch, which performs slightly better and could save of the need to put second server online.

  2. Well, an idea in multitenant environments is to deliver all traffic from a particular tenant to a single asterisk box, and also forward the dids to that box. So every customer has is own tenantID and uses this as sip account. You can then easily split traffic in Kamailio per tenant to multiple boxes. Unless you have a really large client with more 1000 extensions.

    1. Hey Bram,
      The problem with just using Asterisk to distribute load for different tenants to a respective Asterisk box is it doesn’t scale super well as you mentioned – I find in the past I didn’t even get near 1000 extensions if they were heavy users like Contact Center agents.

  3. We can use kamailio as SIP registrar. All agents are registered on Kamailio.
    When a call comes in eventually it goes to kamailio and kamailio know the every agent location.

    I had a same setup and its was working with no issue.

    1. Hi Renah,
      Yes you can do this with Kamailio!
      You will find the call distribution other than broadcast (for example longest idle), won’t be aware of who’s longest idle, as Asterisk can’t share this information between instances.

      1. We use your solution for our call center, thanks for this blog by the way, the agents or users are register in kamailio and logged as “agents” in the asterisk in with a call that’s get “dispatched “ and the calls is distributed using an agi. We create a private acd for this agents. , can be done with conferences too. But we believe acd is cheaper in rss.

Leave a Reply to Nick Cancel reply

Your email address will not be published. Required fields are marked *