When do I use a GenServer?
Notes from the podcast Elixir Fountain 2015-12-04 with Proctor.
Proctor, quoting Jose Valim on when to use a GenServer…
If you only want behavior and you don’t want state, you can use a Task. If you only want state and you don’t want behavior, use an Agent. Then you can start to delineate those things that are stateful and just need to hold state in some area and those things that are just running and don’t need state (Task). Then a GenServer can combine the in-between when there is state and there is behavior in the process.
Johnny Winn responded with…
You can start with a GenServer. Agents and Tasks are abstractions around GenServers. You can start with a GenServer when you don’t really know what you have.
The idea being:
Start with a GenServer when you are unsure what you will end up needing. Then as you refine the need, you may refactor it into an Agent or a Task, or leave it as a GenServer.
I liked the rule-of-thumb discussion and the practical approach.
Do you have any tips you follow for when to choose a GenServer, an Agent or a Task?