Topic 15 notes — replication, consensus & distribution
Baseline (provided lane, Apple M3 Pro / APFS, measured 2026-07-28)
cargo run --release --bin repl_lag — 2000 entries × 128 B, leader
group-commits every 64, ack semantics WAIT 1 (one follower must acknowledge).
The follower’s fsync policy is the only variable.
| follower fsync | entries/s | ack p50 | ack p99 |
|---|---|---|---|
| every entry | 341 | 2967.0 µs | 3889.5 µs |
| every 8 | 2 730 | 22.2 µs | 2979.8 µs |
| every 64 | 12 187 | 14.0 µs | 2133.0 µs |
| never | 20 174 | 13.8 µs | 64.5 µs |
59× throughput between the safest and the loosest row, and the durability argument is entirely in the p99 column. Two things to sit with:
- The
every 8row already recovers most of the median (22.2 µs, within 60% ofnever) while its p99 stays at 2980 µs — batching hides the fsync from the common case and leaves it in the tail. If you judge a replication setting by its median you will pick a configuration whose worst case you have not seen. every entryat 341 entries/s lines up with topic 5’sF_FULLFSYNCrung (337 commits/s) almost exactly. The follower is not slow because replication is expensive; it is slow because it is paying the same physical media flush, once per entry. Same wall, different topic.
Predictions (fill BEFORE implementing raft.rs)
Topic 5’s fsync ladder, now visible as replication lag: in the baseline above the p50 drops 134× from every-entry to every-8 (2967.0 → 22.2 µs — most acks ride a group), but the p99 stays at 2–3 ms — someone always pays the F_FULLFSYNC.
| question | prediction | actual |
|---|---|---|
| ticks to first leader, 5 nodes (timeout 10-20, heartbeat 3) | ||
| how often does seed 0..10 hit a split vote (extra term)? | ||
| stale-leader test: how many ticks after heal until logs converge? | ||
| minority leader: does it stay Leader forever during partition? (it hears no higher term…) |
Implementation log
- raft.rs: tick (election timeout + heartbeats) — election tests green
- receive: RequestVote/Vote — one leader per term across seeds
- receive: AppendEntries consistency check + truncate; AppendResp next_idx repair — replicates_to_all green
- §5.4.2 commit rule — minority + stale-leader tests green
- partition_test timeline recorded here:
Surprises / dead ends:
Questions from the reading guides
Raft paper (reading-raft-paper.md)
- Why persist (term, voted_for, log) but not commit_index:
- Fig 8 without the current-term rule — which intersection fails:
- Why a leader never overwrites its own entries:
- Snapshot needs last_included_term because:
- valkey vs Raft: what async gives up, what it gets back:
valkey replication.c (reading-valkey-replication.md)
- Statement-shipping vs WAL-shipping ↔ topic 5 logical/physical:
- Backlog-size inequality for partial resync success:
- Chained replication offset coherence:
- Why full sync forks (COW):
- M15 stage 1: which PSYNC parts to keep:
raft-rs (reading-raft-rs.md)
- Why no fsync/sockets/threads in the library:
- maybe_commit on matched=[7,5,5,3,2] → commit index:
- next_idx decrement optimization (§5.3 body, not a footnote — and the paper doubts it is necessary, while raft-rs implements it):
- advance_append pipelining — what still can’t reorder:
- Ready → M15 stage 2 mapping:
qdrant consensus (reading-qdrant-consensus.md)
- 10K upserts/s through raft = ? (use the 3 ms fsync above):
ReplicaState’s eleven variants (replica_set_state.rs:100-133) ↔ Progress replicate/probe/snapshot — start with Active/Dead/Partial, then say whereActiveRead(readable, not a source of truth,:125) andManualRecoveryland:- qdrant vector-read consistency:
- WAL-through-raft: right for a graph DB? FalkorDB’s answer:
- Storage impl behind ConsensusStateRef:
VSR (reading-vsr.md)
- Round-robin primary: removes / costs:
- DOVIEWCHANGE ships logs vs Raft repairs later — when each wins:
- VSR-no-disk loses committed data when:
- Recovery nonce prevents:
- TigerBeetle’s “disk can lie” ↔ topic 5 torn pages:
DDIA ch. 5/8/9 (reading-ddia-repl.md)
- {async, semi-sync, raft} × {RYW, monotonic, prefix} matrix:
- WAIT-1-then-wrong-promotion — which guarantee broke:
- Terms as fencing tokens in M15’s follower:
- Why FLP doesn’t doom Raft in practice:
- Linearizable reads: lease vs ReadIndex vs quorum — M22 pick:
Cross-topic threads
- repl_lag IS topic 5’s fsync ladder: the follower’s durability policy becomes the leader’s observable ack latency. Consensus makes this mandatory (majority must fsync before commit).
- sim.rs = topic 16’s DST in miniature: seeded delivery order + no wall clock ⇒ every partition bug replays from a u64.
- valkey’s replica handshake state machine = topic 7’s nonblocking event-loop pattern; the shared repl buffer = client output buffers.
- (replid, offset) vs (prev_index, prev_term): PSYNC can resume a stream but can’t DETECT divergence; Raft’s consistency check can.
- Hash slots vs ranges = topic 13’s problem in disguise: traversals (range scans) want locality, uniform load wants hashing.
M15 log (WAL shipping → Raft)
- stage 1: M5 WAL streamed over M7 RESP server, PSYNC-shaped (replid+offset, backlog ring, +CONTINUE/+FULLRESYNC)
- WAIT-style ack levels; kill -9 failover: measure acked-write loss per ack level
- stage 2: experiments/raft.rs → the WAL commit path
- latency: async vs WAIT 1 vs raft, same workload (compare against the repl_lag table above)
- read-path decision (stale follower reads?) recorded for M22/M29
Done when
- All 5 raft tests green across seeds; partition_test timeline shows commit freeze + truncation-on-heal; prediction table filled.
- Reading-guide questions answered; M15 stage-1 design sketched.