The scope of process name?


When starting an agent with a name like:

{:ok, pid} = Agent.start_link( fn -> n end, name: :process_name)

Is the :process_name reachable from all nodes of cluster? can I get it on any node using Agent.get ?


That Agent can be reached from anywhere


I tried to search docs for this info, but I did not find it, are you sure?


I read it somewhere in Programming Elixir book by Dave Thomas


Almost correct @elixirnewbie! That process will only be accessible locally via the name :process_name, but not from other nodes connected to the one starting the agent.

To make the agent available to all connected nodes (and nodes that will connect in the future), you have to use the tuple {:global, :process_name}, both when starting the process and fetching it through the Agent functions. Like so:

# In node 1
{:ok, pid} = Agent.start_link(fn -> 10 end, name: {:global, :process_name})
# {:ok, #PID<0.105.0>}

# In node 2
Agent.get({:global, :process_name}, fn i -> i end)
# 10

# same applies to node 1 itself
Agent.get({:global, :process_name}, fn i -> i end)
# 10


Thanks @lucidstack for answer, I could not find a clear info at docs, only global is mentioned, without cluster context…


I also found this in Programming Elixir book:

:global.register_name(@name, pid)

which uses erlang module:


By way of extra clarification:

The scope of a name is defined by the registry that the name is registered in. When you do name: :foo you’re using a built in registry that is scoped to a single node. When you use {:global, :foo} you’re using a different registry that scopes names to a cluster.

You can do {:via, SomeCrazyRegistry, :foo} and that will let a custom registry named SomeCrazyRegistry do the work. It could only remember names given during a full moon or whatever.

Basically, it depends on the registry being used.