liveBook · Manning

Supervisor

As explained in Elixir in Action, Third Edition

A supervisor process is a concurrent process that monitors and manages worker processes, restarting or otherwise handling them when they fail.

A supervisor is a concurrent process manager that starts worker processes, monitors their lifecycles, and restores service when they terminate. This turns individual crashes into recoverable events instead of permanent loss of a system component. section 8.3 section 9.0

A supervisor coordinates its children through a small set of operational rules:

  • Starts each child from a child specification, which describes how that process should be started and managed. section 8.3.2
  • Uses links, exit trapping, and received exit messages to detect child termination. section 8.3 section 8.3.1
  • Applies a restart strategy such as :one_for_one, replacing a failed child without sharing its old state or PID. section 8.3.2
  • Takes down its children when it terminates, preventing dangling processes and supporting orderly shutdown. section 8.3.1

Wrapping a to-do system supervisor

defmodule Todo.System do def start_link do Supervisor.start_link( [Todo.Cache], strategy: :one_for_one ) end end Todo.System.start_link() starts the cache and its database processes. If the cache is killed, the supervisor starts a replacement process, which receives a new PID and begins with fresh state. section 8.3.4 section 8.3.2

Supervisors become more useful when arranged as a supervision tree. A parent can manage separate supervisors for the cache, database, and registry, limiting a failure to the smallest meaningful service. If a database worker repeatedly fails, its supervisor can terminate and let the parent restart the database service without disturbing unrelated processes. section 9.1.5 section 9.1.6

Figure 8.3 Processes in the to-do system
CH08_F03_Juric3

The process diagram shows how workers and supervisors organize the to-do system into managed services. section 8.3

This architecture supports “let it crash”: worker code can focus on its intended behavior while supervisors handle unexpected failures and clean-slate recovery. Explicit error handling remains appropriate when a critical process must not crash or an error has a meaningful local response. section 9.3 section 9.4

How Elixir in Action, Third Edition covers it

How deep it goes
Explains child specifications, links, restart strategies, supervision trees, and failure boundaries, while deferring finer strategies to later treatment.
Examples
Uses a to-do cache, database workers, a wrapped Todo.System supervisor, killed processes, and a hierarchical database supervision tree.
What it assumes
Assumes familiarity with Elixir processes and GenServer while teaching links, exit trapping, child specifications, and supervision hierarchies.
What you can do after
Build it from scratch by defining child specifications, selecting a restart strategy, and wrapping Supervisor.start_link/2 in a system module.
How it connects
Connects supervisors with workers, processes, links, monitors, supervision trees, OTP, registries, and application startup.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elixir in Action, Third Edition. The section links point to the parts of the book it draws on.