jonnifyelixir
F5 · Pragmatic Programming / F5.06 · state

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 state + logic ▸ AGENT a held value ▸ ETS a shared table ▸ SCORING THE HOME UNDER TEST ✓holds state across requests ✓runs decide · evolve per call ✓restarts under a supervisor three of three — the fit the engine needs

[ 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

SUPERVISOR restarts on crash supervises EVENT LOG F5.05 · the source fold ENGINE · GenServer holds the folded state init · handle_call decide · evolve · reply

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.

F5.06.1
Choosing where state lives
A GenServer holds state and runs logic, an Agent holds a value, ETS is a shared table — the engine needs the first.
F5.06.2
The engine GenServer
init folds the log into state; a command call runs decide then evolve; a query call reads and replies.
F5.06.3
Supervision
Let it crash: a supervisor restarts the engine, and 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

Related in this course