F3.08 · part 3 of 3
Supervisors & restart strategies
Isolation (F3.07) means a crash stays local; a supervisor turns that into recovery. A Supervisor
starts a set of child processes and, when one crashes, restarts it by a strategy.
:one_for_one restarts only the failed child; :one_for_all
restarts them all; :rest_for_one restarts the failed child and those started after it.
This is "let it crash": rather than defend every line, let a process die on a bad state and be restarted clean. The portal supervises a tally, a notifier, and a cache. The notifier crashes — choose a strategy and see who else restarts.
When a child crashes
The children start in order: tally, then notifier, then cache. The notifier crashes. The strategy decides which of its siblings restart with it — none, all, or only those started afterward. Select a strategy and read each child's fate.
The restart strategy · select one
The crashed child always comes back; the strategy is only about its siblings. Pick the one that matches how the children depend on each other — independent, all-or-nothing, or ordered.
Building the tree
A supervisor is itself a process, started with a list of children and a strategy. The children are usually other GenServers or supervisors, so the tree nests. When a child exceeds its restart limit, the supervisor itself crashes and its own parent decides what to do.
defmodule Portal.Supervisor do use Supervisor def start_link(_), do: Supervisor.start_link(__MODULE__, :ok) @impl true def init(:ok) do children = [ Portal.Tally, Portal.Notifier, Portal.Cache ] Supervisor.init(children, strategy: :one_for_one) end end
The idea
Do not prevent every failure — isolate it, then recover by starting fresh.
In Elixir
A supervisor restarts crashed children by strategy; each process stays simple, and the tree stays alive.
That completes OTP: a GenServer holds state, call and cast reach it, and a supervisor keeps it alive. Next module: F3.09 — The process playground, an interactive lab where you spawn processes, send messages, and watch the supervision tree restart them live.
References
Primary sources for this lesson, and where it connects in the course.
Sources
Supervisor— Elixir documentation — strategies, child specs, and restarts.GenServer— Elixir documentation — the stateful server a supervisor restarts.- Mix & OTP: GenServer — Elixir documentation — building one step by step.