jwsinner

jwsinner

From what I can gather, Registry is a way to localize keys to make using OTP stuff a bit easier. This is especially important when you want to use a binary key for a GenServer or Agent instead of an atom or setting it to {:global, key}.

That said, and I’m chalking this up to my own ignorance, it seems to be HIGHLY unintuitive to use. In my mind Registry is basically a supervised key/value store (like a map, but backed by an ets table) where the key responds to a {pid, init_value} tuple (or list of tuples if :duplicate is used). So when we have a function called keys, it makes sense that I’m looking for the keys within that Registry, especially since Registry.count/1 takes in the name of the Registry and returns the number of keys… Instead, we’re expected to also pass along the pid, to get the key(s) associated with it? If I’m using Registry as a way to make GenServer or Agent easier to use (not having to know the pid to pass along), why do I need to know the pid to pass along?

Similarly, Registry.values requires the registry name, a pid, AND the key? Does Map.values/1 require anything other than the map?

I understand there needs to be a difference in usages based on :unique vs :duplicate, but that seems like it could be solved via pattern matching. I like the idea of Registry, but it seems VERY clunky to use for what it’s trying to do. Am I just super ignorant of the underlying mechanisms? If so, I’d love some clarification so I can better understand where I’m falling short.

Showing Posts 1 to 5

LostKobrakai

LostKobrakai

I think the part you’re missing here is that it’s generally expected that both ends – registered processes and processes interested in registered processes – know what keys they’re interested in. That would be the very same as with other registries – local atom names or :global.

Registry.keys and Registry.values are not meant as general tools to finding all keys or all values that are currently registered – besides the fact that this could potentially be quite an expensive operation. They’re much rather helpers for figuring out how a process in question is related in the registry, but you generally wouldn’t used them to interact with registered processes.

Registry.lookup, Registry.match, Registry.select are the APIs you’re expected to use to query the registry (in order of complexity).

entone

entone

I think one other use case that you are missing is the ability to use multiple Registries, which would require knowing the registered name or pid of that individual Registry. So the design of Registry is specifically to allow multiple Registries.

jwsinner

jwsinner OP

Thank you. This does clear up a few things.

I think the confusion comes from the documentation:

A local, decentralized and scalable key-value process storage.

If it’s a key-value storage mechanism, it feels like it should behave a lot like the other “go-to” key-value storage mechanism. But I see how having to concatenate the partitions (if partitioned) would be very expensive if keys or values was used a lot.

I guess I have a sweet tooth and would prefer a bit more syntatic sugar over the Erlang abstractions.

jwsinner

jwsinner OP

Since it’s a key-value storage mechanism, it feels like it should behave like one. Map.keys/1 and Map.values/1 are trivial to use because I just pass in the map I want. It seems like Registry.keys should take in the name associated with the registry created in the supervisor, so Registry.keys(MyApp.GroceryRegistry) should return all of the keys available that have been registered either through Registry.register or {:via, Registry, {MyApp.GroceryRegistry, [name]}.

Instead, to get the available keys, we have to use the Erlang match pattern syntax on a function named select. If it’s a key-value store keys should return the list of keys and values should return the values.

That said, as LostKobrakai mentioned, it’s more about process discovery and use and those processes will know about each other. It’s not supposed to be used EXACTLY like a Map.

josevalim

josevalim

Creator of Elixir

It is a key-value process storage. So you can think of it having three dimensions (key, value, pid), instead of two, and, it also allows duplicate keys with a single process. The process bit is crucial, it is not simply a key-value storage. :slight_smile: If that’s your goal, :ets tables are most likely a better choice.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 91898 914
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews