whitfin / cachex

A powerful caching library for Elixir with support for transactions, fallbacks and expirations
https://hexdocs.pm/cachex/
MIT License
1.6k stars 104 forks source link

Check if there is interest in Rendezvous router #391

Open ruslandoga opened 1 hour ago

ruslandoga commented 1 hour ago

👋

I learned recently that Cachex supports distributed caching, and when checking the implementation I saw that it uses consistent hashing. I wonder if there is any interest in implementing the approach described in https://en.wikipedia.org/wiki/Rendezvous_hashing?

I did some experiments with it some years ago and thought at the time that it's a bit more straightforward than https://github.com/discord/ex_hash_ring as the basic implementation doesn't require any 3rd party NIFs or keeping extra in-memory state.

defmodule Cachex.Router.Rendezvous do
  def route(key) do
    {node, _} =
      [Node.self() | Node.list()]
      |> Enum.map(fn node -> {node, :erlang.phash2({key, node})} end)
      |> Enum.max_by(fn {_node, hash} -> hash end)

    node
  end
end
ruslandoga commented 1 hour ago

If there is interest, I'd be happy to contribute!