Back to Insights
VoIP Engineering

Kamailio Dispatcher Module: Load Balancing Configuration That Survives Failures

Telephony Engineering Team
4 min read
Kamailio Dispatcher Module: Load Balancing Configuration That Survives Failures

Kamailio's dispatcher module distributes SIP requests across a set of destinations using a chosen algorithm, monitors their health with probes, and fails over automatically. It is the workhorse behind most Kamailio-in-front-of-FreeSWITCH architectures.

Core Concepts

  • Destination set: a numbered group of backend addresses, for example set 1 with several FreeSWITCH nodes
  • Algorithm: how a destination is picked from the set
  • Probing: periodic SIP OPTIONS to detect when a destination is down
  • Failover: selecting the next destination if one fails to answer

Common Algorithms

AlgorithmBehaviorUse when
Hash over Call-IDSame call always maps to same nodeStateless distribution with call affinity
Round robinRotate evenlyEqual, stateless backends
Weighted round robinProportion by weightMixed-capacity nodes
Hash over From/To/Request-URIAffinity per user or numberKeep a user's traffic on one node
PriorityAlways the first availableActive/standby
Load-based (relative weight and call counts)Considers current loadUneven call durations

Numbering and exact options differ by version, so confirm against the module documentation for your release.

Minimal Configuration Sketch

loadmodule "dispatcher.so"
modparam("dispatcher", "list_file", "/etc/kamailio/dispatcher.list")
modparam("dispatcher", "flags", 2)
modparam("dispatcher", "ds_ping_interval", 10)
modparam("dispatcher", "ds_probing_mode", 1)
modparam("dispatcher", "ds_ping_from", "sip:probe@lb.example.com")

# routing logic
route[DISPATCH] {
    if (!ds_select_dst("1", "4")) {      # set 1, round robin
        send_reply("503", "No destination available");
        exit;
    }
    t_on_failure("LB_FAILURE");
    route(RELAY);
}

failure_route[LB_FAILURE] {
    if (t_is_canceled()) exit;
    if (t_check_status("500|503|408") && ds_next_dst()) {
        t_relay();
    }
}

/etc/kamailio/dispatcher.list lists entries such as 1 sip:10.0.0.11:5060 0 10 (set, address, flags, priority/weight). Use a database table for dynamic changes.

Health Checking and Failover

  • Probe with OPTIONS so that failed nodes are removed from rotation automatically
  • In failure_route, retry only on errors that make sense (408, 5xx). Do not retry a 486 Busy or 603 Decline.
  • Cap the number of retries to avoid loops (see SIP response codes)

Operational Tips

  1. Drain before maintenance: set a node to inactive so existing calls finish and no new ones arrive.
  2. Manage via RPC or database, not config edits and restarts (kamcmd dispatcher.set_state).
  3. Keep in-dialog requests on the right node with Record-Route and loose routing.
  4. Persist state across Kamailio restarts where required.
  5. Run Kamailio itself redundantly with a floating IP or anycast, so the balancer is not the single point of failure (see VoIP disaster recovery).

Related Reading

Why this pattern scales is explained in FreeSWITCH horizontal scaling with Kamailio. For comparing Kamailio with the alternative, see OpenSIPS vs Kamailio, and for registration load see SIP registration explained.


Need a load balancer you can trust at 3 a.m.? Our Kamailio load balancing team designs, builds, and operates highly available SIP edges.

Discussion

0

Join the conversation

Sign in with your preferred account to comment, reply, and keep the discussion useful for other engineers.

Takes a few seconds. No separate password required.

C

Controva LLC

Engineering Team

We build digital empires that scale. Specializing in high-performance web architecture, AI automation, and headless commerce.

Work with us

Share this article