Restart strategies in practice

The supervisors lesson of Elixir used :one_for_one without saying what the other strategies do. The strategy decides which children a failure takes with it, and a program that picks the wrong one either loses more state than it must or keeps a sibling that can no longer work.

one_for_one

The default, and the right choice for children that do not depend on each other. Only the child that died is restarted, and the others keep their state:

one.exs

defmodule Worker do use Agent def start_link({name, n}), do: Agent.start_link(fn -> n end, name: name) def bump(name), do: Agent.update(name, &(&1 + 1)) def value(name), do: Agent.get(name, & &1) def child_spec({name, n, restart}) do %{id: name, start: {__MODULE__, :start_link, [{name, n}]}, restart: restart} end end {:ok, sup} = Supervisor.start_link( [{Worker, {:a, 0, :permanent}}, {Worker, {:b, 0, :permanent}}], strategy: :one_for_one ) Worker.bump(:a) Worker.bump(:b) IO.inspect({Worker.value(:a), Worker.value(:b)}) [{_, pid_a, _, _}] = Enum.filter(Supervisor.which_children(sup), fn {id, _, _, _} -> id == :a end) Process.exit(pid_a, :kill) Process.sleep(50) IO.inspect({Worker.value(:a), Worker.value(:b)})

elixir one.exs

{1, 1}
{0, 1}

The failed worker started again at 0 and its sibling stayed at 1. The two children share nothing, so killing one gives no reason to reset the other; this is why :one_for_one is the common choice.

one_for_all

When the children form one unit, a failure of one invalidates the state of all. The :one_for_all strategy restarts every child of the supervisor:

all.exs

defmodule Worker do use Agent def start_link({name, n}), do: Agent.start_link(fn -> n end, name: name) def bump(name), do: Agent.update(name, &(&1 + 1)) def value(name), do: Agent.get(name, & &1) def child_spec({name, n, restart}) do %{id: name, start: {__MODULE__, :start_link, [{name, n}]}, restart: restart} end end {:ok, sup} = Supervisor.start_link( [{Worker, {:a, 0, :permanent}}, {Worker, {:b, 0, :permanent}}], strategy: :one_for_all ) Worker.bump(:a) Worker.bump(:b) IO.inspect({Worker.value(:a), Worker.value(:b)}) [{_, pid_a, _, _}] = Enum.filter(Supervisor.which_children(sup), fn {id, _, _, _} -> id == :a end) Process.exit(pid_a, :kill) Process.sleep(50) IO.inspect({Worker.value(:a), Worker.value(:b)})

elixir all.exs

{1, 1}
{0, 0}

Both counters restarted, the sibling included. A program pays this reset on every crash, so the strategy belongs to children that share a cache or a connection whose loss breaks all of them.

rest_for_one

A chain of dependencies is the third case: a failed child takes the children started after it and leaves the earlier ones alone. :rest_for_one restarts the failed child and every child that follows it in the list:

rest.exs

defmodule Worker do use Agent def start_link({name, n}), do: Agent.start_link(fn -> n end, name: name) def bump(name), do: Agent.update(name, &(&1 + 1)) def value(name), do: Agent.get(name, & &1) def child_spec({name, n, restart}) do %{id: name, start: {__MODULE__, :start_link, [{name, n}]}, restart: restart} end end {:ok, sup} = Supervisor.start_link( [{Worker, {:a, 0, :permanent}}, {Worker, {:b, 0, :permanent}}, {Worker, {:c, 0, :permanent}}], strategy: :rest_for_one ) Worker.bump(:a) Worker.bump(:b) Worker.bump(:c) IO.inspect({Worker.value(:a), Worker.value(:b), Worker.value(:c)}) [{_, pid_b, _, _}] = Enum.filter(Supervisor.which_children(sup), fn {id, _, _, _} -> id == :b end) Process.exit(pid_b, :kill) Process.sleep(50) IO.inspect({Worker.value(:a), Worker.value(:b), Worker.value(:c)})

elixir rest.exs

{1, 1, 1}
{1, 0, 0}

Child a kept its value and b and c restarted. The order of the list is the dependency order: a program lists the thing depended on first, and the strategy restarts only what comes after the failure.

permanent, transient and temporary

The restart policy of one child decides what its own end means, whatever the strategy of the supervisor:

A temporary child that is killed is not started again, and the supervisor removes its specification:

temporary.exs

defmodule Worker do use Agent def start_link({name, n}), do: Agent.start_link(fn -> n end, name: name) def child_spec({name, n, restart}) do %{id: name, start: {__MODULE__, :start_link, [{name, n}]}, restart: restart} end end {:ok, sup} = Supervisor.start_link([{Worker, {:t, 0, :temporary}}], strategy: :one_for_one) [{_, pid, _, _}] = Supervisor.which_children(sup) Process.exit(pid, :kill) Process.sleep(50) IO.inspect(Supervisor.count_children(sup))

elixir temporary.exs

%{active: 0, workers: 0, supervisors: 0, specs: 0}

Both the active child and its specification are gone. A transient child would have been restarted by the same kill, because the kill is abnormal; only a normal stop would have been final.

The mistake: a temporary child that is expected back

The policies are easy to mix, and the symptom is quiet: the supervisor does not report an error, it simply does not start the child again. A job that must be retried needs :permanent or :transient; a job whose result is already delivered is the one place for :temporary.

The strategy and the policy answer different questions: the strategy says which children a failure takes with it, the policy says whether the failed child itself comes back. A reader who sets one and expects the other sees a tree that looks healthy and misses a worker, which is why Supervisor.count_children/1 is the first check of a supervised program.

The programs on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which strategy restarts only the child that failed?

    Answer

    :one_for_one

    One-for-one restarts the failed child and leaves the siblings alone.

  2. Which strategy restarts the failed child and every child started after it?

    Answer

    :rest_for_one

    Rest-for-one follows the order of the child list.

  3. Which strategy restarts every child of the supervisor?

    Answer

    :one_for_all

    One-for-all treats the children as one unit and restarts all of them.

  4. Which restart policy never starts a child again?

    Answer

    :temporary

    A temporary child is removed when it ends, whatever the reason.

  5. What does this program print? defmodule W do use Agent def start_link({name, n}), do: Agent.start_link(fn -> n end, name: name) def bump(name), do: Agent.update(name, &(&1 + 1)) def value(name), do: Agent.get(name, & &1) def child_spec({name, n, restart}) do %{id: name, start: {__MODULE__, :start_link, [{name, n}]}, restart: restart} end end {:ok, sup} = Supervisor.start_link( [{W, {:a, 0, :permanent}}, {W, {:b, 0, :permanent}}], strategy: :one_for_one ) W.bump(:a) W.bump(:b) [{_, pid, _, _}] = Enum.filter(Supervisor.which_children(sup), fn {id, _, _, _} -> id == :a end) Process.exit(pid, :kill) Process.sleep(50) IO.inspect({W.value(:a), W.value(:b)})

    Answer

    {0, 1}

    One-for-one restarts a at 0 and leaves b at 1.

  6. What does this program print? defmodule W do use Agent def start_link({name, n}), do: Agent.start_link(fn -> n end, name: name) def bump(name), do: Agent.update(name, &(&1 + 1)) def value(name), do: Agent.get(name, & &1) def child_spec({name, n, restart}) do %{id: name, start: {__MODULE__, :start_link, [{name, n}]}, restart: restart} end end {:ok, sup} = Supervisor.start_link( [{W, {:a, 0, :permanent}}, {W, {:b, 0, :permanent}}], strategy: :one_for_all ) W.bump(:a) W.bump(:b) [{_, pid, _, _}] = Enum.filter(Supervisor.which_children(sup), fn {id, _, _, _} -> id == :a end) Process.exit(pid, :kill) Process.sleep(50) IO.inspect({W.value(:a), W.value(:b)})

    Answer

    {0, 0}

    One-for-all restarts both children, so both counters start again at 0.

  7. What does this program print? defmodule W do use Agent def start_link({name, n}), do: Agent.start_link(fn -> n end, name: name) def child_spec({name, n, restart}) do %{id: name, start: {__MODULE__, :start_link, [{name, n}]}, restart: restart} end end {:ok, sup} = Supervisor.start_link([{W, {:t, 0, :temporary}}], strategy: :one_for_one) [{_, pid, _, _}] = Supervisor.which_children(sup) Process.exit(pid, :kill) Process.sleep(50) IO.inspect(Supervisor.count_children(sup))

    Answer

    %{active: 0, workers: 0, supervisors: 0, specs: 0}

    A temporary child is not restarted and its spec is removed.

  8. A transient child stops with :normal. What does the supervisor do?

    Answer

    It does not restart it

    A transient child restarts only on an abnormal end; a normal stop is final.

  9. A child has restart: :permanent and stops with :normal. What does the supervisor do?

    Answer

    It restarts the child

    A permanent child comes back after every end, a normal one included.

  10. Why is the order of the child list important for rest_for_one?

    Answer

    The strategy restarts the children after the failed one

    The strategy depends on the dependency order written in the list.

Article author: Arthur Isaev