# Replication and Consistency — System Design Interview Prep

Source: https://www.skillbyai.com/en/system-design-interview/d-replicate

> Copies for availability and reads.

## Leaders, followers and quorums

With **leader-follower** replication, writes go to the leader and are copied to followers that serve reads; asynchronous replication can make followers lag, so a user may not see their own write (use read-your-writes routing for that user). **Leaderless** systems (Dynamo-style) write to W of N replicas and read from R; if **R + W > N**, read and write sets overlap, so a read sees the latest acknowledged write. The **CAP** theorem says that during a network partition you must choose between consistency and availability.

## Which quorum settings overlap?, run

I ran this with Python 3.12.3 using only the standard library; inputs are fixed or seeded, so the output is reproducible. For N = 3, every combination with R + W greater than 3 guarantees overlap; W = 2 and R = 2 is the common balanced choice.

```python
# Leaderless replication: when do reads see the latest write?
N = 3
for W in range(1, N + 1):
    for R in range(1, N + 1):
        overlap = R + W > N
        note = "strong (overlap)" if overlap else "may read stale"
        print(f"N={N} W={W} R={R}: {note}")
```

Output:

```
N=3 W=1 R=1: may read stale
N=3 W=1 R=2: may read stale
N=3 W=1 R=3: strong (overlap)
N=3 W=2 R=1: may read stale
N=3 W=2 R=2: strong (overlap)
N=3 W=2 R=3: strong (overlap)
N=3 W=3 R=1: strong (overlap)
N=3 W=3 R=2: strong (overlap)
N=3 W=3 R=3: strong (overlap)
```

## Name the consistency you need per feature

A like counter can be eventually consistent; an account balance cannot.

**Quiz:** With N=3 replicas, which setting guarantees reads overlap the latest write?

- [ ] W=1, R=1
- [x] W=2, R=2
- [ ] W=1, R=2
- [ ] W=2, R=1

*Answer:* W=2, R=2. R + W must exceed N.
