Elixir Distributed System Resources (notes)

My notes on Elixir's resources for distributed systems... These notes are not exhaustive, and are not necessarily accurate - they represent my understanding and opinions of said resources.

These notes will probably be most helpful for anyone getting started with implementing distributed systems using Elixir. If you have questions or opinions as well as fixes I should include, please leave a comment below!

Supervision

  • Hex docs: Task

    • Good for running some code that doesn't need supervision

    • Tasks generally don't interact with other processes, and we don't care about the return value

  • Hex docs: Task.Supervisor

    • Dynamically supervise Tasks

    • Good for keeping a handle on running a Task without orphaning it (<- my opinion)

  • Hex docs: Supervisor

    • Good for supervision of known processes (created at server start)

    • Children are defined in the code and started when Supervisor.start_link is called

    • Not conventional for supervising processes created at runtime (deprecated in favor of Dynamic Supervisor)

  • Hex docs: Dynamic Supervisor

    • Good for supervising processes started at runtime

    • Starts with no children, and spawns new children with DynamicSupervisor.start_child

    • Note that children should have unique identifiers (:name) - see Process Registration section

Process Registration

  • Name Registration (via GenServer)

    • Used by Supervisor, Dynamic Supervisor, GenServer

    • __MODULE__ uses this module's own name

      • Can only have one instance registered when using __MODULE__
    • :(atom) registers via Process.register with the atom as the name

      • Again, you can only have one instance registered since the name is not unique
    • {:global, term} registers via "the functions in the :global module", where term can be basically anything

      • Register any number of processes!

      • Find processes by name using GenServer.whereis

      • Must define the names such that they don't collide

    • {:via, module, term} use a custom module to define a registry. Explained well in Brian Storti's Blog on Process Registry

  • Hex docs: Registry

    • Registry module
  • Custom Process Registry (blog; Brian Storti)

    • Custom local implementation using a Map in the registry's state
  • gproc

    • Noted in Brian Storti blog; uses "central server and an ordered-set ets table"
  • Elixir forum: Agent vs Registry

Distributed Supervision/Registration

Horde (medium@derek.kraan2)