CMD Guide
HomeSystem DesignSystem Design Building Blocks

Redundancy and Replication

Redundancy and replication

Redundancy is the deliberate duplication of critical components or functions so that a failure in one place does not take down the whole system. If a file lives on exactly one server, losing that server loses the file; keeping a second copy on a second server removes that single point of failure.

Database replication is the mechanism that keeps those duplicate copies (replicas, or followers) in sync with the original. Two independent questions define any replication design, and keeping them separate is half the battle:

Axis 1 — topology: who is allowed to accept writes?

  1. Single-leader — one node (the leader, or primary) accepts every write and streams changes to its followers. The default in MySQL, PostgreSQL, and most managed relational services.
  2. Multi-leader — several nodes accept writes (typically one leader per region or datacenter) and replicate to each other; each leader keeps an ordered log, and conflicting writes are resolved when the leaders' streams merge.
  3. Leaderless — there is no fixed leader at all; any replica can accept a write, and quorums of replicas coordinate to stay convergent (the Dynamo model).

Axis 2 — acknowledgment mode, within leader-based replication: when does the leader tell the client "done"? Synchronous — after a follower confirms it has the write; asynchronous — immediately, replicating in the background; semi-synchronous — the tunable middle ground, waiting for just one fast follower instead of all of them. These are latency/durability settings on a leader-based topology, not topologies themselves: the same single-leader pair can run in any of the three modes.

The rest of this page walks through the ack modes of single-leader replication and the leaderless alternative — how each works, when to reach for it, when not to — plus a worked example of the lag that asynchronous replication introduces, including a precise definition of what "lag" actually measures.

diagram
diagram

Synchronous replication

In synchronous replication, the leader does not consider a write complete until at least one follower has confirmed it received and applied the change. This gives the pair strong consistency: whatever the client was told succeeded is guaranteed to exist on both the leader and that follower, even if the leader crashes the instant after acknowledging.

When to use it

When not to use it

Asynchronous (and semi-synchronous) replication

In asynchronous replication, the leader commits and acknowledges the client immediately; the change reaches followers afterward, over a queue. This decouples client-perceived latency from follower distance or health, but it means a follower can be meaningfully behind the leader at any given moment. Semi-synchronous replication sits between the two extremes: the leader waits for an acknowledgment from at least one follower — whichever responds first — before committing, while the rest replicate asynchronously. That gives a bounded-loss guarantee at a much smaller latency cost than waiting for every follower.

When to use it

When not to use it

Leaderless replication

Leaderless (quorum-based) replication removes the fixed leader entirely. With N replicas, a write is sent to as many as are reachable and counts as successful once W replicas acknowledge it; a read queries R replicas and returns the most recent version it sees, using per-value versioning (vector clocks or timestamps) to detect which copy is newest. As long as W + R > N, every read quorum is guaranteed to overlap every prior write quorum by at least one replica, so a read can never miss the latest acknowledged write entirely — though it may have to reconcile competing versions. Replicas that fall behind (a network blip, a restart) are brought back in line later through read repair or background anti-entropy, and a temporarily unreachable replica's write can be handed to a stand-in node and reconciled once it returns (hinted handoff).

When to use it

When not to use it

What "replication lag" actually measures

Replication lag is, at its core, a time quantity: the delay between the moment a write becomes durable on the leader and the moment that same write becomes durable (or visible to reads) on a follower. In practice it's often tracked via a position delta instead — the gap between the leader's current log sequence number (LSN, or binlog offset in MySQL terms) and a follower's applied LSN — because comparing two log positions is cheap and needs no clock synchronization between hosts.

That position delta is a count of log entries, not a duration, and the two are only related through the write throughput at the time the gap was measured: time-lag ≈ LSN-gap ÷ writes-per-second. Because throughput changes constantly, the same 18-entry gap can represent very different amounts of wall-clock delay depending on how fast the leader was writing when it opened up. Tools such as MySQL's Seconds_Behind_Master or PostgreSQL's replay_lag report a genuine time value because they compare timestamps — often via a small heartbeat row replicated like any other write — rather than reporting the raw LSN difference.

Worked example: watching an asynchronous follower's lag

A leader has one synchronous follower (A) and one asynchronous follower (B). At t = 0 ms the leader appends write W at LSN 1050. The table tracks LSNs and the resulting gap at each point in time; the chart further below plots the same five points, with the x-axis marking each row's timestamp rather than a linearly scaled clock, since the intervals between rows range from 8 ms to 100 ms.

TimeLeader LSNFollower A (sync)Follower B (async)What's happening
t = 0 ms10501049 · lag 11032 · lag 18Leader appends W, sends it to both, and blocks waiting for A's ack — A hasn't applied it yet, so its lag is still 1 at this instant.
t = 8 ms10501050 · lag 01033 · lag 17A applies W and acks over an 8 ms link; the leader now commits and returns success to the client. B advances on its own schedule, unrelated to this ack.
t = 100 ms10531053 · lag 01038 · lag 15Three more writes land; each blocks on A the same way, so A never falls behind. B isn't on the critical path, so these commits don't wait for it.
t = 200 ms10551055 · lag 01047 · lag 8Write traffic drops to near zero. With no new entries to chase, B's replay thread starts closing the backlog instead of merely keeping pace.
t = 300 ms10551055 · lag 01051 · lag 4B keeps draining during the lull; the 18-entry gap from t = 0 ms is almost gone.

Two things to notice. First, A's lag is never applied-before-acknowledged: the t = 0 ms row shows A one entry behind precisely because the leader is still waiting on its ack, and A only reaches lag 0 once that ack has actually happened, at t = 8 ms. Second, be careful whose rate you divide by when converting an entry gap into time — the table gives you two rates, and they answer different questions. At follower B's apply rate (roughly 15 ms per entry, visible as B drains 13 entries — 1038 → 1051 — over the 200 ms from t = 100 ms to t = 300 ms), an 18-entry backlog is about 270–360 ms of drain time: how long B needs to catch up once the leader goes quiet. Dividing by the leader's write rate instead answers a different question — how much wall-clock write history the gap spans. This leader appends only 5 entries over the whole 300 ms window (1050 → 1055, one every 40–60 ms), so by that measure the same 18-entry gap represents roughly 720–1080 ms of the leader's writing. The formula time-lag ≈ LSN-gap ÷ writes-per-second from the previous section only means something once you say which actor's rate sits in the denominator; here the two answers differ by a factor of two to four.

diagram
diagram

Choosing a replication design

DesignWrite latency costData-loss window on leader failureBest fit
Single-leader, synchronousFull round trip to the follower(s), every writeNone for acknowledged writes (RPO = 0)Financial/ledger writes; nearby follower
Single-leader, asynchronous / semi-synchronousNone (async) or one fast round trip (semi-sync)Unbounded (async) or limited to the slowest-acking follower's backlog (semi-sync)Read scaling, cross-region DR, latency-sensitive writes
Multi-leaderLocal — the client writes to its own region's leader; leaders replicate to each other asynchronouslyA failed leader's not-yet-replicated local writes; concurrent cross-leader writes need conflict resolution at mergeMulti-region active-active where per-region write ordering matters
Leaderless (quorum)Round trip to W of N replicas, no single leader hopNone for the quorum, but concurrent writes may need reconcilingMulti-region active-active, availability over strict ordering

None of these is a strictly better default — the right choice tracks how expensive a lost acknowledged write is versus how expensive extra write latency (or, for leaderless, conflict resolution) is for the specific data being replicated.

Whichever topology you pick, replicas only buy fault tolerance if they fail independently. A replication factor of 3 with all three copies in the same rack (shared power/switch) or same availability zone converts one correlated event — a rack PDU trip, an AZ network partition — into the loss of every copy at once, defeating the redundancy on paper. Spread replicas across independent failure domains (racks, then AZs, then regions as durability demands rise); the further apart they sit, the more independent the failures but the higher the synchronous-write latency, which is the same latency-vs-durability trade-off the topologies above make, now expressed as physical placement.

What an interviewer probes here

Everything above compresses into a handful of follow-up questions that come up almost verbatim. Check that you can answer each one from this page alone:

Sources

Concepts and terminology in this lesson draw on: Martin Kleppmann, Designing Data-Intensive Applications (O'Reilly, 2017), Chapters 5 ("Replication") and 9 ("Consistency and Consensus"), for the synchronous/asynchronous/leaderless replication models and the W + R > N quorum condition; DeCandia et al., "Dynamo: Amazon's Highly Available Key-value Store" (SOSP 2007), for leaderless quorum mechanics, hinted handoff, and read repair; and the MySQL and PostgreSQL replication documentation for synchronous/semi-synchronous configuration and lag-monitoring conventions (Seconds_Behind_Master, replay_lag). LSNs and timings in the worked example are illustrative, not measurements from a specific running system.

🤖 Don't fully get this? Learn it with Claude

Stuck on Redundancy and Replication? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Redundancy and Replication** (System Design) and want to truly understand it. Explain Redundancy and Replication from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Redundancy and Replication** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Redundancy and Replication** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Redundancy and Replication** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes