fireproofsocks

fireproofsocks

The use for keys: :duplicate in Registries?

I find it surprising that :via is not supported for duplicate registries. If you can’t register a pool of processes under the same name what exactly is the use-case for having duplicate-key registries? The docs include an example of dispatching (Registry — Elixir v1.18.0-dev), but I’m not clear how you would even put a process into a custom Registry if :via is not allowed. What am I missing? How do you actually use a custom Registry with duplicate keys?

Most Liked

al2o3cr

al2o3cr

:via isn’t specifically related to Registry, it’s just a fancy way to tell :gen’s name handling to call register_name and whereis_name on a specified module:

(related: Elixir’s wrappers transform a bare atom name into {:local, name})

Conversely, Registry isn’t otherwise related to process naming - it’s just a key-value store that automatically cleans up keys when the registering process exits.

The simplest thing to build with a :duplicate registry is a local publish-subscribe bus:

  • processes that are interested in a topic {:foo, 123} use Registry.register to “subscribe” to that key
  • when a message needs to be delivered for topic {:foo, 123}, the sender uses Registry.dispatch to send it to each PID
  • when a process that’s subscribed to a topic stops, its registration is automatically cleaned up

You might even use both a :unique and a :duplicate registry with the same keys, for instance in the classic “multiplayer game” architecture:

  • the :unique registry tracks a GenServer per “game”. A :via tuple pointing to this registry is used when sending moves, joins, etc to the game
  • the :duplicate registry tracks the Liveview processes of players per “game”, and is used to distribute updates to all players
fireproofsocks

fireproofsocks

Thank you for the informative responses! This is stuff that I’m going to have to ponder about for a while, but I see the set/get is pretty straightforward using Registry.register/3 and Registry.lookup/2:

iex> {:ok, _} = Registry.start_link(keys: :duplicate, name: ClusterRegistry)
iex> Registry.register(ClusterRegistry, :group, "a")
iex> Registry.register(ClusterRegistry, :group, "b")
iex> Registry.register(ClusterRegistry, :group, "c")
iex> Registry.lookup(ClusterRegistry, :group)
[{#PID<0.148.0>, "a"}, {#PID<0.148.0>, "b"}, {#PID<0.148.0>, "c"}]

What is interesting to me is that Registry.register/3 really is all about storing the current process – the value seems almost secondary. The other thing that is interesting is that when a process terminates, anything that it registered gets removed (as. you pointed out Matt):

iex> Task.start(fn -> 
  Registry.register(ClusterRegistry, :group, "hello?") 
  Process.sleep(10_000)
end)
iex> Registry.lookup(ClusterRegistry, :group)
[{#PID<0.148.0>, "a"}, {#PID<0.148.0>, "b"}, {#PID<0.148.0>, "c"}, {#PID<0.148.0>, "hello?"}]

# Wait 10 seconds
iex> Registry.lookup(ClusterRegistry, :group)
[{#PID<0.148.0>, "a"}, {#PID<0.148.0>, "b"}, {#PID<0.148.0>, "c"}]

I feel like I’m only becoming dimly aware of the things that you can do with these tools…

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
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
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement