📎 Webclip
A Brief Guide to OTP in Elixir
The article explains OTP as the set of Erlang tools and libraries that Elixir inherits, and focuses on the actor model built on lightweight processes. It shows how processes receive messages, keep private state, and can be used to model a simple treasury.
Reading notes#
- OTP is presented as the Erlang toolkit Elixir inherits, especially the actor model based on lightweight processes.
- A process can receive messages, create new processes, send messages, and modify private state.
- A basic process can be spawned with
spawn, but it ends after completing its work. - A
receive doloop can keep a process alive and maintain state while handlingstore,withdraw, andbalancemessages. - The example treasury stores balance in the loop argument and replies with the current balance when asked.
- The article says production code would need more than raw
receive doloops, such as corner-case handling, tracing, and reporting. - Behaviours are described as abstract code plus a callback module that supplies the implementation.
GenServeris introduced as a common behaviour for server-like processes.- The article lists
init,handle_cast, andhandle_callas the basic callbacks used in the example. init(balance)starts the server with a state value.handle_castis used for asynchronous updates like storing and withdrawing funds.handle_callis used for synchronous balance queries and returns both a reply and the unchanged state.- Client functions in the same module wrap the server API with
GenServer.start_link,GenServer.cast, andGenServer.call. - The treasury can be named with its module name when started, which makes later calls simpler.
- A supervisor is introduced as a way to start and shut down applications and restart crashed processes.
- The example supervisor starts the treasury as a child and uses the
:one_for_onestrategy so only the crashed child is restarted. - The article notes that a supervision tree can include several processes and that state can also be duplicated or persisted to avoid loss on crash.
