Lesson 2 of 9 · 28m
Supervision Trees and the Real Meaning of Let It Crash
Design restart boundaries, child specs, backoff behavior, and supervision strategies so failures reset to known state instead of cascading.
"Let it crash" is one of the most abused phrases in our industry. It does not mean ignore errors and hope the VM saves you. It means errors cross a boundary you deliberately designed, where a supervisor restarts a clean process with known state. The craft is choosing that boundary.
Supervisors shape recovery
A supervisor starts children, watches them, and restarts them according to a strategy:
children = [
{Registry, keys: :unique, name: ChatRoom.Registry},
{DynamicSupervisor, strategy: :one_for_one, name: ChatRoom.RoomSupervisor},
{ChatRoom.Cleanup, interval: :timer.minutes(5)}
]
Supervisor.init(children, strategy: :one_for_one)Ordering matters. Children start in order and shut down in reverse order. In this tree the registry exists before room processes because room lookup depends on it.
Pick the strategy from dependencies
one_for_one restarts only the child that died. one_for_all restarts every child because their states depend on one another. rest_for_one restarts the failed child and children started after it:
If everything needs one_for_all, boundaries may be wrong. If everything is one_for_one but children secretly depend on shared assumptions, restart can resurrect a half-consistent system.
Restart intensity prevents crash loops
Supervisors do not restart forever. max_restarts and max_seconds bound the retry loop:
Supervisor.init(children,
strategy: :one_for_one,
max_restarts: 3,
max_seconds: 10
)Design restart state before the happy path
Ask what a child loses when restarted. Socket state may be disposable because clients reconnect. Room state may need reconstruction from durable data. Rate-limit state may be security-sensitive because restarting it erases punishment.
def init(room_id) do
case Store.load_room(room_id) do
{:ok, room} -> {:ok, room}
:not_found -> {:ok, Room.new(room_id)}
end
endA good restart converges toward legitimate state. A bad one assumes the world looks exactly as it did before the crash.
Field notes
- Name important processes through
Registryor explicit names; anonymous discovery becomes painful in production. - Supervision trees are executable architecture diagrams. Draw them before incident response requires them.
- Restarting a child does not replay messages already consumed and lost.
- Avoid singleton bottlenecks merely because supervision is convenient.
- Test restart behavior by killing processes, not only by unit-testing callbacks.
Was this lesson helpful?