6. Replication and consistency
One-liner. Copies buy availability and read scale, and sell you freshness.
Memorize
- Leader–follower: writes to leader, reads from followers → replica lag (read-your-writes problems).
- Multi-leader / active-active: conflict resolution needed (LWW, CRDT, version vectors).
- Quorum:
R + W > Ngives strong-ish consistency; tune R/W for latency vs freshness. - CAP is about partitions: during a partition you choose consistency or availability.
- PACELC adds the normal case: latency vs consistency even without failures.
- Patterns: read-your-writes (route to leader after write), monotonic reads (sticky replica).
Trap. "Read replicas give strong consistency" — they're asynchronous by default; you get lag. Do this. Ask what happens if a user reads 50 ms after writing.
Check
After a profile update, a user refreshes and sees old data. Reads go to a lagging replica. Cheapest correct fix?
Why
This is read-your-writes. Pinning all reads to the leader fixes correctness but gives up read scaling.