DEV Community

Mark Nefedov
Mark Nefedov

Posted on

Understanding {:via, Registry, {...}} vs Registry.register/3 in Elixir

Understanding {:via, Registry, {...}} vs Registry.register/3 in Elixir

If you've worked with processes and registries in Elixir, you've likely encountered both the {:via, Registry, {...}} syntax and the Registry.register/3 function. At first glance, these two may appear to do the same thing — allowing you to register processes with a registry. However, they have different purposes and use cases. In this post, we'll explore what each of these does, how they differ, and when to use each.

Background: Why Use a Registry?

In Elixir, registries offer a way to name processes and access them by a unique identifier rather than just a process ID (PID). Instead of passing around PIDs, which can be tricky to manage and track, you can register a process under a name in a registry. This allows you to reference it more easily and enables features like process discovery and message passing.

Elixir’s built-in Registry module provides functions for managing named registries, and with it, you can register processes and retrieve them by a unique identifier (like a room name, session ID, etc.). Now, let’s look at the two methods.

Method 1: {:via, Registry, {...}} Tuple

The {:via, Registry, {...}} tuple is a special syntax used for starting or referencing a process with a registry. This tuple signals to Elixir that you want to look up or register a process via a custom registry and associate it with a key.

The {:via, Registry, {...}} pattern is typically used as the name argument when starting a process with GenServer.start_link/3 or when sending a message to a process registered with a registry.

Here's an example:

GenServer.start_link(MyModule, [], name: {:via, Registry, {ServerAlpha.RoomRegistry, room_id}})
Enter fullscreen mode Exit fullscreen mode

In this example, we’re instructing the GenServer to:

  1. Register its PID with the registry ServerAlpha.RoomRegistry.
  2. Associate the process with the key room_id.

Now, any process can find or send messages to this process by referencing it through {:via, Registry, {ServerAlpha.RoomRegistry, room_id}}, rather than needing the process’s PID directly.

Why Use It?

Using {:via, Registry, {...}} with GenServer.start_link/3 makes the registration part of the startup process, eliminating the need for a separate registration call. It’s a convenient and consistent way to automatically register a process in a registry upon its creation.

Method 2: Registry.register/3

The Registry.register/3 function, on the other hand, is a direct function call used to manually register the current process with a registry. You call this function within the process you want to register, typically right after it starts.

Here’s how it might look:

{:ok, _} = Registry.register(ServerAlpha.RoomRegistry, room_id, somedata)
Enter fullscreen mode Exit fullscreen mode

This line registers the current process (from where this function is called) with ServerAlpha.RoomRegistry under the key room_id. Typically, the somedata could be any term, such as room metadata.

This approach requires the process to register itself after it has been started, making it more suitable for cases where the registration process involves additional logic or metadata.

Why Use It?

With Registry.register/3, you can choose when and where to register the process, allowing more flexibility. This can be especially useful if you need to set custom metadata for the registered process or if the registration conditionally depends on certain initialization steps.

Key Differences: {:via, Registry, {...}} vs Registry.register/3

Now that we understand each method, let’s summarize their differences:

Feature {:via, Registry, {...}} Registry.register/3
Purpose Start or reference a process with a registry. Manually register the current process.
Automatic vs. Manual Registration Automatic registration upon process start. Manual call to register after process start.
Usage Context Use with GenServer.start_link/3 or similar. Call within process for conditional registration.
Metadata Support Not intended for custom metadata. Allows additional metadata upon registration.

When to Use Each

  • Use {:via, Registry, {...}} when you want to make the registry registration part of the process's startup flow. This method is concise and reliable for consistently registering a process upon its creation, making it a great choice for processes with predictable, uniform registration needs.

  • Use Registry.register/3 when you need more control over the registration. For example, if you need to perform some additional setup before registering or want to include custom metadata with the registration, Registry.register/3 offers that flexibility.

Practical Example: A Room-Based Server

Let’s imagine a chat server where each room has its own GenServer process. You want to register each room process in a registry so that clients can look up rooms by room IDs.

Using {:via, Registry, {ServerAlpha.RoomRegistry, room_id}}, you could start a GenServer for a room and automatically register it under its room ID:

def start_room(room_id) do
  GenServer.start_link(RoomServer, room_id, name: {:via, Registry, {ServerAlpha.RoomRegistry, room_id}})
end
Enter fullscreen mode Exit fullscreen mode

Clients can then look up or interact with room servers directly by referencing {:via, Registry, {ServerAlpha.RoomRegistry, room_id}}.

Alternatively, if your GenServers require some initialization before they’re registered, you might start each GenServer without registering it, and then call Registry.register/3 once it’s fully set up:

def init(room_id) do
  # Perform setup
  Registry.register(ServerAlpha.RoomRegistry, room_id, :undefined)
  {:ok, %{}}
end
Enter fullscreen mode Exit fullscreen mode

This way, registration only happens after the necessary setup steps are complete.

In Summary

While {:via, Registry, {...}} and Registry.register/3 may look similar at first, they have distinct roles in process management:

  • Use {:via, Registry, {...}} for automatic, inline registration when starting a process.
  • Use Registry.register/3 for more control, allowing manual registration and customization.

Top comments (0)