HRW.Bounded (hrw v0.2.1)

Copy Markdown

A bounded-load variant of HRW. Distributes a known set of keys across nodes such that no node receives more than ceil(|keys| / |nodes| × (1 + epsilon)) keys.

Pure function of (keys, nodes, opts) — any two callers with the same inputs produce the same assignment, no coordination required. Use this when the full key set is known at compute time and you want bounded skew.

Summary

Functions

Returns a map of key => node covering every input key. Returns %{} when keys is empty.

Functions

assignments(keys, nodes, opts \\ [])

@spec assignments([term()], [term()], keyword()) :: %{required(term()) => term()}

Returns a map of key => node covering every input key. Returns %{} when keys is empty.

Each key is assigned to its highest-scoring node, with overflow falling through to the next-best when a node hits the cap of ceil(|keys| / |nodes| × (1 + epsilon)).

Options

  • :epsilon - load slack factor. Smaller values give tighter balance but more movement on node churn. Defaults to 0.25.
  • :hash_fn - a function term -> integer. Defaults to &:erlang.phash2/1.

Examples

iex> HRW.Bounded.assignments(["a", "b", "c", "d"], ["x", "y"], epsilon: 0.0)
%{"a" => "x", "b" => "x", "c" => "y", "d" => "y"}