Building Distributed Systems in Elixir: Part 5 — Supervisor From Scratch
In the previous part of this series, we explored process links. A linked worker that crashes sends an exit signal to the process linked to it. By default, that failure propagates and can terminate both processes. We also saw that a process can trap exits: Process . flag ( :trap_exit , true ) Once exit trapping is enabled, an incoming exit signal becomes a mailbox message: { :EXIT , pid , reason }…
In the latest installment of this series, we dive into crafting a supervisor from scratch in Elixir. A supervisor is a process that monitors the health of worker processes and takes action when a worker fails. Our supervisor will manage two workers, restarting only the failed worker while leaving the other ones running. To achieve this, we will utilize the spawn/1, spawn_link/1, Process.flag(:trap_exit, true), send/2, and receive/1 functions.
Our supervisor won't rely on GenServer or OTP supervisor, but rather, we'll utilize the BEAM's built-in features for failure detection and restart policy. We will create a manual supervisor process, two workers, and a restart loop that uses the link between the supervisor and workers, along with exit signals and exit trapping, to monitor worker health and implement a restart policy.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.