Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

elixir supervisors
supervisors stra

What are the strategies available to supervisors in Elixir?

Muhammad Ali Shahid

What are supervisors in Elixir?

The supervisor is a specialized process with only one purpose: to monitor other processes. It supervises the complete cycle of the process from start to finish. The supervisors help us develop fault-tolerant applications by automatically restarting the process when they fail.

Let’s look at the example below:

Supervisor supervising three processes

In the example mentioned above, the supervisor supervises three child processes. In step 1, all three processes are up and running. In the second step, process 2 crashes. The supervisor starts the process again. All three processes are up and running in step 3.

Types of strategies

Let us look at the strategies that are available to the supervisor to restart the crashed processes. We will discuss the three strategies available in Elixir. These are listed below:

  • one_for_one
  • one_for_all
  • rest_for_one

The one_for_one process

The one_for_one process is what we normally expect from a supervisor. Whenever a process crashes, the supervisor will spin up a new instance of that process and replace it with the crashed process.

This is summarized in the image below. It shows three processes linked to a supervisor. The supervisor will replace the process P:2 with a new instance when it crashes.


The one_for_all process

The one_for_all process is similar to one _for_one in the sense that it restarts a process when it crashes. However, once a process dies, the supervisor also kills all the other processes and restarts each one of them. As can be seen in the example below, when process P:2 crashes, processes P:1 and P:3 are also killed and restarted.



The rest_for_one process operates differently compared to the other two strategies. The first two strategies do not consider the order in which the processes are added to the supervisor. In contrast, when a process crashes in rest_for_one, the supervisor restarts that process, as well as all the processes that were added after the crashed process.

In our example, this would mean that when process P:2 crashes, processes P:2 and P:3 will get restarted, but P:1 would stay intact, because this process started before P:2. We can see this in the example below.



elixir supervisors
supervisors stra


Muhammad Ali Shahid
Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring