🕵️ SicherheitslückenWhat continuous operational resilience looks like under DORA(09.09.2026 um 17:53 Uhr)
🔧 AI Nachrichten OpenAI seeks tougher AI rules. CIOs may feel the ripple effects(10.09.2026 um 12:11 Uhr)
🔧 AI Nachrichten Mistral valued at €21bn after €3bn Series D funding round(08.09.2026 um 10:19 Uhr)
🪟 Windows TippsWindows XP's Cursor Indicator Is Getting a Windows 11 Refresh(25.08.2026 um 13:00 Uhr)
🕵️ SicherheitslückenWhat continuous operational resilience looks like under DORA(09.09.2026 um 17:53 Uhr)
🔧 AI Nachrichten OpenAI seeks tougher AI rules. CIOs may feel the ripple effects(10.09.2026 um 12:11 Uhr)
🔧 AI Nachrichten Mistral valued at €21bn after €3bn Series D funding round(08.09.2026 um 10:19 Uhr)
🪟 Windows TippsWindows XP's Cursor Indicator Is Getting a Windows 11 Refresh(25.08.2026 um 13:00 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 4 Min Lesezeit
0

Creating Named GenServers in Elixir Using Registry

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

In this guide, we’ll explore how to use Registry along with a Dynamic Supervisor to create named GenServers.


Let's start by creating a simple GenServer




CODE
defmodule Server do
use GenServer

def start_link({init_arg, name}) do
GenServer.start_link(__MODULE__, init_arg, name: name)
end

@impl true
def init(arg) do
{:ok, arg}
end

def state(name) do
GenServer.call(name, :state)
end

@impl true
def handle_call(:state, _from, state) do
{:reply, state, state}
end
end









CODE
{:module, Server, <<70, 79, 82, 49, 0, 0, 19, ...>>, {:handle_call, 3}}






Now, we can start a new instance of the GenServer and attempt to retrieve its current state.




CODE
initial_value = 123
server_name = :server
Server.start_link({initial_value, server_name})









CODE
{:ok, #PID<0.160.0>}









CODE
Server.state(:server)









CODE
123






Note that the name cannot be a string:






CODE
Server.start_link({123, "server"})









Dynamic Supervisor



With our GenServer ready, the next step is to create a Dynamic Supervisor that will manage and start our GenServers.




CODE
defmodule DSupervisor do
use DynamicSupervisor

def start_link(init_arg) do
DynamicSupervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
end

def init(_init_arg) do
DynamicSupervisor.init(strategy: :one_for_one)
end

def start_server(initial_value, name) do
DynamicSupervisor.start_child(__MODULE__, {Server, {initial_value, name}})
end
end










CODE
{:module, DSupervisor, <<70, 79, 82, 49, 0, 0, 10, ...>>, {:start_server, 2}}






Now, we can start the Dynamic Supervisor and use it to launch another GenServer.




CODE
DSupervisor.start_link(1)









CODE
{:ok, #PID<0.170.0>}









CODE
DSupervisor.start_server(321, :test)









CODE
{:ok, #PID<0.171.0>}









CODE
Server.state(:server) |> IO.inspect()
Server.state(:test)









CODE
123









CODE
321









So, what’s the issue?



Naming a GenServer using atoms is generally not a problem. However, if your application dynamically creates GenServers with different names—such as when users start GenServers by creating a new match in a browser game or starting a new chat room—this approach can lead to problems.



Atoms are not garbage collected, and dynamically generating them as names for GenServers increases memory usage over time. Eventually, this can exhaust the atom table and cause the application to fail.



To solve this, we need a way to associate a custom name with a GenServer while storing its PID. Fortunately, Elixir provides a built-in solution for this: the Registry.




CODE
Registry.start_link(keys: :unique, name: Registry)









CODE
{:ok, #PID<0.172.0>}






Now we can use the Registry to register process names.




CODE
name = {:via, Registry, {Registry, "cool_name"}}
DSupervisor.start_server(42, name)









CODE
{:ok, #PID<0.174.0>}









CODE
Server.state(name)









CODE
42






The Registry allows us to fetch the PID of a process using the lookup function, which we will use in this guide to terminate a server.




CODE
[{pid, _value}] = Registry.lookup(Registry, "cool_name")
DynamicSupervisor.terminate_child(DSupervisor, pid)









CODE
:ok






Registry will monitor the GenServer and automatically remove the registration when the GenServer is terminated.






CODE
Server.state(name)






By using the Registry, we can safely register and manage names for GenServers with ease, ensuring that each process is uniquely identifiable and accessible. The Registry also helps by automatically cleaning up registrations when a GenServer is terminated, preventing memory leaks and improving the efficiency of your application.



With this approach, you can build scalable, maintainable systems where users can dynamically create and interact with multiple GenServers without worrying about the limitations of atom-based naming.






A few noteworthy details



When starting the Registry or the Dynamic Supervisor, the




CODE

we use sometimes look like a module, but they are just atoms, they could be named differently, like

```:registry```

or

```:supervisor```





```elixir
Registry.start_link(keys: :unique, name: :registry)
name = {:via, Registry, {:registry, "another_name"}}
DSupervisor.start_server(42, name)









CODE
{:ok, #PID<0.5230.0>}






Interacting with the Registry could be encapsulated within the GenServer, making it easier to work with.






CODE

defp registry_name(name), do: {:via, Registry, {:registry, name}}

def start_link({init_arg, name}) do
GenServer.start_link(__MODULE__, init_arg, name: registry_name(name))
end

def state(name) do
GenServer.call(registry_name(name), :state)
end


Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Sam Altman calls GPT-6 Astra rollout ‘messy’ as enterprise users wait for access
1 Quelle
Swiss government explores replacing Microsoft 365 with open-source software
1 Quelle
What continuous operational resilience looks like under DORA
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Creating Named GenServers in Elixir Using Registry

Thematisch verwandte Begriffe: Creating, Named, GenServers, Elixir · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...