# `RpcLoadBalancer.LoadBalancer.SelectionAlgorithm.WeightedRoundRobin`
[🔗](https://github.com/MikaAK/rpc_load_balancer/blob/main/lib/rpc_load_balancer/load_balancer/selection_algorithm/weighted_round_robin.ex#L1)

Weighted round robin node selection algorithm.

Accepts a weight map via `algorithm_opts` where keys are node names and
values are positive integers representing relative capacity. Nodes with
higher weights receive proportionally more traffic.

Storage:
  * `:weights` (set once at `init/2`) lives in `:persistent_term`
    keyed by `{__MODULE__, lb_name, :weights}`.
  * The expanded node list (the weight map projected into a flat
    list, one entry per weight unit) lives in `WeightedRoundRobinCache`
    (ETS). It's rebuilt on every `on_node_change` so storing it in PT
    would trigger continuous global GC in clusters with steady
    flapping; ETS writes are local and lock-free under
    `write_concurrency: true`.

Steady-state hot path is one ETS lookup, one atomic counter
increment, and an `Enum.at/2` into the cached expanded list.

## Usage

    RpcLoadBalancer.start_link(
      name: :my_balancer,
      selection_algorithm: RpcLoadBalancer.LoadBalancer.SelectionAlgorithm.WeightedRoundRobin,
      algorithm_opts: [weights: %{:"node1@host" => 3, :"node2@host" => 1}]
    )

Nodes not present in the weight map receive a default weight of 1.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
