Sikau

System design · Day 5 · about 40 min

Replication and quorums

Copies on several machines; R + W > N keeps reads fresh.

Machines die

Keep only one copy of your data and one dead disk loses it. So databases replicate: each value lives on N machines. Waiting for all N on every write is slow, so systems like Cassandra and DynamoDB use a quorum: write to W replicas, read from R. If R + W > N, every read overlaps at least one replica holding the latest write.

A tiny replicated store

const isQuorum = (n, r, w) => r + w > n; // every read set overlaps every write set

function createCluster(n) {
  const replicas = Array.from({ length: n }, () => new Map());
  const alive = Array(n).fill(true);
  let version = 0;
  function choose(count, start) {
    const chosen = [];
    for (let k = 0; k < n && chosen.length < count; k++) {
      const i = (start + k) % n;
      if (alive[i]) chosen.push(i);
    }
    if (chosen.length < count) throw new Error("Not enough replicas");
    return chosen;
  }
  return {
    write(key, value, w, start = 0) {
      const chosen = choose(w, start);
      version++;
      for (const i of chosen) replicas[i].set(key, { value, version });
      return version;
    },
    read(key, r, start = 0) {
      const chosen = choose(r, start);
      let best;
      for (const i of chosen) {
        const entry = replicas[i].get(key);
        if (entry && (!best || entry.version > best.version)) best = entry; // newest wins
      }
      if (best) for (const i of chosen) if ((replicas[i].get(key)?.version ?? 0) < best.version) replicas[i].set(key, best); // read repair
      return best?.value;
    },
    down(i) {
      alive[i] = false;
    },
    up(i) {
      alive[i] = true;
    },
    peek: (i, key) => replicas[i].get(key)?.value,
  };
}

The rest of this lesson

  • Why R + W > N worksexplain
  • Trade-offs in practiceexplain
  • N = 3, W = 2, R = 1predict
  • Is it a quorum?match
  • Quorum reads and writeswrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 8 lessons of System design