10 Beyond GenServer
This chapter covers
- Tasks
- Agents
- ETS tables
Chapters 8 and 9 introduced the distinction between worker and supervisor processes. Workers are the processes that provide some part of your service, whereas supervisors organize the worker processes into a tree. This allows you to start and stop processes in the desired order, and also to restart critical processes if they fail.
As was mentioned in section 9.1.6, all processes that are started directly from a supervisor should be OTP-compliant processes. Processes started with plain spawn and spawn_link are not OTP-compliant, so you should refrain from running such processes in production. Modules such as Supervisor, GenServer, and Registry allow you to start OTP-compliant processes that can be placed into a supervision tree.
In this chapter you’ll learn about two additional modules that also allow you to run OTP-compliant workers: Task and Agent. Tasks can be very useful when you need to run one-off jobs, whereas agents can be used to manage state and provide concurrent access to it. Finally, we’ll discuss a related feature called ETS tables, which under some conditions can serve as more efficient alternatives to GenServer and Agent.
There’s a lot of new ground to cover, so let’s start by discussing tasks.