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 Arkay · Updated · Published
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:
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:
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:
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.
| Algorithm | Hashes over | What stays on one destination | Needs |
|---|---|---|---|
| 0 | Call-ID | Every request that carries the same Call-ID, so one call | Nothing extra |
| 1 | From-URI | Requests from the same From URI | Nothing extra; affected by flags bit 1 |
| 2 | To-URI | Requests to the same To URI | Nothing extra; affected by flags bit 1 |
| 3 | Request-URI user | Requests for the same Request-URI user | Nothing extra |
| 5 | Authorization username, from the Proxy-Authorization or Authorization header | Requests from the same authenticated user | Credentials in the request; falls back to round-robin if no username is found |
| 7 | The content of a string of pseudo-variables | Whatever the string holds | The 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:
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:
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:
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:
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?

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.
- The caller sends
INVITEto Kamailio. - Kamailio sends
100 Tryingback to the caller. - Kamailio sends
INVITEto the gateway that the hash chose. - The gateway sends
180 Ringingto Kamailio. - Kamailio sends
180 Ringingto the caller. - The gateway sends
200 OKto Kamailio when the call is answered. - Kamailio sends
200 OKto the caller. - The caller sends
ACKto confirm the final response. - The caller sends
BYEto Kamailio to end the call. - Kamailio sends
BYEto the gateway. - The gateway sends
200 OKto Kamailio to confirm the BYE. - Kamailio sends
200 OKto 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.](https://pub-a8d8d5c430154fb8b0cad26868f7eaad.r2.dev/media/6af9190e7a11731a87ee60d2.webp)
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:
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:
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:
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.
Related guides
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
- ds_select_dst(set, alg[, limit]) selects a destination from an address set and returns tru (www.kamailio.org)
- The Kamailio 5.8.x dispatcher documentation lists the same six hashing algorithms (0, 1, 2 (kamailio.org)
- The Call-ID header contains a globally unique identifier for the call, so every request of (www.rfc-editor.org)
- The pseudo-variable $ci is the value of the Call-ID header, $fU is the username in the URI (www.kamailio.org)