CAPSTONE: REPLICATED KV UNDER PARTITION
Everything in this path, in one three-node store
Three nodes N1, N2, N3 replicate a balance as a log; N1 leads term 1. A partition cuts N1 off from N2 and N3, which elect N2 leader of term 2. In NAIVE mode nothing checks for a majority, so when client CA sends w3 +5 to N1, which still thinks it leads, N1 acks it. What happens to w3 when the partition heals?
Three nodes replicate one balance as a log. N1 leads term 1. A partition puts N1 and client CA on one side, N2, N3 and client CB on the other. N2 wins term 2. CA timed out on a write that actually committed, so it retries against N2.
Every earlier lesson in this path has one job here. Majority acks stop the minority from acknowledging writes it will lose. The term on each write fences off the stale leader. The request id turns the retry into a no-op. Log catch-up after the heal makes every node agree.
// any node that believes it leads:
function handleWrite(req) {
log.append(req); // no term, no dedup
apply(req);
return ack(); // before anyone else has it
}function handleWrite(req, term, reachable, store) {
// followers do the fencing: they reject entries from a lower term, and
// their replies (and any vote request) tell this node the highest term seen.
if (term < store.highestTerm) return refuse('stale term'); // replaced: step down
if (reachable < majority) return refuse('no majority'); // minority side
const prev = store.get(req.id);
if (prev) return ok(prev); // retry: stored result
const result = store.apply(req.amount, term); // replicated to a majority
store.set(req.id, result); // with the result, atomically
return ok(result);
} Watch the three invariants. In NAIVE, w3 is acknowledged, then dropped from N1 when N2 sends its log after the heal, and the w1 retry is applied a second time. In RESILIENT, under the same partition and the same retry, N1 refuses w3, N2 deduplicates w1, and every node ends at the balance the acknowledged writes add up to.
The resilient store survives the partition. Inject one more fault so that CB's write w2 is never acknowledged. Then read the invariant strip: what did you break, and what did you not?
GOAL: w2 is never acknowledged, and every invariant still holds
Make the leader's write path safe. Refuse if your term is lower than store.highestTerm, the highest term this node has heard from a peer. Followers do the fencing: they reject entries from an older term, and their rejections (or a vote request) are how a replaced leader learns the new term and steps down. A stale leader that hears nothing is stopped by the majority check instead. Refuse if reachable nodes, counting yourself, are not a majority of store.clusterSize. If store.get(req.id) has a result, return it without applying. Otherwise apply, record the result under the id, and return it.
etcd, Consul, CockroachDB and every Raft- or Paxos-backed store, plus the client libraries in front of them that retry with request ids. The symptoms when one check is missing: writes lost after a failover, or duplicated after a timeout.
“Design a replicated key-value store that survives a partition.” Cover leader election by majority, acks only after a majority holds the write, terms or epochs as fencing tokens, client request ids for retries, and log catch-up after the heal. Say that the minority side refuses writes, and why.