Elixir Supervisor Trees

What are they and how does the Ramsey Bus Tracker use them?

First:

Processes

Elixir Processes

  • Lightweight units of concurrency
  • Communicate via message passing
  • NOT OS processes or threads, but like OS processes, they have a PID
  • All code in an Elixir application is run in a process

GenServer

  • "Generic Server"
  • A wrapper around an Elixir process
  • Provides a standard set of interface functions
  • Commonly used to hold state

"MainWorker"

GenServer Process

%{lat: 1.0005, long: 2.9997}

%{running: true}

Ramsey Bus

But what if...

MainWorker crashes???

Supervisor

  • A type of process that watches one or more other processes and restarts them if they die

MainSupervisor

MainWorker

MainSupervisor watches MainWorker and respawns it if it dies

That's cool, but where does MainWorkers' state go after it dies?

/dev/null

MainSupervisor

MainData

(GenServer)

Text

MainSubSupervisor

MainWorker

(GenServer)

2 functions:

get_state()

save_state()

Ramsey Bus

save_state()

get_state()

Why is this cool?

  • Fault tolerant storage for ephemeral data
  • "Let it crash"
  • Quick recovery and no loss of data = 

Resources

  • http://elixir-lang.org/docs.html
  • http://learnyousomeerlang.com/content
  • https://hexdocs.pm/phoenix/Phoenix.html
Made with Slides.com