Skip to content

Kamailio Call Load Distribution: Algorithm 10 Setup

Kamailio call load distribution (dispatcher algorithm 10): required ds_hash_size and duid settings, load functions, failover and one call flow in sngrep.

By · Updated · Published

Network switch and cables
Photo by nayukim · CC BY 2.0 · via Flickr

What is call load distribution in Kamailio?

Call load distribution is algorithm "10" of the ds_select_dst function in the Kamailio dispatcher module. It sends each new INVITE to the gateway with the fewest calls associated with it.

The documentation's example call is:

Kamailio config
ds_select_dst("1", "10");

Here "1" is the destination set and "10" is the call load algorithm. The documentation gives the full form as ds_select_dst(set, alg[, limit]).

The first destination selected is the one with the least number of calls. The rest of the list follows in the order of the entries in the set. Algorithm 10 can be used only for INVITE requests, because INVITE is the only SIP method that creates a call. The Kamailio 5.8.x dispatcher documentation lists the same algorithm and the same duid requirement.

Which settings does algorithm 10 need?

Person typing on a laptop computer keyboard
Photo by Anton Ryazanov · Unsplash License · via Unsplash

Algorithm 10 needs two things. The ds_hash_size module parameter must be greater than 0, and every address in the destination set needs a unique duid attribute. The maxload attribute is optional.

The table below lists the related parameters from the documentation, with their defaults and the example values the documentation uses. We compiled it on November 15, 2026 from the official dispatcher documentation, cross-checked with the 5.8.x and devel pages. It is a reading of published documentation, not a benchmark.

Module parameterWhat it doesDefaultDocumentation example
ds_hash_sizeHash table slots as a power of two; must be greater than 0 to enable algorithm 100 (off)9
ds_hash_expireSeconds before load is removed from a destination if no BYE was received72003600
ds_hash_initexpireSeconds before load is removed if no 200 for the INVITE was received720060
ds_hash_check_intervalSeconds between scans of the hash table for expired items3060

The value of ds_hash_size is a power of two. The documentation says 8 gives 256 slots. The lines below use the documentation's example values. They are examples, not recommendations.

Kamailio config
modparam("dispatcher", "ds_hash_size", 9)
modparam("dispatcher", "ds_hash_expire", 3600)
modparam("dispatcher", "ds_hash_initexpire", 60)
modparam("dispatcher", "ds_hash_check_interval", 60)

The duid attribute must be a unique value that identifies a destination, such as a gateway address. The maxload attribute must be a positive integer. It sets the upper limit of active calls per destination. Once a gateway reaches its maxload, it is not selected for new calls until an existing call through it ends. A value of 0 means no limit.

How do you set up dispatcher.list with duid and maxload?

Each line in the destination list file has a set ID, a SIP URI, optional flags, an optional priority and optional attributes. Put duid= on every gateway. Add maxload= if you want a cap on its active calls. The lines below use example addresses from the 192.0.2.0 documentation range.

In dispatcher.list:

Output
1 sip:192.0.2.10:7070 0 0 duid=gw1;maxload=20
1 sip:192.0.2.11:7070 0 0 duid=gw2;maxload=20

In these lines, the set ID is 1, the flags and priority are both 0, and the attributes are duid and maxload. By default the file is /etc/kamailio/dispatcher.list or /usr/local/etc/kamailio/dispatcher.list. To set the path yourself, use the list_file parameter. In kamailio.cfg:

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

The dispatcher.list command lists the routing groups with their destination addresses and attributes. Run it from the shell:

Shell
kamcmd dispatcher.list

Its sample output shows the configured MAXLOAD under ATTRS. The documentation does not say that this figure is the live call count. The FLAGS field has two letters: the state (A, I, T or D), then P for probing or X for no probing.

Where do ds_load_update and ds_load_unset go in kamailio.cfg?

ds_load_update() goes in two places. In the request route, it removes the load from the destination used to forward the INVITE when a BYE or CANCEL arrives. In the reply route, it sets the call load record to confirmed when a 2xx reply to the INVITE arrives.

ds_load_unset() goes in the reply route for INVITE replies with a 3xx to 7xx code. It removes the call load for the destination that routed the call.

In kamailio.cfg:

Kamailio config
request_route {
    if(is_method("BYE|CANCEL")) ds_load_update();
}

onreply_route {
    if(is_method("INVITE")) {
        if(status=~"2[0-9][0-9]") ds_load_update();
        else if(status=~"[3-7][0-9][0-9]") ds_load_unset();
    }
}

The textops documentation says the name in is_method() can be a list such as method1|method2. That is why BYE|CANCEL works in one check. The printed example in the dispatcher documentation leaves out a closing parenthesis on the if(is_method("INVITE")) line. The block above has it closed.

Both functions can be called from REQUEST_ROUTE, FAILURE_ROUTE, BRANCH_ROUTE and ONREPLY_ROUTE.

Why do calls keep counting against a gateway after they end?

A call keeps counting when Kamailio never sees its BYE. The load then stays until ds_hash_expire runs out. The default is 7200 seconds (2 hours). The documentation's example sets it to 3600 seconds (1 hour).

A call that never gets a 200 for its INVITE counts until ds_hash_initexpire runs out. That is also 7200 seconds by default. The documentation's example lowers it to 60 seconds.

The ds_hash_check_interval parameter sets how often the hash table is scanned for expired items. The default is 30 seconds. These timers are the fallback for calls whose BYE or 200 never reaches Kamailio.

How do you add failover to another gateway?

The flags parameter of the dispatcher module enables failover when it is set to 2. The documentation says failover support is enabled when flag 2 is set. The rest of the destination set is then stored in an XAVP list. In kamailio.cfg:

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

In the request route, the selection comes first. If the selection fails, the route replies 404 with "No destination". Otherwise, it arms the failure route and relays the request.

t_on_failure sets the failure routing block that runs when a transaction completes with a negative result. t_relay relays the request statefully to the destination in the current URI when it has no parameters. Both are TM module functions. In the request route of kamailio.cfg:

Kamailio config
request_route {
    if(is_method("INVITE")) {
        if(!ds_select_dst("1", "10")) {
            send_reply("404", "No destination");
        } else {
            t_on_failure("RTF_DISPATCH");
            t_relay();
        }
    }
}

The failure route picks the next gateway. It runs when t_check_status("500") is true, or when t_branch_timeout() is true and t_branch_replied() is not.

ds_next_dst() takes the next destination address from the XAVPs and sets the outbound proxy address. If it finds one, the documentation's sample calls t_on_failure again and relays, so a further failure can trigger the same route. The failure route in kamailio.cfg:

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

What does one call look like in sngrep?

Call flow: Caller sends INVITE to Kamailio, which answers 100 Trying and sends INVITE to the gateway with the fewest calls. 180 Ringing and 200 OK come back through Kamailio. Caller sends ACK. BYE goes through Kamailio to the gateway, and 200 OK comes back.
One call dispatched by call load, as in sngrep

The steps below follow one call from INVITE to BYE, in the order Kamailio sees them. The message meanings come from the SIP specification.

  1. Caller to Kamailio: INVITE, asking to set up a session.
  2. Kamailio to caller: 100 Trying, showing the INVITE was received.
  3. Kamailio to gateway: INVITE, sent to the gateway with the fewest calls.
  4. Gateway to Kamailio, then to caller: 180 Ringing.
  5. Gateway to Kamailio, then to caller: 200 OK. ds_load_update() runs here.
  6. Caller: ACK for the 2xx response.
  7. Either side: BYE. ds_load_update() runs here and frees the load.
  8. The other side: 200 OK for the BYE.

In short, the count rises at step 3, is confirmed at step 5 and falls at step 7. If the caller hangs up before the answer, it sends CANCEL instead, and Kamailio runs ds_load_update() on that CANCEL too. If the gateway answers the INVITE with a 3xx to 7xx reply, the reply route runs ds_load_unset(), and the failed call stops counting.

Advanced examples: what limits should you plan around?

Our reading of the documentation found two limits for algorithm 10 that affect design:

  • The documentation does not say what ds_select_dst returns when every destination has reached its maxload. It documents no event route for that case. The only documented events are dispatcher:dst-down and dispatcher:dst-up.
  • With the defaults, a call whose BYE never reaches Kamailio counts against its gateway for 7200 seconds.

The examples below combine only the documented pieces above. The values are the documentation's examples.

Example 1: timers, list file and the request route

Kamailio config
# Example values from the dispatcher documentation. Load the TM module too, for t_relay.
loadmodule "dispatcher.so"
modparam("dispatcher", "list_file", "/etc/kamailio/dispatcher.list")
modparam("dispatcher", "ds_hash_size", 9)            # must be greater than 0
modparam("dispatcher", "ds_hash_expire", 3600)       # seconds, default 7200
modparam("dispatcher", "ds_hash_initexpire", 60)     # seconds, default 7200
modparam("dispatcher", "ds_hash_check_interval", 60) # seconds, default 30

request_route {
    # A BYE or CANCEL releases the load on the gateway used for the INVITE.
    if(is_method("BYE|CANCEL")) {
        ds_load_update();
        t_relay();
    }
    if(is_method("INVITE")) {
        # Select the gateway with the fewest calls in set 1, algorithm 10.
        if(!ds_select_dst("1", "10")) {
            send_reply("404", "No destination");
        } else {
            t_relay();
        }
    }
}

Example 2: failover and load release on INVITE replies

Kamailio config
# Flag 2 enables failover; the rest of the set is kept in an XAVP list.
modparam("dispatcher", "flags", 2)

request_route {
    if(is_method("INVITE")) {
        if(!ds_select_dst("1", "10")) {
            send_reply("404", "No destination");
        } else {
            # Arm the failure route for this transaction, then relay.
            t_on_failure("RTF_DISPATCH");
            t_relay();
        }
    }
}

failure_route[RTF_DISPATCH] {
    # Try the next gateway on a 500 reply, or on a timeout with no reply.
    if(t_check_status("500") or (t_branch_timeout() and !t_branch_replied())) {
        if(ds_next_dst()) {
            t_on_failure("RTF_DISPATCH");
            t_relay();
        }
    }
}

# Reply route: confirm the load on a 2xx reply to the INVITE, remove it on a 3xx to 7xx reply.
onreply_route {
    if(is_method("INVITE")) {
        if(status=~"2[0-9][0-9]") ds_load_update();
        else if(status=~"[3-7][0-9][0-9]") ds_load_unset();
    }
}

Example 3: reading the destination list

Shell
# Shows the configured MAXLOAD under ATTRS. The documentation does not say that it shows the live call count.
kamcmd dispatcher.list
# The documentation says dispatcher.reload is disabled for algorithm 10, so do not run it for this setup.

These examples are not a complete production configuration. Where the documentation is silent, such as a set in which every gateway has reached its maxload, plan the behaviour in your own design.

More guides in Open-Source PBX and SIP Servers.

Frequently asked questions

How does Kamailio pick the gateway with the fewest calls?
It selects the destination with the least number of calls associated. The rest of the list follows the order of the entries in the set. Kamailio tracks these calls itself, by Call-Id.
Does algorithm 10 need the dialog module or stateful mode?
No. The documentation says algorithm 10 works even in stateless proxy mode, using its own lightweight call tracking by Call-Id. It lists the TM module only for active recovery of failed hosts, but the failover example above uses TM functions.
Can I run dispatcher.reload when I use call load dispatching?
No. The documentation says the dispatcher.reload command is disabled for call load based dispatching (algorithm 10).

Sources

  1. In the dispatcher module, algorithm "10" of ds_select_dst is call load distribution, and e (www.kamailio.org)
  2. The Kamailio 5.8.x documentation lists algorithm "10" as call load distribution with the s (kamailio.org)
  3. In the textops module, is_method(name) checks whether the message's method matches the nam (www.kamailio.org)
  4. In SIP, the INVITE request asks a server to establish a session. (www.rfc-editor.org)