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
-
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
-
Dynamically supervise
Task
sGood for keeping a handle on running a
Task
without orphaning it (<- my opinion)
-
Good for supervision of known processes (created at server start)
Children are defined in the code and started when
Supervisor.start_link
is calledNot conventional for supervising processes created at runtime (deprecated in favor of
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__
- Can only have one instance registered when using
:(atom)
registers viaProcess.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", whereterm
can be basically anythingRegister 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
-
- Registry module
Custom Process Registry (blog; Brian Storti)
- Custom local implementation using a
Map
in the registry'sstate
- Custom local implementation using a
-
- Noted in Brian Storti blog; uses "central server and an ordered-set ets table"