Supervisor (Erlang)

From Elixir Wiki
Jump to navigation Jump to search

Supervisor (Erlang)[edit]

File:Elixir supervisor.png
Supervisor in Elixir

A **Supervisor** in **Erlang** is a behavior that helps in building fault-tolerant systems by monitoring and controlling a set of child processes. It is one of the key components of **OTP (Open Telecom Platform)**, which is widely used in Elixir for building concurrent and reliable applications.

Functionality[edit]

A Supervisor manages a group of child processes, which are typically workers responsible for performing different tasks. The main functionality of a Supervisor can be summarized as follows:

  • **Starting Child Processes**: A Supervisor starts and initializes the specified child processes when it starts up.
  • **Monitoring Child Processes**: A Supervisor monitors the state of its child processes, detecting and responding to failures.
  • **Restarting Child Processes**: In case of a child process failure, a Supervisor is responsible for restarting the failed process.
  • **Managing Child Process Dependencies**: A Supervisor can define dependencies between child processes, ensuring proper startup and shutdown order.

Supervision Strategies[edit]

Supervisors support different strategies for handling child process failures. The most commonly used strategies are:

  • **One for One**: In this strategy, when a child process fails, only that specific child process is restarted.
  • **One for All**: If any child process fails, all the child processes supervised by the Supervisor are restarted.
  • **Rest for One**: If a child process fails, the Supervisor restarts that specific child process and all the processes that were started after it.

Supervision Trees[edit]

Supervisors can be organized in a hierarchical manner, forming a "supervision tree." This helps in creating a fault-tolerant system where failures can be isolated and handled in a structured way. A supervision tree typically consists of a top-level Supervisor, which supervises multiple intermediate Supervisors, which in turn supervise worker processes.

Usage in Elixir[edit]

In Elixir, the Supervisor behavior is implemented through the `Supervisor` module in the `:simple_one_for_one` and `:one_for_all` strategies. Elixir provides a clean and expressive syntax for defining Supervisors using the `use Supervisor` macro. Here's an example:

```elixir defmodule MySupervisor do

 use Supervisor
 
 def start_link do
   Supervisor.start_link(__MODULE__, :ok, name: __MODULE__)
 end
 
 def init(:ok) do
   children = [
     worker(MyWorker, []),
     supervisor(MyOtherSupervisor, [])
   ]
   
   supervise(children, strategy: :one_for_one)
 end

end ```

In this example, `MySupervisor` is defined as a Supervisor module, managing a child worker process `MyWorker` and another Supervisor `MyOtherSupervisor`.

Summary[edit]

Supervisors play a critical role in developing robust and fault-tolerant systems in Elixir. They provide the foundation for creating supervision trees and managing the lifecycle of child processes. Understanding how to use Supervisors effectively is essential for building reliable Elixir applications.

See Also[edit]