💧 Elixir Beginner

What is a Supervisor in Elixir?

Answer

A Supervisor is an OTP process that monitors child processes and restarts them according to a restart strategy when they crash. This embodies the "let it crash" philosophy — instead of defensive error handling, let processes fail and rely on supervisors to recover them. Define a Supervisor: defmodule MyApp.Supervisor do; use Supervisor; def start_link(_), do: Supervisor.start_link(__MODULE__, []); def init([]), do: Supervisor.init([{MyWorker, []}, {MyCache, []}], strategy: :one_for_one) end. Restart strategies: :one_for_one: restart only the crashed child. :one_for_all: restart all children when one crashes. :rest_for_one: restart the crashed child and all children started after it. Restart values: :permanent (always restart), :temporary (never restart), :transient (restart only on abnormal exit). Supervisors form a tree — an entire application is a hierarchy of supervisors and workers.