A global alias associates a process with an arbitrary term and makes that process discoverable across every connected node in a cluster. It solves cluster-wide process discovery when one process must answer consistently for a given identity. section 12.1.3 section 12.2.2
Global registration trades coordination during registration for fast local lookups:
- Acquires a cluster-wide lock, checks whether the alias exists, informs all nodes, and releases the lock. section 12.1.3
- Caches registration information in each node’s local ETS table, so later lookups avoid network chatter. section 12.1.3
- Rejects a competing registration for the same alias, preserving one cluster-wide process identity. section 12.1.3
- Fits single-process discovery; use process groups when multiple processes should share one cluster-wide alias. section 12.2.2
Registering and finding a to-do server
Register the current process with :global.register_name({:todo_list, "bob"}, self()). A second node cannot claim the same alias, while :global.whereis_name({:todo_list, "bob"}) returns the registered process PID. GenServer can use {:global, alias} as its name. section 12.1.3 section 12.2.2
Listing 12.2 Global registration of to-do servers (todo_distributed/lib/todo/server.ex)
defmodule Todo.Server do
...
def start_link(name) do
GenServer.start_link(Todo.Server, name, name: global_name(name))
end
defp global_name(name) do
{:global, {__MODULE__, name}} #1
end
...
end
Shows the minimal GenServer change that replaces local registration with a global name. section 12.2.2
