Supervisors

Look at the details of supervisors in Elixir and practice it.

Role of supervisors in Elixir

Most new Elixir developers tend to think of supervisors in terms of fault tolerance because they provide the restart strategies that are an essential part of building reliable systems. Supervisors are so much more. They form the backbone of Elixir applications.

Ultimately, supervisors are responsible for how our processes start and shut down, whether an application is crashing, restarting, or simply starting. Restarting processes is optional, while starting and stopping them is essential. Let’s explore the startup flow by addressing a bug in our PathAllocator implementation defined earlier.

Starting a supervisor is a matter of defining all its child specifications and then calling start_link. A child specification specifies exactly how the supervisor starts a child process, when and how many times to restart it, and how to shut it down. For a complete reference on child specifications, consult the Elixir documentation for supervisors.

Example of a supervisor

We can start the PathAllocator example we have seen earlier, under a supervisor by defining a list of children. Here, each element is a tuple with the module name as the first element and the argument given to start_link as the second argument, like this:

Get hands-on with 1200+ tech skills courses.