Supervisors In Elixir

A demo of supervisor strategies and when to use them

Bobby Grayson
Flatiron Labs
7 min readApr 3, 2019

--

This blog post was originally published on Elixir School.

One of the things that makes OTP and Elixir unique is the model of supervisor behaviour that applications can take with different processes they start. In this post, we will examine each of the three available in Elixir by making a supervised app.

The code is available here with a branch that implements each strategy if you want to follow along without writing the code yourself.

To start, we make a supervised application:

Now that we have an app, we are going to create 3 modules. They will all be GenServers that are started with the application who send themselves a message every second to increment their state by one. One will always work, one will fail every 6 messages, and one will fail every 20 messages.

Note: No matter your supervisory strategy, if the children in your app do not succeed on start_link and return an {:ok, pid} tuple, the application as a whole will not start and your supervisory strategy does not matter at all.

To start, it will have the default supervisory strategy of one_for_one in application.ex. This strategy says that if one process dies, its siblings should stay working unaffected. We will stick with that in the beginning.

Let's start with the first module in lib/counter/one.ex. It will fail if its state is 22.

Note: This is a slight modification of a great example from the GenServer docs. Also see this past Elixir School blog post.

Now, if we open lib/counter/application.ex and add it to children, we can get it to start with our app:

This fails because we made a specific clause to coerce failure by raising an error when the state reached 22 in our counter. It is restarted with a state 0 (the default) after this failure.

Now let’s make another module that will never fail:

We can add it to lib/counter/application.ex as well.

Now, for our third and final module that will fail if state is 5.

We can add it to lib/counter/application.ex in the list of children, last after the other two:

One For One

Now, let's start our application and see the failure behaviour and state for each GenServer. These logs are truncated to just the interesting parts.

So, we can see our first crash. The process for Counter.One failed with our raised error, and was restarted. Because our default strategy in Elixir is one_for_one, this is expected. In the default configuration, we don’t want one child processes failure to effect any others.

If we let it continue to 22 with Counter.One, we would see the same behaviour (allow a crash without impacting any siblings, as it’s one for one).

Rest For One

Now let's try it with rest_for_one. Rest for one as a strategy starts the children in sequence, and if a later child fails, the ones before it do, too. We want to change our line assigning opts in lib/counter/application.ex to state that.

Now, let's start up again. These logs are also truncated to the interesting part:

The key takeaway here is order matters. Because Counter.Three doesn't fail until 22 is its state, and Counter.One fails with a state of 5, Counter.Three will force a restart of all 3 children since it’s last, but Counter.One's failures have no effect on its siblings.

One For All

Now let's enable it with one_for_all. In this supervisory model, if one child fails, all must be restarted. To do this, let’s change lib/counter/application.ex again.

opts = [strategy: :one_for_all, name: Counter.Supervisor]

If we start our app again with iex -S mix, we can see the behaviour as soon as Counter.Three reaches a state of 5, but it again will confirm that it works the same again when we reach 22.

We could also change the order in the children variable match, and the same thing would happen.

That was a lot to take in, but hopefully the supervisory strategies of Elixir applications are a bit clearer now!

Thanks for reading! Want to work on a mission-driven team that loves working in Elixir? We’re hiring!

Footer top

To learn more about Flatiron School, visit the website, follow us on Facebook and Twitter, and visit us at upcoming events near you.

Flatiron School is a proud member of the WeWork family. Check out our sister technology blogs WeWork Technology and Making Meetup.

Footer bottom

--

--