DevSnippets

Home » Kamailio » Load balancing between multiple servers in Kamailio

Load balancing between multiple servers in Kamailio

Kamailio Configure load balancing between multiple SIP servers 5 views

Code

Step 1: Enable Dispatcher Module
Edit /etc/kamailio/kamailio.cfg and include:

loadmodule "dispatcher.so"
modparam("dispatcher", "db_url", "mysql://kamailio:password@localhost/kamailio")
modparam("dispatcher", "table_name", "dispatcher")
modparam("dispatcher", "flags", 2)  # Round-robin with re-check on failure

Step 2: Define SIP Server Destinations
Use MySQL table dispatcher:

INSERT INTO dispatcher (setid, destination, flags, priority, attrs, description) VALUES
(1, 'sip:192.168.1.101:5060', 0, 0, '', 'SIP Server 1'),
(1, 'sip:192.168.1.102:5060', 0, 0, '', 'SIP Server 2');

Or, for file-based setup, define in kamailio.cfg:

modparam("dispatcher", "list_file", "/etc/kamailio/dispatcher.list")
And create /etc/kamailio/dispatcher.list:

1 sip:192.168.1.101:5060
1 sip:192.168.1.102:5060

Step 2: Modify Request Routing in kamailio.cfg
Add dispatcher routing logic:


route {
    if (is_method("INVITE")) {
        if (!ds_select_dst("1", "4")) {
            send_reply("500", "No destination");
            exit;
        }
        xlog("L_INFO", "Routing to: $ru via dispatcher\n");
        t_relay();
        exit;
    }
}

🛡 Bonus: Health Checking
Kamailio can automatically disable dead SIP servers:

modparam("dispatcher", "ds_ping_interval", 30)
modparam("dispatcher", "ds_ping_from", "sip:ping@yourdomain.com")
modparam("dispatcher", "ds_ping_method", "OPTIONS")

Explanation

To configure load balancing between multiple SIP servers, you can use a SIP proxy/load balancer like Kamailio, OpenSIPS, or FreeSWITCH (with some scripting), or even hardware/load balancer software (e.g., HAProxy or NGINX with RTPEngine for RTP).

Here’s how you can do it step-by-step with Kamailio — one of the most robust open-source SIP proxies/load balancers:

Rate this snippet

Click to rate

Have a Better Solution?

Know a more efficient way to solve this problem? Share your own code snippet and help the community!

Submit Your Version