View Source Nebulex.Adapter.Keyslot behaviour (Nebulex v2.6.4)

This behaviour provides a callback to compute the hash slot for a specific key based on the number of slots (partitions, nodes, ...).

The purpose of this module is to allow users to implement a custom hash-slot function to distribute the keys. It can be used to select the node/slot where a specific key is supposed to be.

It is highly recommended to use a Consistent Hashing algorithm.

Example

defmodule MyApp.Keyslot do
  use Nebulex.Adapter.Keyslot

  @impl true
  def hash_slot(key, range) do
    key
    |> :erlang.phash2()
    |> :jchash.compute(range)
  end
end

This example uses Jumping Consistent Hash.

Summary

Callbacks

Returns an integer within the range 0..range-1 identifying the hash slot the specified key hashes to.

Callbacks

@callback hash_slot(key :: any(), range :: pos_integer()) :: non_neg_integer()

Returns an integer within the range 0..range-1 identifying the hash slot the specified key hashes to.

Example

iex> MyKeyslot.hash_slot("mykey", 10)
2