RpcLoadBalancer.LoadBalancer.SelectionAlgorithm.LeastCpu (rpc_load_balancer v0.3.0)

Copy Markdown View Source

Least CPU node selection algorithm.

Routes calls to the node with the lowest CPU utilization. A background Poller GenServer periodically samples local and remote CPU metrics, storing them in NodeCpuCache. Selection reads directly from cache and randomly picks from nodes within a configurable threshold of the minimum.

Selection never blocks on the network — if a node's cache entry is missing or stale, it falls back to @default_cpu (50.0) and lets the Poller refresh the value on its next tick.

Usage

RpcLoadBalancer.start_link(
  name: :my_lb,
  selection_algorithm: RpcLoadBalancer.LoadBalancer.SelectionAlgorithm.LeastCpu,
  algorithm_opts: [
    poll_interval: 5_000,
    cpu_cache_ttl: 10_000,
    cpu_threshold: 5.0
  ]
)

Options

  • :poll_interval - Background poll frequency in ms (default: 5_000)
  • :poll_startup_jitter - Max ms of random delay before the first poll fires (default: 60_000). Prevents thundering-herd multicalls when many nodes boot at once. Set 0 to disable.
  • :cpu_cache_ttl - Max cache age before an entry is treated as missing (default: 10_000)
  • :cpu_threshold - Band width in percentage points for "close enough" selection (default: 5.0)
  • :cpu_sampler - Zero-arity function returning a numeric CPU percent. Defaults to :cpu_sup.util/0. Override for tests or to plug in an alternative metric source.

Telemetry

The Poller emits :telemetry events under the [:rpc_load_balancer, :least_cpu, :poll] prefix. See RpcLoadBalancer.LoadBalancer.SelectionAlgorithm.LeastCpu.Poller for details.