ELIXIR / CHAPTER 13

Supervision and recovery boundaries

Advanced

Design restart behavior around actual state and dependencies.

By the end of this chapter

  • Choose a restart strategy from dependency relationships
  • Distinguish transient and permanent restart policies
  • Separate process recovery from durable data recovery

The idea

A supervisor manages child processes according to restart policies and a strategy. One-for-one restarts the failed child, while other strategies can restart related children when their dependency relationship requires it. A permanent child is restarted after any exit; a transient child is restarted after abnormal exits but not ordinary successful termination. These choices express the system’s intended lifecycle.

Restarting a process does not recover every piece of its previous in-memory state or undo an external side effect. Durable state, idempotent operations, and initialization rules need separate design. A supervisor also has restart-intensity limits to avoid an endless rapid crash loop. Test the cause of failure and the recovery boundary rather than assuming that supervision makes every operation reliable automatically.

Elixir example
spec = Supervisor.child_spec({Agent, fn -> 0 end}, id: :counter, restart: :transient)
IO.inspect(spec.id)
IO.inspect(spec.restart)
Result
:counter
:transient

The child specification describes an identity and restart policy; it does not itself start or persist the Agent.

Try it yourself

Choose a supervision strategy for two independent workers and explain how each reconstructs its state.

Use your own editor for this exercise. Runnable challenges on CodeQuest are available for JavaScript, Python, and SQL.

CHECK YOUR UNDERSTANDING

Chapter checkpoint

Under a one-for-one strategy, which child is normally restarted when one child fails?