Architecture Deep-Dive
View SourceRaftEx is a pure-functional Raft implementation layered over OTP.
Layers
- Domain (
lib/domain/): pure logic — election, replication, state machine application. No processes. - Infra (
lib/infra/): persistence (WAL, segments, snapshots), networking, registry, observability. - App (
lib/app/): OTP processes —ServerRuntime(gen_statem), supervisors, system. - Facade (
lib/raft_ex.ex): public API.
gen_statem lifecycle
RaftEx.App.ServerRuntime is a :gen_statem with states :follower, :candidate, :leader. Transitions are driven by effects returned from the domain layer.
Effects system
Domain functions return {new_state, effects} where effects are tuples like
{:send_rpc, peer, rpc}, {:store_meta, key, value}, {:become, :leader}.
The runtime executes them. This keeps domain logic testable without OTP.
RPC routing
RPCs are sent via RaftEx.Infra.Network.Transport (in-BEAM or distributed).
Replies are routed back to the originating server process.
Data flow
- Client calls
RaftEx.process_command/3. - Runtime appends to Raft log (WAL).
- Leader replicates via AppendEntries.
- On quorum, commit index advances.
- Committed entries applied to state machine.
- Reply sent to client.