Erlang - Elixir: What is a supervision tree?
Asked Answered
R

2

7

I'm doing a tutorial and it says "In this guide, we will learn how to build a complete Elixir application, with its own supervision tree, configuration, tests and more".

In plain english, what is a supervision tree in Elixir?

Thanks!

Rundlet answered 3/10, 2017 at 22:37 Comment(2)
You might find this helpful: The most basic Erlang service ⇒ worker patternDuffy
Two other solid references: Erlang: Supervision Principles and LYSE: Who Supervises the Supervisors?Duffy
A
19

In Erlang and Elixir applications, structure is imposed by having a top-level "supervisor" processes which starts the other processes in your application. Those other processes can include other supervisors, which also have their own children, and this recursive structure takes the shape of a tree, hence "supervision tree". Supervision of processes is what gives Erlang/Elixir it's fault tolerance features, as failure is isolated to some branch of the tree, where the supervisor of that branch can either restart the failed children, or allow itself to fail as well, bubbling the failure up to the next highest supervisor in the tree.

Alarise answered 3/10, 2017 at 22:59 Comment(1)
Pretty good explanation about self healing supervision tree kodius.com/blog/elixir-supervision-treeMiosis
X
3

Since you have tagged Erlang, I will answer this Erlang's perspective. But again Erlang, Elixir its pretty much the same thing.

Erlang is a concurrency oriented programming language. Meaning its built ground up to deal with concurrency. One main feature especially needed in telecommunication/protocol related applications. Almost every programming language has a mechanism to deal with concurrent operations.

So for example,

  • Java uses multiple threads (multi-threading) to deal with concurrency
  • Go uses channels to deal with concurrency
  • Similarly, Erlang uses actor model to deal with concurrency.

Actor model in plain English

In actor model we have 2 types of processes.

  1. Supervisors
  2. Workers

Basically supervisors are there to supervise worker processes. That's it. If we want to create a worker process to carry out some work we ask supervisor create it for us. In case of the worker dies, the supervisor will restart it for you. You dont need to worry about the well being of the worker process. Furthermore supervisors can other supervisors also.

Worker processes are there to perform some tasks. That's it. It will perform whatever the tasks it is assigned and just die. In case it dies/crash due to a a not normal reason, its supervisor will restart the worker process to carry out its intended task.

So overall we can hav a root supervisor(call it S0) manage 2 supervisors S1, S2. S2 manages another supervisor S3. S3 supervises 5 workers processes namely W1-W5. This whole hierarchy is the supervision tree in Erlang/Elixir.

Here is an example supervision tree in a RADIUS protocol related project. https://github.com/sigscale/radierl/blob/master/doc/supervision.png

There is bunch of more stuff related to actor model like message passing etc. This will be good place to get an idea about actor model.

X answered 17/10, 2017 at 5:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.