Skip to content

Kamailio Dispatcher Load Balancing: 2027 Setup Guide

Set up Kamailio dispatcher load balancing with algorithms, dispatcher.list, failover, health checks and kamcmd commands explained step by step.

By · Updated · Published

Photo of brocade, cable, cables
Photo by Jemimus · CC BY 2.0 · via Flickr

What does the Kamailio dispatcher module do?

The Kamailio dispatcher module provides SIP load balancing. Its dispatcher module documentation describes it as SIP load balancer functionality that can be used as a SIP traffic dispatcher.

You group destinations into sets, such as several Asterisk servers. The routing script asks the module to pick one destination for each request. The algorithm you choose decides which one. Each set has a numeric ID, and the script refers to that ID.

The module offers two selection functions, ds_select_dst and ds_select_domain. This guide uses ds_select_dst in its examples. The FAQ at the end explains how the two differ.

How do you write the dispatcher.list file?

The list file holds one destination per line. Each line has a set ID, a destination SIP URI, and optional flags, priority and attributes, in that order. The set ID and the destination are required. In this two-line example, pbx1.example.com and pbx2.example.com are example hosts:

Output
1 sip:pbx1.example.com:5080 0 0 duid=abc;maxload=20
1 sip:pbx2.example.com:5080 0 0 duid=xyz;maxload=20

Read the first line from left to right. The 1 is the set ID, so this destination belongs to set 1. The sip:pbx1.example.com:5080 part is the destination. The first 0 is the flags field, and the second 0 is the priority. The last field holds the attributes, separated by semicolons.

In the attributes, duid=abc gives this address a unique ID, and maxload=20 sets a load limit. A maxload of 0 means no limit.

The module reads the file named by the list_file parameter. If you do not set it, the defaults are /etc/kamailio/dispatcher.list or /usr/local/etc/kamailio/dispatcher.list. The module can also load the list from a database table, which the FAQ covers.

The flags field takes these values:

Flag valueMeaning
1Inactive
2Trying
4Admin disabled
8Probing
16Skip DNS at startup
32No ping

The attributes field takes these names:

AttributeMeaning
weightShare for algorithm 9, a whole number from 1 to 100 inclusive
rweightShare for algorithm 11, from 1 to 100
duidUnique string ID for each address, needed by algorithm 10
maxloadLoad limit, where 0 means no limit

The docs also list socket, sockname, ping_from, obproxy and latency as attributes.

Which dispatcher algorithm should you pick?

Round-robin (algorithm 4) is the simplest choice. Use it when you want calls spread evenly and do not need a caller tied to one server. Hash algorithms (0 to 3, and 5) pick a destination from a value in the request, so the same value tends to reach the same server.

Weight (9) and relative weight (11) split calls by share. Call load (10) spreads calls by load, and the reference marks it as INVITE only.

How do the algorithm IDs compare in our reference?

Hands typing on a laptop with a spreadsheet on screen
Photo by Bluestonex · Unsplash License · via Unsplash

We compiled this side-by-side reference from the official dispatcher module documentation, checked November 14, 2026. The page names no release number. Nothing was run or tested for this guide, and every row comes from that page. The number is the second argument of ds_select_dst.

IDAlgorithm
0Hash over Call-ID
1Hash over From URI
2Hash over To URI
3Hash over Request-URI user
4Round-robin, next destination for each call
5Hash over authorization username (round-robin if none)
6Random destination
8Serial forking, sorted by the priority attribute
9Weight, needs the weight attribute
10Call load, needs a duid attribute on each address; INVITE only
11Relative weight, needs the rweight attribute; cc=1 turns on congestion control
12Parallel forking to all destinations
13Latency-optimized
14Round-robin if equal priorities are above 0, otherwise serial
64Round-robin with overload control

An unknown ID selects the first entry, so check the number before you deploy.

How does weight-based load balancing work in the dispatcher module?

Weight-based balancing (algorithm 9) gives each destination a share set by its weight attribute. The weight must be a whole number from 1 to 100, inclusive. Algorithm 11 uses the rweight attribute instead, which also takes values from 1 to 100. The reference adds cc=1 for congestion control on that algorithm.

Output
1 sip:pbx1.example.com:5080 0 0 weight=100
1 sip:pbx2.example.com:5080 0 0 weight=1

The first line gives pbx1 a weight of 100, and the second gives pbx2 a weight of 1. These values sit at the two ends of the allowed range, so they only show the syntax. Choose your own values. The advanced examples at the end show how to call algorithm 9 with a list like this.

How do you configure kamailio.cfg for round-robin load balancing?

Flow chart of four steps for round-robin load balancing: load the dispatcher module and set its list file, write dispatcher.list with at least one line per destination, add a route that calls ds_select_dst with set 1 and algorithm 4, then start Kamailio and check the groups with kamcmd.
Round-robin setup in kamailio.cfg, step by step

Round-robin uses algorithm 4. Follow these steps.

  1. Load the dispatcher module in your configuration, then set its list file.
  2. Write dispatcher.list with at least one line per destination.
  3. Add a route that calls ds_select_dst with set 1 and algorithm 4.
  4. Start Kamailio and check the groups with kamcmd, as shown later in this guide.

Set the list file in kamailio.cfg:

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

The first argument of modparam is the module name, dispatcher. The second is the parameter name, list_file. The third is the path to the list file, which holds the destination sets.

Next, the routing logic:

Kamailio config
route[DISPATCH] {
    if(ds_select_dst("1", "4")) {
        route(RELAY);
    }
}

The ds_select_dst call picks a destination from set 1 with algorithm 4, which is round-robin. The first argument is the set ID as a string, and the second is the algorithm. The call sets the destination URI and leaves the Request-URI unchanged.

If a destination is found, the if block calls route(RELAY). The docs sample handles the other case with a 404 reply that says No destination, and you can add that branch to the same route. The full sample is on the docs page linked in the first section.

The RELAY route forwards the request with t_relay():

Kamailio config
route[RELAY] {
    if(!t_relay()) {
        sl_reply_error();
    }
}

If the relay fails, sl_reply_error() sends an error reply. Each new call takes the next destination in the set, so traffic spreads over the whole set.

How does failover to the next server work?

Failover needs three things: the flags parameter set to 2, the XAVP that holds the candidate list, and a failure route. Setting flags to 2 enables failover support, and the default is 0.

Kamailio config
modparam("dispatcher", "flags", 2)
modparam("dispatcher", "xavp_dst", "_dsdst_")

The xavp_dst parameter names the XAVP that holds the list of addresses and their properties. Its default is dsdst, so the second line repeats the default and makes the name visible in the file.

Next, the DISPATCH route arms the failure route before it relays:

Kamailio config
route[DISPATCH] {
    if(ds_select_dst("1", "4")) {
        t_on_failure("RTF_DISPATCH");
        route(RELAY);
    }
}

The t_on_failure call names the failure route, RTF_DISPATCH. The failure route then decides whether to try the next address:

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

The first condition checks that the caller has not canceled. The second matches only a 500 reply or a local timeout with no reply on that branch. For those cases, ds_next_dst() takes the next address from the XAVP and sets the destination URI. The route then arms t_on_failure again, so a further failure gets the same treatment, and relays the request.

Other failure codes are not retried by this route. To take a failed destination out of rotation, ds_mark_dst() marks it. With no state given, it marks the destination inactive.

How does the dispatcher module detect dead servers?

Health checks send OPTIONS pings. Set ds_ping_interval above 0 to turn them on, because the default is 0, which disables them. The docs example uses 30. The reference notes that this setting needs the tm module.

Kamailio config
modparam("dispatcher", "ds_ping_interval", 30)
modparam("dispatcher", "ds_ping_method", "OPTIONS")
modparam("dispatcher", "ds_ping_from", "sip:dispatcher@localhost")
modparam("dispatcher", "ds_probing_mode", 1)
modparam("dispatcher", "ds_probing_threshold", 1)
modparam("dispatcher", "ds_ping_reply_codes", "class=2;code=403")

The ds_ping_method value is OPTIONS, which is also the default, so the line only makes it explicit. The ds_ping_from value sets the From address for the pings. The address sip:dispatcher@localhost is a placeholder.

ParameterDefaultWhat it does
ds_ping_interval0Sets how often a request goes to a gateway marked inactive; 0 turns this off
ds_probing_mode0Controls which gateways are pinged; 1 tests all gateways
ds_probing_threshold1Sets how many failed attempts mark a gateway inactive
ds_ping_methodOPTIONSThe method used for pings
ds_ping_reply_codesemptyOnly 200 OK counts as a valid reply

The ds_ping_reply_codes example in the code above, class=2;code=403, shows the syntax for adding reply codes beyond 200 OK.

When a destination goes down, the module calls an event route. The docs heading names event_route[dispatcher:dst-down], but the body text says dispatcher:ds-down. Check the name against the module documentation for your version before you write the route.

What goes wrong, and how do you troubleshoot it?

Start with dispatcher.list, since it shows the states the module holds. Then check the flags, the failover settings and the health-check parameters, in that order.

SymptomWhat to check
Failover never runsflags is 2, xavp_dst is dsdst, and t_on_failure("RTF_DISPATCH") runs before route(RELAY).
A failure is not retriedThe sample failure route retries only on a 500 reply or a local timeout.
No OPTIONS pings are sentds_ping_interval is 0 by default. Set it above 0, and make sure the tm module is loaded.
A working server is marked inactiveOnly 200 OK counts by default. Check ds_ping_reply_codes.
Weight has no effectAlgorithm 9 needs the weight attribute, from 1 to 100 inclusive.
Algorithm 10 does not balanceEach address needs its own duid attribute. Reload is disabled for algorithm 10.
A runtime change is gone after a restartdispatcher.add and dispatcher.remove change memory only. Add the line to dispatcher.list too.
The database list does not loaddb_url must be set. The default NULL leaves database support off.
An unexpected destination is chosenAn unknown algorithm ID selects the first entry.
Two reloads are too close togetherReloads need a minimum gap of 5 seconds.

For the health checks, compare the reply codes your server returns with ds_ping_reply_codes before you change any timer.

How do you manage destinations at runtime with kamcmd?

Use dispatcher.list to see groups and states, dispatcher.reload after you edit the list file, and dispatcher.set_state to change one destination. These commands run against the live server:

Shell
kamcmd dispatcher.list
kamcmd dispatcher.reload
kamcmd dispatcher.set_state ip 2 sip:pbx1.example.com:5080
kamcmd dispatcher.set_state ip 3 all

The dispatcher.list output shows each group with its destinations, attributes and states. The state letters are A, I, T and D, with P for probing and X for no ping. A combined state such as AP shows both letters.

The dispatcher.reload command reloads the groups and their destinations. It is disabled for algorithm 10. Reloads also need a minimum gap of 5 seconds between them.

The dispatcher.set_state command sets the state for one destination address, so you can mark it active or inactive. The docs show ip followed by a number, then either one address or all.

The dispatcher.add and dispatcher.remove commands change the groups in memory only. They do not edit dispatcher.list, so keep the file in step with any change you want to keep. The reference also lists dispatcher.ping_active, which takes 0 or 1, and dispatcher.hash.

Advanced examples: weighted, call-load and priority setups

Each example below combines the documented pieces from this guide. None adds a function or parameter beyond those shown above or listed in the reference.

Weighted split. Use the weight list from the weight section, and route with algorithm 9:

Kamailio config
# Weighted split: the weight attributes in dispatcher.list set the shares.
route[DISPATCH] {
    if(ds_select_dst("1", "9")) {
        route(RELAY);
    }
}

The only change from round-robin is the second argument, now 9. The module then distributes load by the weight attributes in the list.

Call load. Each address needs its own duid, and the reference marks algorithm 10 as INVITE only. Use this list:

Output
1 sip:pbx1.example.com:5080 0 0 duid=abc
1 sip:pbx2.example.com:5080 0 0 duid=xyz

The duid values are unique, as algorithm 10 requires. The route selects with algorithm 10:

Kamailio config
# Call load: algorithm 10, INVITE requests only.
route[DISPATCH] {
    if(ds_select_dst("1", "10")) {
        route(RELAY);
    }
}

The reference lists ds_load_update() and ds_load_unset() for this algorithm. Reloads are disabled for algorithm 10, so plan list changes around that.

Priority with failover and health checks. The priority column is the fourth field of each line:

Output
1 sip:pbx1.example.com:5080 0 0 duid=abc
1 sip:pbx2.example.com:5080 0 1 duid=xyz

The module sorts the destinations by their priority attribute, and algorithm 8 forks to them one after another in that order. Combine the settings from the earlier sections:

Kamailio config
# Priority routing, failover and health checks in one configuration.
modparam("dispatcher", "list_file", "/etc/kamailio/dispatcher.list")
modparam("dispatcher", "flags", 2)
modparam("dispatcher", "xavp_dst", "_dsdst_")
modparam("dispatcher", "ds_ping_interval", 30)
modparam("dispatcher", "ds_probing_mode", 1)

route[DISPATCH] {
    if(ds_select_dst("1", "8")) {
        t_on_failure("RTF_DISPATCH");
        route(RELAY);
    }
}

Keep the RELAY route and failure_route[RTF_DISPATCH] from the failover section. Together they retry a 500 reply or a local timeout on the next address.

More guides in Open-Source PBX and SIP Servers.

Frequently asked questions

Can the dispatcher list be loaded from a database instead of a file?
Yes. Set db_url to a database connection string, and set table_name to dispatcher. The docs example uses a MySQL string with placeholder credentials. The default for db_url is NULL, which keeps database support off.
What is the difference between ds_select_dst and ds_select_domain?
ds_select_dst sets the destination URI and leaves the Request-URI unchanged. ds_select_domain selects a destination the same way, but it also rewrites the host and port of the Request-URI.

Sources

  1. The Kamailio dispatcher module provides SIP load balancing and can be used as a SIP traffi (www.kamailio.org)