markusgod

markusgod

Are RPC calls the only solution for accessing locally registered processes across nodes using a hash ring?

Hi everyone,

I’m working on a distributed Elixir application where I use a hash ring to distribute processes across multiple nodes. Each node has processes that are registered locally using Registry, and they’re identified by a unique name. I want to send messages to these processes based on the hash ring mapping.

Here’s a simplified version of my code:

defmodule Worker do
  use GenServer

  def start_link(name) do
    # Start the GenServer and register it locally
    GenServer.start_link(__MODULE__, [], name: via_tuple(name))
  end

  def send_message(name, message) do
    # Get the target node from the hash ring
    target_node = HashRing.get_node(name)
    
    # Attempt to send a message to the process on the target node
    GenServer.cast({target_node, via_tuple(name)}, {:message, message})
  end

  # Helper for local registration via Registry
  defp via_tuple(name) do
    {:via, Registry, {MyApp.Registry, name}}
  end

  # Callbacks
  def init(_) do
    {:ok, %{}}
  end

  def handle_cast({:message, message}, state) do
    IO.puts("Received message on node #{Node.self()}: #{message}")
    {:noreply, state}
  end
end

The issue I’m facing is that since Registry is local to each node, the GenServer.cast doesn’t reach the process on the remote node because it tries to resolve the name locally. I found that using an RPC call works:

def send_message(name, message) do
  target_node = HashRing.get_node(name)
  :rpc.call(target_node, GenServer, :cast, [via_tuple(name), {:message, message}])
end

However, I’m wondering:

  1. Is using RPC calls the only solution to send messages to processes registered locally on other nodes?

  2. Can I configure Registry to be distributed across nodes so that I can avoid using RPC?

  3. Are there better patterns or best practices for accessing named processes on remote nodes when using local registries and a hash ring?

I’ve considered using a distributed Registry or :global for process registration, but I’m concerned about scalability and potential bottlenecks.

Any advice or suggestions on how to effectively communicate with locally registered processes across nodes would be greatly appreciated!

Thanks in advance!

Where Next?

Popular in Questions Top

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement