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 ) 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.
So this brings me to a couple of questions:
- Are there other ways to manage State in Elixir?
- 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 )