Returns the single node responsible for key.
The key to route (e.g. a room or session identifier).
The id of the highest-scoring node, or undefined if the ring is empty.
Returns the top n nodes responsible for key, in descending score order.
The key to route.
The maximum number of nodes to return.
Up to n node ids ordered from most to least preferred; empty if n is not positive or the ring is empty. Useful for selecting a primary plus replicas.
Checks whether a node with the given id is on the ring.
The node id to test.
true if the node is present.
Returns the ids of all nodes currently on the ring.
A new array of node ids, in insertion order.
Removes a node from the ring.
The id of the node to remove.
true if a node was removed, false if no node with that id existed.
Consistent-hashing ring for distributing keys across a set of weighted nodes.
HashRinguses rendezvous (highest-random-weight) hashing: for a given key it computes a score for every node and returns the highest-scoring one. This yields a stable mapping in which adding or removing a node only remaps the keys that node is responsible for, leaving the rest untouched — ideal for sharding rooms, sessions, or state across a cluster.Remarks
Weights bias the scoring so heavier nodes receive proportionally more keys. The hash is a deterministic 53-bit function of the key and node id, so the same inputs always map the same way regardless of insertion order.
Example