One GenServer Per Cluster - whats the best way to set a process per node?

Hi everyone I`d like to know whats the best way to set a process per node. For example I want to have a Cache GenServer that all my nodes could read from. I know about the :global strategy but when new nodes join the cluster it will have a name conflict because each node when it starts tries to register this cache service How could I implement something like “if a new node joins, drops its cache and uses the already created in cluster and if none is created register yours as the global one”

2 Likes

You should be able to use global and match in the result of start_link. If it returns as already registered, you can then return the atom :ignore and the supervision tree will ignore it.

6 Likes

Thanks, It helped a lot. I also found this blog post that I think implements something in the way you mentioned if anyone is interested this is the link Three real-world examples of distributed Elixir (pt. 2) · bigardone.dev

1 Like

I’ve seen the Highlander library used in production to handle this - it’s exactly the pattern @josevalim described with :global, bundled up with some monitor plumbing to handle “node the process is on goes away”.

2 Likes