Supervision in Elixir

From Elixir Wiki
Jump to navigation Jump to search

Supervision in Elixir[edit]

File:Supervision tree.png
A sample supervision tree in Elixir

Supervision is a fundamental concept in Elixir, allowing developers to build fault-tolerant and resilient applications. With the supervision principle, processes are organized in a tree-like structure, where each process is supervised by a higher-level supervisor. This approach ensures that when a process fails, it is automatically restarted by its supervisor, maintaining the overall stability and availability of the system.

Introduction[edit]

Supervision is at the core of Elixir's approach to concurrency and fault tolerance. It is inspired by the "let it crash" philosophy of the Erlang programming language, which embraces the idea that failures are an inherent part of software development. Rather than trying to prevent failures, Elixir embraces them as opportunities for recovery and self-healing.

Supervision is based on the idea that processes should be organized into structured supervision trees. Each process has a direct supervisor, responsible for monitoring its execution and taking appropriate actions in case of failure. Supervisors can be defined hierarchically, allowing for powerful fault tolerance strategies.

Supervision Trees[edit]

A supervision tree is a hierarchical structure composed of supervisors and processes. The tree starts with a top-level supervisor, which is usually the entry point for the entire application. This supervisor is responsible for starting and managing child processes, as well as handling their failures.

Supervision trees are defined using the `Supervisor` module, which provides a set of functions to create and manage supervisors. These functions include `start_link`, `child_spec`, and `tree`. They allow developers to define the structure of the supervision tree and specify the restart strategy for each child process.

Restart Strategies[edit]

Elixir provides several restart strategies to control how supervisors handle the failures of their child processes. These restart strategies are applied based on the severity of the failure and the desired behavior of the system. The available restart strategies are:

  • `:one_for_one`: This strategy restarts only the failed process and its children, leaving other processes unaffected.
  • `:one_for_all`: This strategy restarts all the child processes of the supervisor, regardless of which process failed.
  • `:rest_for_one`: This strategy restarts the failed process and all the processes starting from it to the right in the supervisor's child list.

Restart strategies can be specified when defining child specifications using the `child_spec` function of the `Supervisor` module.

Dynamic Supervision[edit]

In addition to static supervision trees, Elixir also supports dynamic supervision. Dynamic supervision allows processes to be added or removed from the supervision tree at runtime. This feature is particularly useful in cases where new processes are created or removed dynamically, such as in response to user requests or changing system conditions.

Dynamic supervision is implemented using the `DynamicSupervisor` module, which provides functions like `start_child`, `stop_child`, and `await_termination`. These functions enable developers to add, remove, and monitor child processes dynamically.

Conclusion[edit]

Supervision is a powerful mechanism in Elixir for building fault-tolerant and resilient applications. By organizing processes into structured supervision trees and defining appropriate restart strategies, developers can ensure the availability and stability of their systems. Elixir's supervision mechanisms, combined with its other concurrency features, make it a robust choice for building concurrent, scalable, and fault-tolerant applications.

See Also[edit]