Lesson 04 · Architecture & System Design
Replication, partitioning, consistency, and why consensus is hard — the no-math mental models behind every "weird" bug at scale.
In Lesson 03 you learned to read a system by asking where data flows and what fails first. This lesson goes inside the data layer, because that's where the strangest bugs at scale are born: a comment that vanishes on refresh, one server on fire while its siblings idle, two data centers both convinced they're in charge. None of these are freak accidents — they are the predictable consequences of three moves every large system makes: keeping copies of data, splitting data, and deciding who must agree before anything counts.1 No math today — just the mental models. They're what staff+ system-design interviews actually probe.
You replicate data — keep the same data on several machines — for three reasons: so a dead machine doesn't lose it, so reads can be served by many machines instead of one, and so data can live near the users who read it.1 The most common arrangement is leader/follower: all writes go to one machine (the leader), and the followers copy its changes. Think of a newspaper: one printing press sets the edition, and trucks carry copies to newsstands across the city. Anyone can read at any newsstand — but if you want to publish, you go to the press.
Here's the trade-off you already know from Lesson 02: does the leader wait for followers to confirm each write? Wait for all of them and every write crawls; wait for none and followers fall behind. Almost everyone picks speed — which means followers are always slightly stale. That gap is replication lag: reading a follower is like reading yesterday's newspaper. Nothing in it is wrong — it was all true when it was printed — it's just not current. Usually the lag is milliseconds and nobody notices. Under load it stretches to seconds or minutes, and then users notice.1
What they notice has names. A user posts a comment (written to the leader), refreshes (read from a lagging follower) — their comment is gone. They didn't lose data; they read yesterday's paper one second after publishing today's. The guarantee that prevents this is read-your-own-writes: after I write something, my reads must see it — a cheap, targeted promise, typically kept by routing a user's reads to the leader for a short window after they write. The cost: more load on the leader and routing complexity.1 A cousin bug: two refreshes hit two differently-lagged followers, and the user sees time move backwards — fixed by pinning each user to one replica, at the cost of lumpier load. Notice the pattern: every fix is a trade, never a free lunch.
Replication copies all the data to each machine — which stops working when the data or the write traffic no longer fits on any single machine. Partitioning (sharding) is the other move: split the data by key, so each machine owns a slice. Like an encyclopedia in volumes — A–F on one shelf, G–M on the next — every record has exactly one home, found by its key. Real systems do both at once: partition the data, then replicate each partition.2
Partitioning's signature failure is the hot partition. Splitting helps only if load spreads evenly — and real-world keys refuse to. One celebrity's posts, one viral product on sale day, one enterprise customer's launch: suddenly every request wants the same volume of the encyclopedia, and one shard melts while its siblings sit idle. Adding more shards does nothing — the hot key still lives on one of them. Real mitigations reshape the key (split the hot key into sub-keys, cache it in front, or isolate the big tenant), and each buys relief by adding complexity you'll pay for later.2
| Replication | Partitioning | |
|---|---|---|
| Problem it solves | One machine can die, or can't serve all the reads | One machine can't hold all the data or writes |
| The move | Same data, many copies | Different slices, one home each |
| Signature failure | Lag — stale reads, vanishing writes, time moving backwards | Skew — one hot shard on fire, the rest idle |
| First question to ask | "Who reads stale data, and does it matter here?" | "Which key gets famous, and what happens then?" |
"Consistency" sounds abstract until you phrase it as a user experience. Strong consistency means the system behaves as if there were one copy: once a write completes, everyone sees it. Eventual consistency means copies converge eventually: reads are fast and keep working through failures, but may be stale in the meantime.1 Neither is "better" — it's Lesson 02 again, a straight trade4: strong consistency spends latency and availability (machines must coordinate, and must sometimes refuse to answer rather than answer wrong) to buy simplicity for users and developers. The Principal-level move is choosing per feature, not per system: a like-counter can be a day-old newspaper; an account balance or an inventory decrement cannot. And often the honest requirement is smaller than "strong" — read-your-own-writes is exactly such a scoped promise, much cheaper than global strong consistency.1
One puzzle remains. Leader/follower assumes everyone agrees who the leader is. So the leader dies, a standby takes over — easy? Here's the trap: from inside a network, a dead machine and a slow network look identical.3 If a standby promotes itself whenever the leader goes quiet, then one network blip creates two leaders, both accepting writes that contradict each other. That's split brain — like two store co-managers whose radio link drops: each assumes the other is gone, both start giving orders, and the staff execute both sets. And notice why "just use a leader to decide" begs the question — choosing the leader requires everyone to agree on the choice, and agreement-under-failure is the very problem we're solving. The real answer is majority voting: nothing counts unless a majority of nodes confirms it, so of two rival leaders, at most one can ever hold a majority — the minority side is forced to stand down. It works, and it costs: extra machines, extra coordination on every decision, and a minority partition that refuses writes entirely.3 Which is why the standing advice is: don't build consensus yourself — lean on systems that have it (your managed database's failover, ZooKeeper, etcd), and know what they're protecting you from.
Time to run these models against realistic incidents — the exact skill of a Principal in an incident channel or a system-design interview. Below are four incidents. For each one, write three lines in a doc: (a) the distributed-systems phenomenon, named precisely; (b) why it happens, mechanically; (c) one mitigation and the trade-off it introduces.
Then bring the sheet back to me in chat. I'll review it against a Principal-level rubric: did you name the mechanism (not just "lag" but which guarantee broke), does your mitigation state its cost (a fix with no trade-off named is a red flag at this level), and would your explanation survive an interview follow-up ("okay — and why doesn't adding more shards fix incident 2?"). File the sheet in your evidence trail — Lesson 12 turns these into interview stories, and "I can diagnose distributed failures on sight" is one of the strongest.
Three scenarios. Reason from the mental models — newspapers, encyclopedias, co-managers — not from memory. Wrong picks stay live.
Scenario A
Users update their shipping address, hit save, and land back on the review page — which shows the old address. It corrects itself if they wait a bit. Writes go to the leader; reads go to read replicas. Your diagnosis and first move?
Scenario B
Ticket sales open for one mega-tour. The events database is sharded by event ID across 12 shards. Within minutes, one shard is at 100% CPU and requests to it time out; the other eleven are idle. An engineer proposes doubling to 24 shards. Your call?
Scenario C
A team's failover script promotes the standby database whenever the leader misses three heartbeats. During a routine switch upgrade, the network hiccups — and both nodes spend ten minutes accepting writes. Beyond tuning timeouts, what's the fundamental flaw?
Hand-picked follow-ups. None are required — the primary source above comes first.