Skip to content

Kamailio Round Robin Load Balancing: Dispatcher Guide

Kamailio round robin load balancing with the dispatcher module: algorithm 4 setup, weight 9 vs relative weight 11, failover, and a sngrep call flow.

By · Updated · Published

Photo of server rack with network cables
Photo by br1dotcom · CC BY 2.0 · via Flickr

How do you set up round robin load balancing in Kamailio?

Photo of wordpress, blogging, writing
Photo by StockSnap · Pixabay Content License · via Pixabay

Load the dispatcher module, list your gateways under one set id, and call ds_select_dst("1", "4") before you relay. Algorithm "4" is round-robin: each new request goes to the next destination in the set. The module is loaded with loadmodule "dispatcher.so" (Kamailio dispatcher module documentation).

In kamailio.cfg:

Kamailio config
loadmodule "dispatcher.so"

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

The list_file parameter sets the path to the file with the destination sets. The docs give two default paths, /etc/kamailio/dispatcher.list and /usr/local/etc/kamailio/dispatcher.list.

In dispatcher.list:

Output
1 sip:192.0.2.11:5060 0 0
1 sip:192.0.2.12:5060 0 0
1 sip:192.0.2.13:5060 0 0

Each line has the columns setid, destination, flags, priority and attrs. These three lines put three example gateways in set 1 with flags 0 and priority 0. The addresses come from a documentation range and are examples only.

The docs' sample route for round robin looks like this:

Kamailio config
route[DISPATCH] {
    if (!ds_select_dst("1", "4")) {
        send_reply("404", "No destination");
        exit;
    }
    t_on_failure("RTF_DISPATCH");
    route(RELAY);
}

The ds_select_dst call picks a destination from set 1 with algorithm 4. If it picks none, the route replies 404 "No destination" and exits. Otherwise it sets the failure route with t_on_failure("RTF_DISPATCH") and relays with route(RELAY). The failure route is covered in the failover section below.

Which dispatcher algorithm should you use: round robin, weight or relative weight?

This is the comparison we compiled on November 15, 2026 from the official Kamailio dispatcher documentation. Nothing here was run or measured. The table covers the algorithm values the docs describe for ds_select_dst(set, alg[, limit]).

ValueWhat the docs sayAttribute neededReacts when a gateway goes inactive
4Round-robin: next destinationNoneNot stated
8Sorted by priority; serial forking ordered by prioritypriorityNot stated
9Weight based load distributionweight, integer 1 to 100Not stated
11Relative weight based load distributionrweight, integer 1 to 100Yes: probabilities are recalculated
10Call load: first destination is the one with the fewest calls; INVITE requests onlyunique duid, optional maxload, ds_hash_size above 0Not stated; dispatcher.reload is disabled
13Latency optimized round-robin that favors the highest prioritypriorityn/a
14Round-robin if all destinations share one priority above 0, otherwise serial dispatching (algorithm 8)priorityn/a
64Round-robin with overload controlocrate, ocminn/a

The docs list other values too. Value 0 is a hash over Call-ID, 1 is From-URI, 2 is To-URI and 3 is the Request-URI user. Value 5 is the authorization username (round-robin if there is none) and 6 is random. Value 7 is a hash over PVs (needs hash_pvar), and 12 is parallel forking to the whole set. Note that value 0 is a Call-ID hash, not round-robin.

How does weight based load balancing work in Kamailio (algorithm 9)?

Call ds_select_dst with algorithm "9" and give every gateway a weight attribute. The weight is an integer from 1 to 100. The docs describe it as the percent of calls sent to that gateway (Kamailio dispatcher module documentation).

The rules, from the docs:

  1. A weight must be a positive integer from 1 to 100. An invalid value sets the weight to 0, and that destination is ignored.
  2. The weights in a set must not add up to more than 100. Destinations whose weight pushes the sum over 100 are ignored.
  3. If the sum is under 100, the last destination takes the missing percentage.

The docs do not state how algorithm 9 reacts when a gateway goes inactive. Algorithm 11 is the one that recalculates, as the next sections show.

How do you set the Kamailio dispatcher weight on a gateway?

Weights go in the attrs column, the fifth field of a dispatcher.list line, as name=value pairs separated by semicolons. The column order is setid, destination, flags, priority, attrs. A list file that splits calls evenly between two gateways looks like this:

Output
1 sip:192.0.2.11:5060 0 0 weight=50
1 sip:192.0.2.12:5060 0 0 weight=50

Each gateway gets half of the calls, and the sum is exactly 100, so no destination is ignored. After you edit the file, reload the sets and check them:

Shell
kamcmd dispatcher.reload
kamcmd dispatcher.list

dispatcher.reload reloads the destination sets. dispatcher.list lists the routing groups with their destination addresses and attributes, and its FLAGS column shows two letters per destination.

What is Kamailio relative weight load balancing (algorithm 11)?

Algorithm "11" uses the rweight attribute, an integer from 1 to 100, as a ratio rather than a percentage. A destination's chance is its rweight divided by the sum of the rweights of all active destinations in the group (Kamailio dispatcher module documentation).

The docs give this example: 100 calls to three destinations with rweight 1, 2 and 1 are split 25, 50 and 25. If the third destination becomes inactive, the split becomes 33, 67 and 0.

DestinationrweightShare, all activeShare, third inactive
First125%33%
Second250%67%
Third125%0%

The docs say percentages are truncated, and the last destination takes the remainder up to 100. The main difference from algorithm 9 is that probabilities are recalculated when destinations become active or inactive.

The docs also say that with cc=1, weight sets congestion tolerance as well, and they recommend rweight 50. A matching attrs value looks like rweight=50;weight=50;cc=1;.

How do you add failover to the Kamailio dispatcher?

Set the dispatcher flags parameter to 2, call t_on_failure("RTF_DISPATCH") before relaying, and call ds_next_dst() in the failure route. Flag 2 enables failover support, so the rest of the set is stored for ds_next_dst(). The default for flags is 0.

Kamailio config
modparam("dispatcher", "flags", 2)
modparam("dispatcher", "ds_ping_interval", 60)

The docs' sample config uses these values. The ping interval defaults to 0, which turns pinging off. Pinging is used only when the TM module is loaded. The failure route below is from the docs' sample:

Kamailio config
failure_route[RTF_DISPATCH] {
    if (t_is_canceled()) {
        exit;
    }
    if (t_check_status("500") or (t_branch_timeout() and !t_branch_replied())) {
        if (ds_next_dst()) {
            t_on_failure("RTF_DISPATCH");
            route(RELAY);
        }
    }
}

The route stops if the transaction was canceled. It tries the next destination only on a 500 reply or a branch that timed out without a reply. If ds_next_dst() finds a destination, the route sets the failure route again and relays.

Gateways are brought back by probing. These parameters control it:

  • ds_ping_method sets the SIP method used to probe gateways. The default is OPTIONS.
  • ds_probing_threshold sets how many failed requests change a gateway from active to inactive. The default is 1.
  • ds_inactive_threshold sets how many successful requests change a gateway from inactive to active. The default is 1.
  • ds_probing_mode set to 0 (the default) tests only gateways in the PROBING state. Set to 1, it tests all gateways.

To take a destination out by hand, ds_mark_dst("i") marks the last used destination inactive, so it is ignored in future requests.

What does a round robin call look like in sngrep?

Call flow: the caller sends INVITE to Kamailio, Kamailio sends 100 Trying back and relays the INVITE to Gateway 1. 180 Ringing and 200 OK travel from Gateway 1 back through Kamailio to the caller. The next call's INVITE goes to Gateway 2.
A round robin call through Kamailio, as sngrep shows it

Follow one call host by host. The message order below is the one the SIP specification describes for a call through a proxy.

  1. The caller sends an INVITE to Kamailio.
  2. Kamailio sends 100 Trying back to the caller.
  3. Kamailio relays the INVITE to the next gateway in the set.
  4. The gateway sends 180 Ringing, which travels back through Kamailio to the caller.
  5. The gateway sends 200 OK when the call is answered, and it travels back through Kamailio to the caller.
  6. The caller sends an ACK to confirm the 200 OK.
  7. One side sends a BYE, and the other side answers with 200 OK.

The next call's INVITE goes to the next gateway in the set, which is what round-robin means.

Advanced examples: what do full dispatcher configurations look like?

The first example combines round robin with failover and pinging. It uses the values from the docs' sample config. Pair it with the route[DISPATCH] and failure_route[RTF_DISPATCH] blocks shown above.

Kamailio config
# Example 1: round robin (algorithm 4) with failover and pinging
loadmodule "dispatcher.so"
modparam("dispatcher", "list_file", "/etc/kamailio/dispatcher.list")
modparam("dispatcher", "flags", 2)
modparam("dispatcher", "ds_ping_interval", 60)
modparam("dispatcher", "ds_ping_from", "sip:proxy@kamailio.org")
modparam("dispatcher", "ds_probing_mode", 1)
modparam("dispatcher", "ds_probing_threshold", 10)
modparam("dispatcher", "ds_inactive_threshold", 10)

The second example is a three-way weight split with algorithm 9. The IPs are examples. In dispatcher.list:

Output
1 sip:192.0.2.11:5060 0 0 weight=50
1 sip:192.0.2.12:5060 0 0 weight=30
1 sip:192.0.2.13:5060 0 0 weight=20

In kamailio.cfg:

Kamailio config
# Example 2: weight based split (algorithm 9)
route[DISPATCH_W] {
    if (!ds_select_dst("1", "9")) {
        send_reply("404", "No destination");
        exit;
    }
    t_on_failure("RTF_DISPATCH");
    route(RELAY);
}

The three weights add up to exactly 100, so no destination is ignored and none has to take a missing percentage.

The third example uses relative weight with the limit argument. The IPs are examples. In dispatcher.list, with the docs' 1, 2, 1 rweight example:

Output
1 sip:192.0.2.21:5060 0 0 rweight=1
1 sip:192.0.2.22:5060 0 0 rweight=2
1 sip:192.0.2.23:5060 0 0 rweight=1

In kamailio.cfg:

Kamailio config
# Example 3: relative weight (algorithm 11), failover kept for up to 3 destinations
route[DISPATCH_RW] {
    if (!ds_select_dst("1", "11", "3")) {
        send_reply("404", "No destination");
        exit;
    }
    t_on_failure("RTF_DISPATCH");
    route(RELAY);
}

The limit argument 3 caps how many destinations are stored for failover. The split follows the 25, 50 and 25 example while all three gateways are active. Failover in this route needs flags set to 2, as in the first example.

More guides in Open-Source PBX and SIP Servers.

Frequently asked questions

What happens to the traffic split when a gateway goes down?
With algorithm 11, the docs say probabilities are recalculated when a destination becomes inactive. The 1, 2, 1 example moves from 25, 50 and 25 to 33, 67 and 0. For algorithm 9, the docs do not say how the split reacts, so do not assume it does.
How do I check which gateways are active and reload the list?
Run `kamcmd dispatcher.list` to see each destination and its attributes. Run `kamcmd dispatcher.reload` to reload the sets. The reload command is disabled for algorithm 10.
How does Kamailio detect that a gateway is down and bring it back?
Failed and successful requests are counted against two thresholds, ds_probing_threshold and ds_inactive_threshold, and both default to 1. Pinging is off until ds_ping_interval is set above 0, and the docs' sample uses 60.

Sources

  1. The Kamailio dispatcher module is a SIP load balancer and traffic dispatcher. (www.kamailio.org)
  2. A SIP call starts with the caller sending an INVITE request. (www.rfc-editor.org)