Elixir Supervision

From Elixir Wiki
Jump to navigation Jump to search

Elixir Supervision[edit]

File:Elixir logo.png
The Elixir programming language logo

Elixir Supervision is a key feature of the Elixir programming language that allows developers to build fault-tolerant and robust systems. It provides a mechanism for creating and managing processes, ensuring the stability and reliability of an application.

Introduction[edit]

Supervision is a fundamental concept in the Erlang virtual machine, upon which Elixir is built. It enables processes to be organized hierarchically, with a designated supervisor process responsible for managing the lifecycle and behavior of its child processes. These child processes, in turn, may have their own child processes, forming a tree-like structure.

The primary purpose of supervision is to isolate failures and ensure that they are contained within a specified portion of the system. When a process under supervision terminates, the supervisor can respond accordingly, which may involve restarting the process, terminating other related processes, or even restarting the entire application.

Supervision Strategies[edit]

Elixir supervision uses different strategies to manage the lifecycles of child processes. The two main strategies are:

  • Simple One for One: This strategy allows for dynamically spawning a new process for each individual task. Here, each child is considered independent from the others.
  • One for All: This strategy treats all child processes as a group. If any child terminates, all other children are terminated as well, and subsequently restarted in a prescribed order.

The Supervisor Module[edit]

The core of supervision in Elixir resides in the `Supervisor` module. This module provides functions for creating and managing supervisors, adding and removing child processes, and defining restart strategies.

Some key functions of the `Supervisor` module include:

  • `start_link/2`: Initiates a supervisor process and its supervision tree.
  • `child_spec/1`: Defines the specification for a child process.
  • `add_child/2`: Adds a child process to the supervisor's supervision tree.
  • `delete_child/2`: Removes a child process from the supervisor's supervision tree.
  • `restart_child/2`: Restarts a specific child process.
  • `restart/2`: Restarts all child processes of a supervisor.

Restart Strategies[edit]

The restart strategies define how supervisors react to child process failures. They can be defined for individual child processes or for the supervisor itself.

The available restart strategies are:

  • :one_for_one: Restarts only the failed child process.
  • :one_for_all: Restarts all child processes.
  • :rest_for_one: Restarts the failed child process and all subsequent children.
  • :simple_one_for_one: Restarts only the failed child process, which is dynamically added.

Using Supervision[edit]

To use Elixir supervision, developers typically define a supervision tree in their application's top-level module. This tree is composed of supervisor and worker processes that collectively manage the application's processes.

Supervisors are defined as children of other supervisors or the application itself using the `Supervisor.child_spec/1` function. Workers, on the other hand, represent the actual processes performing the application's tasks.

By defining the restart strategy and specifying child processes, the supervisor takes care of starting, monitoring, and restarting the system's components to maintain its stability and integrity.

Conclusion[edit]

Elixir Supervision provides a powerful and flexible mechanism for building robust and fault-tolerant applications. By organizing processes hierarchically and defining restart strategies, developers can ensure that failures are isolated, contained, and appropriately handled.

Supervision plays a crucial role in the reliability and maintainability of Elixir applications and is an essential concept for any developer working with the language.

See Also[edit]