CAPSTONE: REPLICATED KV UNDER PARTITION

Everything in this path, in one three-node store

L320 MINDISTRIBUTED SYSTEMS
PREDICT

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.

▶ SIMULATION
N1
role: LEADER · term 1
log: —
balance = 100
N2
role: FOLLOWER · term 1
log: —
balance = 100
N3
role: FOLLOWER · term 1
log: —
balance = 100
modeNAIVE (local ack, no fencing, no dedup)
clientsCA last reply: — · CB last reply: —
acknowledged— → expected balance 100
eventN1 leads term 1; balance = 100 on every node
✓ an acknowledged write is never lost✓ at most one leader per term✓ no request is applied twice
SCHEDULE
01[NET]NAIVE store
02[CA]CA: w1 +50 → N1 (reaches all three nodes, commits)
03[NET]partition {N1, CA} | {N2, N3, CB}; the ack for w1 is lost
04[CA]CA: w3 +5 → N1, which still thinks it leads
05[N2]N2: election timeout
06[CB]CB: w2 +20 → N2
07[NET]partition heals
08[CA]CA: w4 +1 → N1, which still thinks it leads term 1
09[N2]N2: sends its log to every node
10[CA]CA: timed out on w1 earlier; retries w1 +50 → N2 (same request id)
11[N2]N2: sends its log to every node
12[NET]check: acked writes vs every node's balance
Start of naive scenario. 12 steps.
// 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
}

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.

BREAK IT NOT YET

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

FIX IT NOT YET

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.

Mental model:

a replicated store is correct when three checks sit on the write path: majority before the ack, the term as a fencing token, and the request id stored with the result. Take any one away and a partition plus a retry is enough to lose or duplicate a write the client was told about.

IN PRODUCTION

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.

IN INTERVIEWS

“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.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: WEBHOOK DELIVERY →