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.15.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

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New

Other popular topics Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
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