Design decisions

Replication and failover

Decisions about log shipping, synchronous replication, the Raft core, the first automatic failover and replication of lsm tables (D17, D38, D39, D42, D47).

The election part of D42 was later moved out of the server; see D51 in Control plane and partitions.

D17 Write our own Raft core in the raft-rs shape; no Raft dependency#

The Raft core is a pure state machine (tick, step, propose, ready, advance) that uses the WAL and runs under the simulator. Rejected: openraft (async runtime, large code base, about 22 direct dependencies) and raft-rs as a dependency (its own log trait and protobuf entries). The log states the weakness: no spike was run, and consensus is the riskiest item of the plan, so the work starts with a written protocol model and simulation.

D38 Replication v0: asynchronous log shipping to read-only replicas, promoted by hand#

A shard's log is already an ordered, checksummed, durable record stream, so a replica is a second copy of the same files: the primary streams durable log bytes, and the replica appends them unchanged and applies them with the same code that replays a log. A replica's log is therefore byte-identical to the primary's, which makes resume, promotion and testing simple. Continuity is checked with a CRC of the replica's last 4 KiB; a mismatch makes the replica rebuild that table from zero. Replicas refuse writes, and a promote call allows them. Left for later steps: automatic failover, synchronous replication, replicating accounts, TLS between nodes and multi-master.

D39 Synchronous replication is a per-primary setting with a timeout fallback#

With --sync-replicas N, a commit's replies are held until N followers confirm that the log is durable on their own disks up to that record, so a read on the replica right after the primary answered sees the write. The wait is bounded by a timeout (default 1000 ms); on timeout the write is acknowledged anyway and counted as a degraded commit, because refusing all writes whenever a replica is down turns one failure into an outage. Operators who prefer blocking can set a very long timeout.

D42 Automatic failover: Raft elects the primary, log shipping moves the data#

Each member ran the Raft core over an authenticated peer port; the Raft log carried only the no-op of each term, so it gave terms and a majority vote, while data kept travelling by log shipping. The rules: writes are acknowledged only after the primary and half of the other nodes hold them; a node votes only for a candidate whose data position is at least its own, so a candidate lacking an acknowledged write cannot win; a former primary is "dirty" and refuses votes until it follows a newer primary that accepted its copy; the primary accepts writes only after its term's first entry is committed. A test found a stale node winning because followers did not publish their applied position, and it was fixed. A majority must survive; two failures of three stop writes. Rejected: promoting by a script (split brain). Part of this design (where the election runs) was replaced by D51.

D47 Replication and failover for lsm tables#

The lsm engine deletes its own log segments after a flush, so it cannot offer a log to ship. An lsm shard therefore keeps a second log, the replication log, in exactly the file and record format of the in-memory engine's shard log, and the record format got one owner shared by both engines. Everything that already worked on the log (following, byte-exact resync, acknowledgements, promotion) then works unchanged for lsm tables.