sezaru

sezaru

Hello,

A part of my application currently have a supervisor hierarchy that looks something like this:

As you can see, I have a Manager Genserver and a Childs Supervisor that will contain a lot of children’s (this is a normal supervisor for now, but maybe in the future I would want to start the children on demand, so that can be changed to a Dynamic Supervisor in the future I guess).

My question is regarding the Manager Genserver communication with the children’s. Basically Manager Genserver needs to send broadcast messages to all the children’s, this can happen very frequently (like multiple times a second), so I have a performance concern.

So, what kind of communication strategy should I use in this case? I’m kinda new with OTP, so I’m not 100% sure and are contemplating the following solutions:

  1. Create a function inside Childs Supervisor that will iterate and do a GenServer.call to all it’s children. Not sure if this is efficient or will bottleneck all the communication if one children blocks the call for some reason;

  2. Send a message from each Child to the Manager GenServer with it’s PID, store it in a list and send GenServer.call iterating through it;

  3. Use a PubSub system like Phoenix.PubSub. This seems like a good solution, but as far as I know the Phoenix.PubSub is global and used more for a distributed case. I’m not sure if using it would be over-engineering and maybe be a bottleneck;

  4. Use the Registry. this seems like another good solution too but I only saw examples of global (but local to the beam node) examples of Registry, in my case I only want to handle communication from Manager Genserver to all the Childs, is it possible to create maybe a local Registry for Parent Supervisor?

Any help would be much appreciated.

Thanks a lot!

Showing Posts 1 to 6

kokolegorille

kokolegorille

Supervisor has the which children function (and more…)

You should try to benchmark if sending message to that list of children is within your refresh frequency.

lud

lud

I’d go the same way as @kokolegorille but if for some reason it could not satisfy your requirements I would implement a pub-sub pattern with a registry.

tty

tty

Is every child process using every message broadcast by the Manager GenServer ?

(2), (3) and (4) are common techniques with each having their own pro/cons.

sezaru

sezaru OP

Yes, manager will send a message that all child process need to receive and process it.

sezaru

sezaru OP

So, I created a simple project to test the solutions, https://github.com/sezaru/msg_bench_test.

I implemented 3 solutions for now:

  1. Childs send PID to Manager and Manager loops through it and send a GenServer.call message;

  2. Childs send PID to Manager and Manager loops through it and send a GenServer.cast message;

  3. Create a local Registry, child’s subscribe to receive a type of message and Manager send that message through Registry.

I didn’t implement the Supervisor which children one since it should be very similar in performance with 1) and 2).

If you are interested in test the code, you can run it inside Iex and run the following code:

MsgBench.run_msg_manager(:msg_call)
MsgBench.send_msg(:msg_call)

For 1).

MsgBench.run_msg_manager(:msg_cast)
MsgBench.send_msg(:msg_cast)

For 2).

MsgBench.run_msg_manager(:registry)
MsgBench.send_msg(:registry)

For 3).

1). Simply timeouts since it will do the call sequentially and will take a long time to finish (I added a :timer.sleep(1_000) to each child when processing the message).

2). seems to work great, but I feel that I shouldn’t abuse GenServer.cast too much and I guess I wouldn’t have any guarantee that the message was delivered to all childs.

3). Looks like it works as good as 2), but I’m not sure (and couldn’t find it in the documentation) if I have any kind of guarantee that my childs will receive the message when I call the Registry.dispatch.

Also, I’m not so sure how to modify my code so I can really see the overhead difference between each solution.

lud

lud

Your 1) timeouts because you implemented it inside a handle_call and call it from another process. But this problem is out of scope. You could add a :infinity timeout.

You could call all children concurrently:

    reply = state.childs
    |> Map.values()
    |> Enum.map(fn pid -> Task.async(fn -> GenServer.call(pid, {:received_msg, "hello"}) end) end)
    |> Enum.map(&Task.await/1)

Edit: but this is a poor man’s implementation of GenServer.multi_call.

Or use Task.async_stream to limit the number of concurrent calls.

Calling your children with GenServer.call provides another guarantee : all calls will be made before you start to call your children again. The other solutions do not give this guarantee. But you may not care about it.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews