DEV Community

Discussion on: GenServer, Agent, Task

Collapse
 
girorme profile image
girorme

To complement the article, i like this snippet from pragprog:

Here are a few heuristics to help you get started on the right foot:

  • Use a Task if you want to perform a one-off computation or query asynchronously.
  • Use an Agent if you just need a simple process to hold state.
  • Use a GenServer if you need a long-running server process that store states and performs work concurrently.
  • Use a dedicated GenServer process if you need to serialize access to a shared resource or service used by multiple concurrent processes.
  • Use a GenServer process if you need to schedule background work to be performed on a periodic interval.

Don't spend too much time fretting over the decision. You can start with an Agent, for example, and then migrate it to a GenServer if the process needs to do more than simply store state.