liveBook · Manning

Restart strategy

As explained in Elixir in Action, Third Edition

A restart strategy specifies which supervised child processes a supervisor terminates and restarts after a child failure.

A restart strategy tells a supervisor how to respond when a supervised child terminates. The default example, :one_for_one, starts a replacement while leaving sibling processes alone. section 8.3.2

A process is not literally restarted: the supervisor terminates the failed process and starts another process from the same module. The replacement receives a new PID and does not share the old process state. section 8.3.2

Choose the strategy according to how children depend on one another:

  • :one_for_one replaces only the terminated child. section 9.1.6
  • :one_for_all terminates the other children, then starts all children again when one child crashes. section 9.1.6
  • :rest_for_one terminates younger siblings of the failed child, then replaces the terminated children. section 9.1.6

Use :one_for_all when siblings depend on one another in every direction. Use :rest_for_one when younger children depend on older ones, such as database workers depending on a registry. section 9.1.6

Configuring a supervisor

Supervisor.start_link([Todo.Cache], strategy: :one_for_one) starts the cache under supervision. Killing the cache causes the supervisor to start a replacement, allowing the system to continue serving requests. section 8.3.2

How Elixir in Action, Third Edition covers it

How deep it goes
Explains three sibling-restart policies, their dependency trade-offs, and replacement-process semantics.
Examples
Demonstrates Todo.Cache under :one_for_one and relates :rest_for_one to database workers and a registry.
What it assumes
Assumes familiarity with supervised child processes and introduces strategy choice through supervisor startup and failure behavior.
What you can do after
Configure a supervisor with a restart policy matched to the coupling among its child processes.
How it connects
Connects supervisors, supervision trees, child specifications, processes, and fault tolerance.

Also explained in

Compare how 2 books explain restart strategy →

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.