📎 Webclip
Building Distributed Systems in Elixir: Part 1 - Building a Stateful Process in Elixir Without GenServer
The article explains a minimal stateful server in Elixir by turning a counter into a process that owns its private state. Clients do not read that state directly; they send requests with send/2, wait for replies with receive, and rely on the process to update and return values.
Reading notes#
- A stateful server is described as a process with private state that receives messages, updates state, sends replies, and waits for the next message.
- The example uses only
spawn/1,send/2,receive, and recursion, withoutGenServer,Agent, or other OTP behaviours. - The counter is modeled as a process because BEAM processes do not share memory.
- The protocol uses
{:get, caller_pid},{:increment, caller_pid}, and:stop, and replies with{:counter_value, value}. start/1callsspawn/1and begins the recursiveloop/1with the initial value as private state.get/1sends a request and then waits for a new reply message; it does not read the message it sent.increment/1follows the same request-reply pattern, but the counter process performs the state update.- The receive loop matches incoming messages, handles each operation, and calls
loop/1again with the next state. - Because the process handles one mailbox message at a time, requests are serialized and clients do not modify the state concurrently.
GenServeris presented as a higher-level abstraction that adds request handling, correlation, timeouts, monitoring, supervision, debugging support, system messages, and consistent APIs.
Series#
Building Distributed Systems in Elixir, by krishnadaspc. Research: Distributed Systems Patterns.
