Search code examples
cachingelixirets

Distributed Caching in Elixir


I am writing an Elixir app that requires a registry to store the mapping of which pid belongs to which user. I will have an GenServer per user in the app that will be supervised. I have the basic example working with one node using ETS but with 2+ nodes, I can't use ETS since it does not support clustering/replication. What are some other options for having a distributed cache? From doing some research, my options are using Database such as Redis or use Amensia.


Solution

  • Assuming you don't want duplicates across your cluster, you could simply register each GenServer globally.

    GenServer.start_link(__MODULE__, args, [name: {:global, user_id}]
    

    Then you can simply lookup :global.whereis_name(user_id) to get the pid. If the process dies, it's automatically unregistered. Here's the documentation for Erlang's global module.