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 as well as to restart critical processes if they fail.

As was mentioned in section 9.1.6, all processes 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.

10.1 Tasks

10.1.1 Awaited tasks

10.1.2 Non-awaited tasks

10.1.3 Supervising dynamic tasks

10.2 Agents

10.2.1 Basic use

10.2.2 Agents and concurrency

10.2.3 Agent-powered to-do server

10.2.4 Limitations of agents

10.3 ETS tables

10.3.1 Basic operations

10.3.2 ETS-powered key-value store

10.3.3 Other ETS operations