F5 · the engine · module 6
Where engine state lives
The fold from F5.05 is pure: it takes the event log and returns the current state, then forgets it.
That is fine for a calculation, but the Portal must keep the state alive between thousands of HTTP requests —
the next request needs to see the enrollment the last one recorded. On the BEAM, live in-memory state lives in a
process. F5.06 picks the process that owns the folded state — a GenServer,
an Agent, or ETS — builds it around decide and
evolve, and puts a supervisor around that so a crash restarts cleanly
and replays the log.
One process holds the state; one supervisor keeps it alive. Select a piece to see what it does and which dive builds it.
Three BEAM homes, one fit
[ GenServer · 3 / 3 ]
Holds state, runs logic per call, and restarts under a supervisor — the fit the engine needs.
A home for the fold
Reading the diagram: the event log is the source of truth from F5.05. A process folds it once on start and holds the result, answering commands and queries from that state. A supervisor sits above the process and restarts it if it dies. The three selectable pieces are the three questions this module answers — which process, how it is built, and what keeps it alive. Select a piece.
The running engine · select a piece
piece: Holder
in the Portal: a GenServer owns the state
A pure fold computes the state and lets it go; a process is what holds onto it. Choosing that process and the boundary around it is the whole of this module.
Three deep dives
In order: which kind of process should hold the state and why; building that process as a GenServer around
decide and evolve; and the supervisor that restarts it and
lets the log rebuild the state.
Agent holds a value, ETS is a shared table — the engine needs the first.init folds the log into state; a command call runs decide then evolve; a query call reads and replies.init replays the log so the state returns.F5.05 · state as a fold
The reducer computes the current state from the event log — pure, deterministic, and gone the moment it returns.
F5.06 · a home for it
A GenServer folds the log once and holds the result; a supervisor keeps that process alive across restarts.
Start with choosing where state lives, then the engine GenServer, then supervision. The next module, F5.07 — Pragmatic testing, tests the pure core and the engine boundary. For the runtime path this module sits on, see the design brief: the command & event flow.
References
Holding state in a process, and the choices for where it lives.
Sources
- Elixir — GenServer — stateful server processes.
- Elixir — Agent — simple shared state.
- Erlang — ETS — in-memory term storage.
Related in this course
- F5.06.1 · Choosing where state lives
- F5.06.2 · The engine GenServer
- F5.06.3 · Supervision
- F5.05 · Commands, queries & events — the fold this state comes from.
- F5.0.3 · The command & event flow — the runtime path this sits on.