Registry

A registered name in Elixir, such as the Counter of the GenServer lesson is one atom for one process. A program that starts servers at run time needs a table instead of a fixed atom: Registry is a process that maps a key to one or many pids and answers where the process is.

A name through Registry

Registry.start_link/1 starts the table under a name of its own. A process then registers with a via tuple, and every call that takes a name accepts the tuple in place of an atom:

registry.exs

{:ok, _} = Registry.start_link(keys: :unique, name: MyRegistry) {:ok, pid} = Agent.start_link(fn -> 0 end, name: {:via, Registry, {MyRegistry, :counter}}) IO.inspect(match?([{_, _}], Registry.lookup(MyRegistry, :counter))) IO.inspect(Agent.get({:via, Registry, {MyRegistry, :counter}}, & &1)) IO.inspect(GenServer.whereis({:via, Registry, {MyRegistry, :counter}}) == pid)

elixir registry.exs

true
0
true

The via tuple has three parts: the module that knows how to register, the registry, and the key. The same tuple works in Agent.start_link/2, GenServer.start_link/2 and GenServer.whereis/1, which is why a program passes it around instead of a pid.

lookup and whereis

Registry.lookup/2 answers a list of {pid, value} tuples; the list is empty when the key is not registered. GenServer.whereis/1 takes the via tuple and answers the pid or nil, which is the call a program uses when it only needs the process. The value in the tuple is whatever the process registered, not its state.

Many processes under one key

A unique registry holds one process per key. keys: :duplicate lets several processes share one key, which is how a program keeps a list of workers or channels. Each registration carries a value, so a lookup tells the processes apart:

duplicate.exs

{:ok, _} = Registry.start_link(keys: :duplicate, name: MyRegistry) pid1 = spawn(fn -> Process.sleep(:infinity) end) pid2 = spawn(fn -> Process.sleep(:infinity) end) {:ok, _} = Registry.register(MyRegistry, :workers, {1, pid1}) {:ok, _} = Registry.register(MyRegistry, :workers, {2, pid2}) IO.puts(length(Registry.lookup(MyRegistry, :workers)))

elixir duplicate.exs

2

The key is the same and the two entries are both there. A registration disappears when the process that made it ends, so a registry does not keep a dead pid: the cleanup is part of the process exit, and a program that reads the table always sees the live workers.

Dispatch to every entry

Registry.dispatch/3 runs one function over every entry of a key in the process that owns the registry. It is the broadcast of the registry: the function receives the list of entries and does its work there, and the call answers :ok:

dispatch.exs

{:ok, _} = Registry.start_link(keys: :duplicate, name: MyRegistry) for n <- 1..3 do pid = spawn(fn -> Process.sleep(:infinity) end) {:ok, _} = Registry.register(MyRegistry, :workers, {n, pid}) end parent = self() :ok = Registry.dispatch(MyRegistry, :workers, fn entries -> Enum.each(entries, fn {_pid, {n, _}} -> send(parent, n) end) end) sum = for _ <- 1..3 do receive do n -> n end end |> Enum.sum() IO.puts(sum)

elixir dispatch.exs

6

The callback runs inside the registry, so it should be short: a long function there blocks every registration of the table. A program that needs to call each worker sends a message from the callback and lets the workers do the work, which is what the example does with send/2.

The mistake: a unique key taken twice

A unique registry refuses the second registration of a key. The error carries the pid that holds the key, which changes on every run, so the program matches the shape and prints its own line:

taken.exs

{:ok, _} = Registry.start_link(keys: :unique, name: MyRegistry) pid1 = spawn(fn -> Process.sleep(:infinity) end) pid2 = spawn(fn -> Process.sleep(:infinity) end) {:ok, _} = Registry.register(MyRegistry, :worker, :one) case Registry.register(MyRegistry, :worker, :two) do {:error, {:already_registered, _pid}} -> IO.puts("the name is taken") :ok -> IO.puts("registered") end IO.inspect(Process.alive?(pid1) and Process.alive?(pid2))

elixir taken.exs

the name is taken
true

The second process is alive and has no name, which is why the call failed. The fix is keys: :duplicate when several processes share a key, or a key per process, for example a tuple of the worker name and a number, when each one needs its own name.

The programs on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does a via tuple name?

    Answer

    A process in a registry, by registry and key

    The tuple names the registry module, the registry and the key of the process.

  2. What does Registry.lookup/2 answer?

    Answer

    A list of {pid, value} tuples

    A key can have several entries with :duplicate, so the answer is always a list.

  3. What is the difference between keys: :unique and keys: :duplicate?

    Answer

    Unique allows one process per key; duplicate allows several

    A unique key refuses the second registration; a duplicate key accepts many.

  4. What does Registry.dispatch/3 return?

    Answer

    :ok

    The callback runs inside the registry and dispatch answers :ok.

  5. What does this program print? {:ok, _} = Registry.start_link(keys: :unique, name: MyRegistry) {:ok, pid} = Agent.start_link(fn -> 3 end, name: {:via, Registry, {MyRegistry, :a}}) IO.inspect(GenServer.whereis({:via, Registry, {MyRegistry, :a}}) == pid)

    Answer

    true

    whereis reads the same key and answers the pid the agent registered.

  6. What does this program print? {:ok, _} = Registry.start_link(keys: :duplicate, name: MyRegistry) a = spawn(fn -> Process.sleep(:infinity) end) b = spawn(fn -> Process.sleep(:infinity) end) {:ok, _} = Registry.register(MyRegistry, :k, :a) {:ok, _} = Registry.register(MyRegistry, :k, :b) IO.puts(length(Registry.lookup(MyRegistry, :k)))

    Answer

    2

    A duplicate key keeps both registrations.

  7. What does this program print? {:ok, _} = Registry.start_link(keys: :unique, name: MyRegistry) a = spawn(fn -> Process.sleep(:infinity) end) {:ok, _} = Registry.register(MyRegistry, :k, :a) case Registry.register(MyRegistry, :k, :b) do {:error, {:already_registered, _}} -> IO.puts("taken") :ok -> IO.puts("free") end

    Answer

    taken

    The unique key is held by the first process, so the second registration is refused.

  8. What happens to a registration when the process that made it ends?

    Answer

    It is removed with the process

    The registry monitors its members and removes the entry when one ends.

  9. Why should a Registry.dispatch/3 callback be short?

    Answer

    Because the callback runs in the registry and blocks its other work

    The callback runs inside the registry process, so a long one delays every registration and lookup.

  10. Which call is right when a program needs the pid behind a via tuple?

    Answer

    GenServer.whereis/1 with the via tuple

    whereis answers the pid or nil for a via tuple, which is the direct question.

Article author: Arthur Isaev