Skip to content

Kamailio Hash Load Balancing: 6 Algorithms for 2027

Compare the six Kamailio dispatcher hashing algorithms: what each hashes, how Call-ID keeps a call together, hash_pvar, and the sngrep call flow.

By · Updated · Published

Close-up of server cooling fans in a vibrant data center
Photo by Winston Chen · Unsplash License · via Unsplash

What is hash load balancing in Kamailio?

The dispatcher module hashes one part of the SIP request and uses the result to pick a destination from an address set. The same input gives the same hash, so matching requests go to the same destination. The documentation says the module can be used as a stateless load balancer and does not depend on any call state tracking module.

Destination sets come from a list file. In kamailio.cfg:

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

In /run/kamailio/dispatcher.list, each line holds a set id and a destination, followed by optional flags, priority and attributes. The addresses are examples from the documentation ranges:

Output
1 sip:192.0.2.10:5060 0 5
1 sip:198.51.100.20:5060

The first line sets flags to 0 and priority to 5. The second line gives only the set id and the destination. The dispatcher module documentation lists every parameter used in this post.

To check which destination sets are loaded, use the dispatcher.list RPC command. It lists the routing groups with destination addresses and their attributes. It can be run with kamcmd or kamctl:

Shell
kamcmd dispatcher.list
kamctl rpc dispatcher.list

The call that picks a destination is ds_select_dst(set, alg[, limit]). It selects a destination from the set and returns true if a new destination is set.

Which dispatcher algorithms hash, and over what?

We compiled the six hashing algorithms from the official dispatcher documentation on November 15, 2026. This is a reading of published documentation, not a benchmark.

AlgorithmHashes overWhat stays on one destinationNeeds
0Call-IDEvery request that carries the same Call-ID, so one callNothing extra
1From-URIRequests from the same From URINothing extra; affected by flags bit 1
2To-URIRequests to the same To URINothing extra; affected by flags bit 1
3Request-URI userRequests for the same Request-URI userNothing extra
5Authorization username, from the Proxy-Authorization or Authorization headerRequests from the same authenticated userCredentials in the request; falls back to round-robin if no username is found
7The content of a string of pseudo-variablesWhatever the string holdsThe hash_pvar module parameter must be set

The dispatcher module documentation and the 5.8.x dispatcher page list the same six algorithms. Algorithm 5 falls back to round-robin when no username is found. Flags bit 1 changes how algorithms 1 and 2 hash a URI, as the sticky section below shows.

How does hash over Call-ID keep one call together?

Every request of a call carries the same Call-ID, so algorithm 0 sends them to the same destination. The SIP specification describes the Call-ID as a globally unique identifier for the call.

The dispatcher module documentation says the dispatcher is stateless, but some algorithms are designed to select the same destination for later requests of the same dialog. Hashing the Call-ID is its example. In kamailio.cfg:

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

Set 1 is the destination set from the list file. If no destination is set, the script replies 404 and stops. Otherwise, t_on_failure sets the failure route RTF_DISPATCH, which runs after a transaction ends with a negative result, and route(RELAY) relays the request.

How do you get sticky load balancing per caller or user?

Hash on a value that identifies the caller. Algorithm 1 keeps requests from the same From-URI together, and algorithm 5 keeps requests from the same authenticated user together. Algorithm 3 hashes the Request-URI user instead.

Without flags, a URI-based hash uses the username, hostname and port. The port counts only if it differs from 5060, or from 5061 for a sips: URI. Setting flag 1 means only the username part of the URI is hashed. The value 1 is used because only bit 1 is set. In kamailio.cfg:

Kamailio config
modparam("dispatcher", "flags", 1)
if(!ds_select_dst("1", "1")) {
    send_reply("404", "No destination");
    exit;
}

With this setting, the hostname and port of the From-URI do not change the hash.

How does hash_pvar work with algorithm 7?

Algorithm 7 hashes the content of a string of pseudo-variables, and it works only when hash_pvar is set. The parameter is null by default. The documentation says you must set it to hash over custom message parts. The string holds pseudo-variables, and algorithm 7 hashes the content of that string. Because the parameter is null by default, the algorithm has nothing to hash until you set it.

In kamailio.cfg:

Kamailio config
modparam("dispatcher", "hash_pvar", "hash the $fU@$ci")
if(!ds_select_dst("1", "7")) {
    send_reply("404", "No destination");
    exit;
}

The documentation's example string combines the From user and the Call-ID. In it, $fU is the username in the URI of the From header, and $ci is the value of the Call-ID header. The documentation's other example, $avp(hash), hashes the value of the AVP named hash. The pseudo-variables cookbook also lists $tU for the username in the To URI, $au for the user part of the username in the Authorization or Proxy-Authorization header, and $si for the source IP address.

Is hash distribution fair, and what are its limits?

No. The dispatcher module documentation says the algorithms that hash over parts of the SIP message do not guarantee a fair distribution. Round robin and call load dispatching, unlike hashing, do a fair distribution in terms of calls delivered to gateways.

The documentation advises measuring to decide which hashing algorithm fits an environment. This post does not report any distribution figures.

To see which slot a value maps to, use the dispatcher.hash RPC command. It computes the hash id for the string values you give it, and it returns the slot when a number of slots is given. The parameters are nslots, val1 and an optional val2. For a URI with a port other than 5060, or 5061 for TLS, val2 has to be domain:port. The documentation's examples are these two commands:

Shell
kamctl rpc dispatcher.hash 0 alice server.com
kamctl rpc dispatcher.hash 4 bob server.com

The documentation also leaves some points open. It does not say how hash results are remapped when the set changes size. It does not say whether the transport is part of a URI-based hash. It documents no variable that holds the computed hash value after ds_select_dst runs.

What does a hash-dispatched call look like in sngrep?

Call flow: the caller sends INVITE to Kamailio, which answers 100 Trying and sends INVITE to the chosen gateway. 180 Ringing and 200 OK return through Kamailio. The caller sends ACK. BYE goes from caller to Kamailio to gateway, and 200 OK comes back.
One hash-dispatched call through Kamailio

This walk-through follows one call from the caller through Kamailio to the gateway the hash chose. The message roles follow the SIP specification, and each item is one SIP message.

  1. The caller sends INVITE to Kamailio.
  2. Kamailio sends 100 Trying back to the caller.
  3. Kamailio sends INVITE to the gateway that the hash chose.
  4. The gateway sends 180 Ringing to Kamailio.
  5. Kamailio sends 180 Ringing to the caller.
  6. The gateway sends 200 OK to Kamailio when the call is answered.
  7. Kamailio sends 200 OK to the caller.
  8. The caller sends ACK to confirm the final response.
  9. The caller sends BYE to Kamailio to end the call.
  10. Kamailio sends BYE to the gateway.
  11. The gateway sends 200 OK to Kamailio to confirm the BYE.
  12. Kamailio sends 200 OK to the caller.

Either side can end a call with a BYE. This example shows the caller ending it.

The documentation does not describe the hash step in the SIP messages, so this list shows the message flow only. It does not show which gateway a given Call-ID maps to.

Advanced examples: how do hashing and failover combine?

Flow chart of failure_route[RTF_DISPATCH]: exit if t_is_canceled(); if t_check_status("500") matches or the branch timed out with no reply, ds_next_dst() takes the next address, t_on_failure("RTF_DISPATCH") is set again, and route(RELAY) relays.
Failover to the next destination after a failed branch

The documentation's example sets flags to 3, which turns on username-only hashing and failover together. Setting use_default to 1 adds a last-resort destination, such as an announcement server. The xavp_dst parameter is set to dsdst, as in the documentation's sample.

In kamailio.cfg:

Kamailio config
modparam("dispatcher", "flags", 3)
modparam("dispatcher", "use_default", 1)
modparam("dispatcher", "xavp_dst", "_dsdst_")

In kamailio.cfg, the failure route runs after a failed branch:

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

The failure route exits when the call was canceled. The regular expression in t_check_status matches the reply code, so a 500 reply matches. For a 500 reply, or a local timeout with no reply, it calls ds_next_dst() to take the next address, sets the failure route again, and relays. The RELAY route calls t_relay(), which relays the request statefully:

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

Flag 2 turns on failover support. The remaining addresses of the set are stored in XAVPs, and ds_next_dst() takes the next one and sets it as the outbound proxy address. The force_dst parameter controls an address that is already set. Its default is 1, which overwrites that address. When it is 0, an error is returned instead.

More guides in Open-Source PBX and SIP Servers.

Frequently asked questions

How can I see which slot a value hashes to?
Use the dispatcher.hash RPC command. It computes the hash id for the string values you give it, and it returns the slot when you also give a number of slots. For a URI with a port other than 5060, or 5061 for TLS, the second value has to be domain:port.
What happens to a hashed request when its destination is inactive?
An address marked as inactive or disabled is ignored by ds_select_dst and ds_select_domain. The documentation does not say how hash results are remapped when a destination goes inactive.

Sources

  1. ds_select_dst(set, alg[, limit]) selects a destination from an address set and returns tru (www.kamailio.org)
  2. The Kamailio 5.8.x dispatcher documentation lists the same six hashing algorithms (0, 1, 2 (kamailio.org)
  3. The Call-ID header contains a globally unique identifier for the call, so every request of (www.rfc-editor.org)
  4. The pseudo-variable $ci is the value of the Call-ID header, $fU is the username in the URI (www.kamailio.org)