Cruxly

Cruxly › System design › Foundations of Data Systems

Replication and Consistency

copies disagree; decide who notices

Open the interactive lesson

Replication is how you survive machine failure and serve reads at scale. The moment there is more than one copy, the copies can disagree, and your job is to decide precisely who is allowed to see a disagreement and for how long.

Two reasons to replicate, and they are different

ReasonWhat it buysWhen it applies
Durability and availabilityN copies survive N−1 machine failuresAlways, for anything you can't regenerate, whatever the read load
Read scalingReplicas multiply read capacityOnly when reads dominate. At 1:1, every replica still applies every write, so you gain almost nothing

Keep them apart: "Three replicas for durability; reads go to replicas only where lag is acceptable." "I'll add replicas for scale" leaves the interviewer unsure you know the difference.

Single-leader

All writes go to one primary; replicas follow its log. Almost every relational deployment works this way, and for most designs it's the right answer.

You get no write conflicts (one writer, one order) and mature tooling. You pay:

CostWhyFix
Write bottleneckReplicas apply every write too, adding no write capacityPartition
Replication lagAsync ack comes before followers have the data: ms normally, seconds under load. Users see "I posted it and it disappeared"; the write isn't lost, just not there yetRead-your-own-writes
Risky failoverDetection needs a timeout: slow detection or false positivesFencing

:::key Read-your-own-writes After a user writes, route that user's reads to the primary for a few seconds. You don't fix lag; you hide it from the one person who would notice. :::

:::race Split brain and fencing Promoting a follower that was behind loses acknowledged writes, and two nodes may both think they're primary. Fix: a monotonically increasing epoch the storage layer checks on every write, so the demoted primary's stale-epoch write is rejected (the fencing token from the distributed lock exercise). :::

Multi-leader and leaderless

Multi-leader: several nodes accept writes, usually one per region: local write latency, survives a region outage, and real write conflicts when two regions edit one record. Name the rule:

RuleTrade-off
Last-write-winsSimple, silently discards data; clock skew makes "last" a lie
Application mergeFor structured data, e.g. union two shopping carts
CRDTsMerge deterministically by construction; only counters, sets, text and similar

Leaderless (quorum): write to W of N nodes, read from R. If W + R > N, read and write sets overlap on at least one node, so a read sees the latest write. W=N, R=1 gives fast reads and fragile writes; W=1, R=N the reverse; W=R=2, N=3 is the balanced default.

:::warn W + R > N is not strict consistency (the differentiator)

:::

Synchronous versus asynchronous

ModeAck whenLost if the primary diesWrite latency
SynchronousA replica has itNothingReplica's: 100 ms+ cross-region; writes block if it's slow or down
AsynchronousImmediatelyAcknowledged writesFast
Semi-synchronousOne replica has it, rest asyncNothing if one machine diesOnly the fastest replica's

:::key The answer being fished for Synchronous within a region (sub-millisecond), asynchronous across regions, and the exposure stated: a few seconds of writes if a whole region is lost. :::

Saying consistency precisely

Name the guarantee, the window, and who notices. Strongest first:

GuaranteeMeaningCost
LinearizableActs like one copy; every read sees the latest writeCoordination per operation; cross-region latency
SequentialEveryone sees the same order, not necessarily currentSingle-leader ordering
Read-your-own-writesYou see your own changes; others may lagPin the writer's reads to the primary
Monotonic readsTime never goes backwards for youPin a session to one replica
EventualConverges once writes stopNothing, but explain the window

Monotonic reads is the underrated one: without it, reads from two replicas can show a comment appearing, then vanishing. Sticky sessions fix it.

Attach guarantees to data, not the system: "Seat inventory is linearizable, since double-selling is unacceptable; the 'N people viewing' badge is eventual, maybe ten seconds stale, which nobody can detect." That's what real designs do.

The vocabulary: failover, data replication, and shared-nothing

Interviewers use these as shorthand and expect them back precisely.

TermMeansThe precise part
FailoverA secondary takes over when the primary is detected deadA mechanism, not a replication mode. Say what detects the failure and what stops the old primary acting as one
Data replicationAn active node's writes are mirrored to standby copiesMakes failover safe: a secondary helps only if it already has (nearly) all the primary's data
Shared-nothingNodes share no memory and no disk, and coordinate only over the networkContrast: shared-disk, many nodes on one storage array

Fencing a primary that owns its disk: an epoch only fences where something shared checks it (storage, a lock service). Otherwise use leader leases (the old primary stops accepting writes when its lease expires, before a new one is granted) or STONITH ("shoot the other node in the head": power off or isolate it).

Shared-nothing, precisely:

How to say it in the interview

"I need replicas for durability regardless. The design question is whether reads may hit a replica — for the feed, yes, a two-second lag is invisible; for the author's own post, no, so I'll pin their reads to the primary briefly after a write."

Checklist

Practice this interactively
Share this lessonWhatsAppLinkedInX

Related lessons