How to 'NOT' abuse GenServer Construct in OTP

As a novice, after learning about processes, I unfortunately see the GenServer can be used everywhere and anywhere. Possibly because I come from the Java World and still think in terms of objects, therefore seems my reasoning is impaired to think in a functional paradigm.

What exactly is the GenServer and its purpose? More precisely, as I already know the official definitions and purpose of GenServer, what is the WRONG way to use the GenServer? How can it
be abused?

To clarify how a novice (like me) thinks even after much reading, when I hear genserver, the first thing that comes to mind is a way to make my code faster :-), and after some thought, to identify how a computing task can be broken into independent components which can be processed concurrently :-D. Where is my thinking stinking?

Unfortunately, i cannot think where GenServer CANNOT be used. As for a Sequential Task,(the closest I could think of is (non-commutative) mathematical operations which involve multiplication and addition in a given specific order), I feel the GenServer functions can be written, using conditionals, in a such a way they follow a specific order. Is this ABUSE of GenServer?

Thanks! :smiley:

Maybe this post can help You.

https://www.theerlangelist.com/article/spawn_or_not

6 Likes

I second that. IMHO this is one of the required readings for anyone learning to program in Elixir or Erlang.

4 Likes

Have a look here:

2 Likes

The function Task.async_stream is a good place to start for things like this.

On the other hand, poolboy uses “workers” that are GenServers to handle bounded concurrency.

Keep in mind that splitting up work into independent processes will only help when the work takes more time than starting processes and collecting the results. The BEAM makes those things way way smaller than, say, calling fork - but they still aren’t free.

1 Like