Free preview

Single-Leader Replication

Why this matters: this is what almost every relational deployment runs. Its failure modes — a write bottleneck, a lost leader, and lagging reads — are the ones you will actually debug.

Key takeaway

In primary-secondary replication, one node is the primary (leader). It processes all write operations and propagates updates to secondary nodes (followers). This model is ideal for read-heavy workloads: scale by adding followers and distributing reads among them.

The model

All updates go to the primary; reads can be served by the primary or any secondary.

Two consequences follow immediately:

  • The primary becomes a bottleneck for writes — adding followers scales reads and does nothing for write throughput.
  • If the primary fails, asynchronous replication may cause inconsistency, because recent updates might not have reached the secondaries.

When the primary fails

A secondary is appointed as the new primary, which speeds recovery of the original. There are two approaches:

ApproachHow it worksTrade-off
ManualAn operator decides which node becomes primary and notifies all secondariesSlow — a human is in the recovery path — but deliberate and safe
AutomaticSecondaries detect the primary has failed and elect a new one through leader electionFast, but risks acting on a false positive

The three replication methods

How do the changes actually get to the followers? Three approaches, each with a specific failure mode.

MethodWhat shipsUsed byKey weakness
Statement-based (SBR)The SQL statements themselvesOlder MySQL versionsNondeterministic functions diverge
Write-ahead log (WAL) shippingLow-level byte changesPostgreSQL, OracleTightly coupled to engine internals
Logical (row-based)Row-level data changesPostgreSQL, MySQLMore verbose than WAL

Statement-based replication

The primary executes SQL statements — INSERT, UPDATE — and writes them to a log file. Secondaries read this log and re-execute the statements.

Best use case: workloads with many reads and few writes, where read requests can be distributed across followers to reduce load on the leader.

Drawback: nondeterministic functions such as NOW() generate different values on different nodes, causing data divergence.

Write-ahead log (WAL) shipping

Transactions are written to a log file on disk before execution. That log records low-level byte changes rather than SQL statements.

WAL ensures consistency for nondeterministic functions — the bytes are already decided — and aids crash recovery. Its weakness is that it is tightly coupled to the database engine's internal structure, which makes version upgrades across a cluster complicated: you generally cannot have a leader and follower running different engine versions, so rolling upgrades become difficult.

Logical (row-based) replication

Captures changes at the row level — which column values changed in which row.

Because it captures the logical data change rather than physical disk bytes or SQL statements, it is flexible and compatible across different engine versions. That makes it the method of choice when you need rolling upgrades or want to replicate into a different system entirely (a data warehouse, a search index, a change-data-capture stream).

The problems asynchronous replication creates

Asynchronous replication creates replication lag between primary and secondaries, with two consequences:

ProblemWhat happens
Data lossIf the primary fails before propagating writes, those updates are lost
Inconsistent readsA user writes data and immediately reads from a lagging follower, making it appear the data was lost

The solution: read-your-own-writes

If a user is accessing data they can modify — such as their own profile — route those reads to the leader to ensure strong consistency. For read-only or less consistency-sensitive data, follower replicas serve the reads.

This requires a mechanism to determine whether the data may have been recently modified, without issuing an extra consistency check. Profile data in a social network is usually writable only by the account owner, which gives a simple rule:

Always read the user's own profile from the leader; read any other user's profile from a follower.

Cons: this increases load on the primary node — the very node that is already the write bottleneck.

Key takeaway

Single-leader replication scales reads, not writes, and its correctness problems all stem from lag. Choose the replication method by what you need to survive: SBR for simplicity, WAL for engine-level fidelity, logical for cross-version and cross-system flexibility.

Interview signal by level

LevelWhat a strong answer sounds like
L4"One primary handles writes, replicas handle reads."
L5Handles the lag: "replicas lag, so a user reading right after writing might see stale data — route their own reads to the primary."
Staff+Covers failover and scale: "automatic failover needs fencing tokens or a partitioned primary comes back and split-brains us. And rather than sending every self-read to the leader — which loads the bottleneck — I'd use a version watermark so any caught-up follower can serve it."

Next: what happens when more than one node can accept writes.

Enjoying the preview?

Create a free account to unlock the rest of this course, the in-browser judge, and live AI mock interviews.

Sign up free to continue