jonnify·persistence /echo-persistence/native-elixir/the-volume-server

Module 7 · Dive 7.1

The VolumeServer.

The write path is one GenServer. Many clients commit at once, but a GenServer has a single mailbox and runs one message at a time — so concurrent commits become a strict order for free. Each is checked with OCC (Module 3): if the base it read still matches the head, it appends; otherwise it’s a conflict and the client retries. No lock, because the mailbox already is one. Enqueue from two clients and process.

§1

Concurrent in, ordered out

interactive
One mailbox, one at a time. Two clients enqueue commits (each captures the head it read as its base). The VolumeServer processes the front message: base matches head → append; else conflict. The order is the mailbox order.
client A reads head client B reads head single mailbox = serialization point VolumeServer handle one, then next head = LSN 4 applied: 1 2 3 4 enqueue append
readout · Have A and B both commit (they each read head = LSN 4 as their base), then process. The first to be processed appends; the second finds the head moved and gets {:error,{:conflict, head}} — resolved by order, not by a lock.
§2

The mailbox is the lock

store.design.md

People reach for a mutex to protect a shic counter; on the BEAM you reach for a process. Because a GenServer dequeues and runs one message to completion before the next, there is never a moment where two commits inspect the head at once — the serialization is structural, not enforced. So the OCC check inside handle_call is a plain comparison: does this commit’s base still equal the head? If yes, append the page to CubDB and bump the head; if no, reply {:error,{:conflict, head}} and let the caller re-read and retry. Two clients that both based on LSN 4 cannot both win: whoever the mailbox put first takes LSN 5, the other conflicts. One writer process per volume, no locks, and the strict order Module 3 assumed — that is the whole write path. Next, the read path, which never blocks on this at all.

§3

References & sources