Fl4m3Ph03n1x

Fl4m3Ph03n1x

Background

I have an application that upon start creates 100 processes. Now, every time I need to perform a specific operation (say, calculate the size of my ego :scream:) I pick one of those processes at random to perform the calculation.

So now I need to access those processes, which means I need to save them somewhere, aka, I need state.

State via named ETS tables

This solution advocates that when starting my app, I create all the processes and save their pids into an ETS table with a name (which is a singleton in the end).

This solution is simple, and works as a cache (so far so good), but I really don’t like singletons in my apps because they make for code poorly testable. I could create a mock of the singleton and use it for tests, but I wonder if there is simpler way.

State via GenServer

After reading pragmatic’s Dave approach to components, I understand that he (and some erlangers) prefer to manage state via GenServers. This would mean I need processes to communicate with this GenServer and would add a lot of boiler plate code to my app, not to mention I believe this is quite overkill for the problem at hand - I just want a table of pids after all.

Questions

So this brings me to a couple of questions:

  1. Are there other ways to manage State in Elixir?
  2. Which approach (ets or genserver) would you choose and why?

PS: the size of my ego is the same as the size of my intelligence: NaN (so small doesn’t even qualify :stuck_out_tongue: )

Showing Posts 1 to 10

shanesveller

shanesveller

I would say that this isn’t a question of state so much as of process registration. The workers themselves sound stateless in your description. Any one of them is capable of fulfilling a request, and none of the persist or share anything between subsequent requests.

Read up on how GenServer’s allow various forms of name registration. Next, take a look at :global from Erlang and Registry from the Elixir stdlib. If you just need a constant-sized worker pool where any of them are used to handle a given single invocation, look at the :poolboy Erlang library. One of these should fit your use-case.

https://github.com/devinus/poolboy

alvises

alvises

If I understand correctly you need a process pool. You can use something like Poolboy. With Poolboy you can setup a supervisor that supervises your processes.

kokolegorille

kokolegorille

Why not use Registry?

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

I know of poolboy but I choose not to use it for this specific case, reason being, that having a pool of processes was an analogy to make the problem easier to understand.

What actually ends up happening is that I have around 10_000 gun connections open and I need to save them somewhere. Upon opening a connection (which is in reality a stream) it remains open forever, waiting for me to send requests for it to send to some domain.

This is why it makes no sense to use poolboy here. In any other case, I would agree.
Should I updated the problem and get rid of the analogy?

alvises

alvises

Do you need to broadcast the same message to all of them?

You can use a PubSub system. Take a look at :pg2.
Registry is great when each single process needs to have a name/id. In your case you just need to have access the processes all-together. :pg2 also monitors your processes so if anyone crashes, it’s automatically removed from the group.

P.S. It’s also possible to use pg2 on multiple distributed node, but be careful about this because if I remember correctly the replication over multiple nodes is made locking the pg2 processes.

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

I don’t need to broacast a message. I need each process to accept a given command and make a request to a domain.

My main question now is: What happens if a connection in Registry dies?

Thanks for the pubsub idea though!

alvises

alvises

If the connection process dies it’s automatically removed from the registry. Registry monitors the processes like pg2.

If the processes are from the same module, I would use :pg2, or some sort of pubsub mechanism, to group them together without reinventing the wheel. You could do it with a DynamicSupervisor where you just add the new processes to the supervisor and asks to the supervisor the processes list..

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

How would the process get a command if not by receiving a message? Notably if they’re inside a registry you don’t also need pg2, you can use Registry to broadcast, there are examples in the registry docs.

gregvaughn

gregvaughn

The same thing that happens when any other BEAM process dies: it depends on what supervisor it has. The Registry also monitors the process and will de-list a dead process. But the supervisor can restart the process and re-register the new instance with the Registry.

peerreynders

peerreynders

You need to differentiate between between Singleton and Just Create One.

  • If you pass in a handle or name when the process spawns it is more like “Just Create One” because you don’t depend on any globally fixed information.

That being said having 10000 processes pounding on one, single process can still yield all the disadvantages of “being shared” (singleness) in the form of a bottleneck - so a single process isn’t necessarily a good replacement for a single ETS table (depending on the circumstances of course).

I still think that you have to reveal the complete interaction pattern before it can become clear what the best solution is.

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 94592 917
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
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
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
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
AstonJ
Since we have deprecated our Erlang sections (as we have dedicated Erlang Forums now) let’s add this thread for those who’d like to post ...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
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
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
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews