3. Overview
Examples
Erlang Supervision
What is Supervision?
Watches child processes
Starts processes (or links to them) and is notified on their exit
Tristan Sloughter (kungfooguru@gmail.com) Erlang/OTP Supervision
4. Overview
Examples
Erlang Supervision
Why Use Supervision?
On failure a process can immediately be returned to a base
state
Keeps the system running even on failures
Allows you to not rely on defensive coding to keep a system
running for a long time
Tristan Sloughter (kungfooguru@gmail.com) Erlang/OTP Supervision
5. Overview
Examples
Erlang Supervision
Real World Examples
Chat Server : On failure restart with empty list of connected
users (the base state)
Streaming video : Video streaming in could be corrupted, if
this causes a process to crash it can be quickly restarted and
continue accepting data
Tristan Sloughter (kungfooguru@gmail.com) Erlang/OTP Supervision
6. Overview
Examples
Erlang Supervision
Most Basic Example
Tristan Sloughter (kungfooguru@gmail.com) Erlang/OTP Supervision
7. Overview
Examples
Erlang Supervision
A Supervision Tree
Tristan Sloughter (kungfooguru@gmail.com) Erlang/OTP Supervision
8. Overview
Examples
Erlang Supervision
Classic Erlang Supervision
Link processes
A terminating process sends an exit signal to all linked
processes
Trap exits
Sets the process to not exit on receiving exit signal
Process instead receives message of the form: {’EXIT’, From,
Reason}
Tristan Sloughter (kungfooguru@gmail.com) Erlang/OTP Supervision
12. Overview
Examples
Erlang Supervision
Restart Strategies
...
RestartStrategy = one_for_one,
MaxRestarts = 1000,
MaxSecondsBetweenRestarts = 3600,
...
A supervisor has a restart strategy to follow when a child dies,
which could effect other children
The RestartStrategy variable in our example could be:
one for one : Only the one process that dies is restarted
one for all : All processes are started and restarted in order if
one dies
rest for one : Processes started after child that dies are
stopped and then all restarted in order
Tristan Sloughter (kungfooguru@gmail.com) Erlang/OTP Supervision
13. Overview
Examples
Erlang Supervision
Children Restart Strategies
...
AChild = {Name, {Module, Function, Args},
Restart, Shutdown, Type, Module},
...
Each child process (worker) can have 1 of 3 strategies for the
supervisor to follow when it exists
The Restart variable in our example could be one of these:
permanent : On ANY exit, restart
transient : On abnormal exit, restart
temporary : Never restart
Tristan Sloughter (kungfooguru@gmail.com) Erlang/OTP Supervision
14. Overview
Examples
Erlang Supervision
One For All
Tristan Sloughter (kungfooguru@gmail.com) Erlang/OTP Supervision
15. Overview
Examples
Erlang Supervision
Simple One For One
Dynamic supervision
Essentially a process factory
Tristan Sloughter (kungfooguru@gmail.com) Erlang/OTP Supervision
16. Overview
Examples
Erlang Supervision
Scenarios
Web Server : one for one with temporary children
Media Server Encoder : one for one with permanent children
Poker Server : one for all with transient children
Facebook Session : simple one for one
Tristan Sloughter (kungfooguru@gmail.com) Erlang/OTP Supervision