fireproofsocks

fireproofsocks

Difference between Registry.lookup/2 and Process.whereis/1

I’m starting to build more complex apps and I’m slowly reaching for the built-in Registry module. I’ve been reading GenServer — Elixir v1.20.2 a lot. I can feel that there is a lot of depth and subtlety to process registration, but I’m just trying to understand some of the basics first.

Conceptually, Registry.lookup/2 and Process.where/1 seem to serve a similar purpose in that theyboth can resolve an atom representing a named process and yield a PID. But they aren’t the same, e.g.

iex(2)> {:ok, _} = Registry.start_link(keys: :unique, name: Registry.ViaTest)
{:ok, #PID<0.112.0>}
iex(3)> Agent.start_link(fn -> 0 end, name: {:via, Registry, {Registry.ViaTest, :agent1}})
{:ok, #PID<0.115.0>}
iex(4)> Process.whereis(:agent1)
nil

Can someone explain to me why these return different results? I infer that :via provides a kind of “namespace” but I would like more clarity. Thanks for any wisdom!

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Process.whereis uses the built in, atom based registry that ships with the VM itself. When you give a GenServer (or other OTP process) a name that is a bare atom, the OTP behaviors will use this registry. If you give it a via tuple, it’ll use that other registry.

The difference you get with using the Elixir Registry vs the built in registry is:

  1. Key flexibility: you aren’t limited to atoms.
  2. “namespacing”. Naming with the built in registry can be annoying when you have clusters of processes that you want to be accessible to each other by name, but you need N of those clusters. This also makes testing easier because you can spawn a registry + cluster of processes for each test in parallel.
  3. Other registry features (pubsub, duplicate keys, etc).
LostKobrakai

LostKobrakai

Yes. Both are registries, but with different capabilities. OTP by default ships with two types of process registries and elixir ships with another one.

  • OTP - Local registry
    • Supplied with name: value
    • Allows only atoms as keys
    • Uniqueness only per node
  • OTP - :global
    • Supplied with name: {:global, value}
    • value key can be any term
    • Uniqueness for the whole cluster
  • Elixir - Registry
    • Supplied with via tuples name: {:via, mod, value}
    • value can be {registry_name, key} or {registry_name, key, value}
    • Uniqueness only per node (if enabled)
    • Supports process registration without unique keys

There are also various third party registries, which also work with via tuples, but might otherwise come with completely different tradeoffs based on their implementations.

Now Process.whereis only integrates with the first listed option - local registration by atom. GenServer.whereis however works with any registry (built in or via tuples). Registry.lookup (and a lot of the other APIs on the module) exist to support more complex and explicit usage of the registry, especially around the non-unique key usecase, which doesn’t integrate with via tuples.

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement