Skip to main content

Primary-Secondary Replication

1. Executive Summary​

Primary-secondary replication (also called leader-follower or master-replica) is the dominant replication topology in production databases: one primary (leader) accepts all writes; secondaries (followers) receive a log of changes and apply them locally. Clients may read from secondaries for scale and locality, but write ordering is centralized at the primary. Kleppmann (DDIA, Chapter 5) treats this as the default replication model because it avoids write conflicts while providing a clear serialization point.

The model trades write scalability (single leader bottleneck) for simplicity of consistency: if replication is synchronous, followers reflect committed writes before the client receives acknowledgment; if asynchronous, the primary can acknowledge before all followers catch up—improving latency and availability at the cost of replication lag and data loss risk on failover. Production systems—PostgreSQL streaming replication, MySQL binlog replication, MongoDB replica sets (single writable primary), and cloud RDS read replicas—are variations on this theme with different failover, quorum, and consistency mechanisms.

This chapter covers the replication log, sync vs async tradeoffs, read-your-writes and monotonic reads, failover pitfalls (split-brain, lost writes), and principal-level design criteria for when primary-secondary is the right foundation versus multi-leader or leaderless alternatives.

2. Why This Topic Matters​

Primary-secondary replication is the baseline mental model for distributed data at principal-architect interviews. Most engineers have operated a database with a primary and replicas; fewer can explain:

  • What exactly is replicated (statement-based vs row-based vs logical log).
  • Whether a read from a replica is linearizable, monotonic, or arbitrarily stale.
  • What happens to in-flight writes when the primary fails.
  • How failover interacts with fencing, epoch numbers, and split-brain.

Interview failures include: assuming replicas are always consistent with the primary; ignoring replication lag in UX; designing global writes on a single leader without latency analysis; or conflating "replica set" with multi-master. Principal architects must connect replication mode to RPO/RTO, CAP behavior during partition (typically CP for the write path), and operational runbooks.

3. Problems Being Solved​

ProblemPrimary-secondary approach
Durability beyond one machineReplicate write-ahead log to followers
Read scalabilityServe reads from many secondaries
Geographic read localityRegional read replicas
Maintenance without downtimePromote replica; demote primary
Backup and analyticsSnapshot from secondary without load on primary
Disaster recoveryCross-region replica for RPO

Primary-secondary does not solve global low-latency writes (single leader RTT), write-heavy horizontal scale without partitioning, or availability during primary loss without failover complexity.

4. Assumptions and System Model​

Assume partial failure, asynchronous network unless sync replication configured, and crash-stop nodes (may restart with disk):

  • Single writable primary at a time (invariant for this topology).
  • Total order of writes established at primary (via log sequence).
  • Followers are passive for writes; apply log in order.
  • Failover may promote a follower; requires external coordination or consensus to avoid dual primaries.

Safety property (typical goal): No two primaries accept conflicting writes simultaneously (split-brain prevention).

Liveness property: System continues serving reads; writes resume after failover completes.

Not assumed: Automatic zero-data-loss failover without sync replication or quorum; linearizable reads from async replicas without additional protocol.

5. Essential Terminology​

TermDefinition
Primary / leaderNode accepting writes; appends to replication log.
Secondary / follower / replicaNode applying log; may serve reads.
Replication logOrdered stream of changes (WAL, binlog, oplog).
Synchronous replicationPrimary waits for follower ack before commit ack to client.
Asynchronous replicationPrimary acks after local commit; followers catch up later.
Replication lagDelay between primary commit and follower application.
FailoverPromote follower to primary after primary failure.
Split-brainTwo nodes both believe they are primary—safety violation.
Read replicaSecondary used for read scaling.
Hot standbyFollower ready for immediate promotion.
Semi-syncWait for at least one follower (MySQL semi-sync, etc.).
Catch-up replicationNew follower copies full snapshot then tails log.

Mnemonic: One writer, many readers, ordered log—the single serialization point.

6. Core Mechanism​

Write path​

  1. Client sends write to primary.
  2. Primary writes to local WAL/log, assigns log position (LSN, binlog offset).
  3. Replication stream ships log records to followers (streaming).
  4. Followers apply in order; update local state.
  5. Primary responds to client per sync policy (after local only, or after N followers).

Topology​

Figure 1: Single primary fans out ordered log to secondaries; reads may bypass primary.

Sync vs async commit​

Figure 2: Sync waits for follower; async acks before follower durable—lower latency, higher loss risk.

Failover sequence​

Figure 3: Failover requires detection, election, fencing, and explicit RPO handling for lagging replicas.

7. Step-by-Step Walkthrough​

Scenario: E-commerce order table; PostgreSQL primary + 2 async replicas; read replica for reporting.

StepEventObservation
1Customer places order; write to primaryOrder visible on primary immediately
2Replication streams to replicasLag ~50ms typical
3Customer refreshes page; read hits replicaStale read possible—order missing briefly
4Reporting query on replicaAcceptable lag for analytics
5Primary disk failureWrites fail; failover initiated
6Promote replica with least lagOrders not yet replicated lost (async RPO > 0)
7Old primary recoversMust not accept writes—fencing

Insight: Async replication is fast until failover exposes the durability gap. Product reads after write should route to primary or use read-your-writes session stickiness.

8. Invariants and Guarantees​

PropertySync replicationAsync replication
Write orderingTotal order at primaryTotal order at primary
Durability on primary failCommitted = on ≥1 followerCommitted may exist only on dead primary
Read from followerOften fresh post-commitStale possible
Linearizable readsPossible with routingNot from lagging replica
Single primaryRequiredRequired—enforced by ops/consensus

Safety: Prevent dual primaries (split-brain). Liveness: Failover completes in bounded time with external coordinator (etcd, Pacemaker, operator).

Kleppmann emphasizes: consistency guarantees are properties of the whole system, not the replication label—sync to one follower ≠ sync to all.

9. Failure Scenarios​

Scenario 1: Primary fails with async replication​

Setup: Last 2 seconds of writes not replicated.

Effect: RPO > 0—data loss on promotion.

Mitigation: Semi-sync, synchronous replication for critical tables, or accept loss with business sign-off.

Scenario 2: Split-brain after network partition​

Setup: Primary isolated; replica promoted; old primary still accepts writes.

Effect: Divergent histories—merge extremely costly.

Mitigation: Fencing (disable old primary), epoch numbers (MongoDB, Raft), STONITH in HA clusters.

Scenario 3: Replication lag thundering herd​

Setup: Replica 30s behind; all post-login reads hit replica.

Effect: Users see missing data after actions—support tickets.

Mitigation: Read-your-writes routing, lag-aware load balancing, sync for session-critical reads.

Scenario 4: Cascading replica failure​

Setup: Primary overload replicating to many slow followers.

Effect: Primary replication slots fill; disk pressure; liveness degradation.

Mitigation: Limit replica count, parallel apply workers, drop lagging replicas from read pool.

Scenario 5: Failover to lagging replica​

Setup: Automated failover picks replica without lag check.

Effect: Mass apparent "rollback" of recent data.

Mitigation: Prefer most caught-up replica; manual approval for large lag.

Scenario 6: Logical replication filter misconfiguration​

Setup: PostgreSQL logical replication publishes subset of tables; application assumes full database consistency across subscriber.

Effect: Join queries on subscriber return incomplete results—silent correctness bug distinct from lag.

Mitigation: Integration tests on subscriber; document replication scope; monitor replication slot lag per publication.

10. Performance Characteristics​

DimensionPrimary-secondary
Write throughputBounded by single primary
Write latency+RTT per sync follower waited
Read throughputScales with replica count
Read latencyLocal replica—low
Failover timeSeconds to minutes (detection + election)
Cross-region writesHigh latency to single leader

Qualitative: excellent for read-heavy workloads; write-heavy global apps need sharding or alternative topologies. Replication lag is unbounded under load unless monitored and capped by backpressure.

11. Scalability Limits​

  • Primary CPU/IO: All writes serialize—vertical scale ceiling.
  • Replication bandwidth: Log volume × replica count—large blobs amplify.
  • Follower apply rate: Single-threaded apply common—lag grows under write spikes.
  • Failover frequency: Flapping primaries degrade trust—stabilize detection thresholds.
  • Global users: Single-region primary imposes WAN RTT on every write.

Sharding partitions data across multiple primary-secondary groups—each shard retains single-leader semantics per key range.

12. Operational Considerations​

  • Monitor: Replication lag (bytes and seconds), replication slot disk usage, primary WAL retention.
  • Alert: Lag exceeding SLO; replica disconnect; failover events.
  • Runbooks: Documented promotion steps; verify fencing; client connection string updates.
  • Testing: Game-day failover; measure actual RPO/RTO—not vendor claims.
  • Schema changes: Online DDL coordination—replicas must apply compatible migrations.
  • Backup: Base backup + WAL archiving; test restore independently of replication.

13. Security Considerations​

  • Replication channel: Authenticate replicas (TLS, certificates); prevent rogue follower injection.
  • Read replica exposure: May lag behind permission revocations—staleness in security state.
  • Split-brain writes: Attacker triggering partition could exploit dual-primary window—fencing is security-relevant.
  • Logical replication: Row filters may leak sensitive data to less-trusted replica subscribers.

Replication does not replace encryption at rest or access control—it copies whatever the primary committed.

14. Cost Considerations​

  • Replica instances: Linear cost per read replica; cross-AZ/region egress for log shipping.
  • Sync replication: Extra follower capacity for durability; write latency may require larger primary.
  • Failover infrastructure: Consensus sidecar (etcd), HA proxy, operator—operational tax.
  • Under-replication: Async saves money until outage—insurance vs premium tradeoff.

Decision criterion: Async replicas for scale and DR with accepted RPO; sync when financial or inventory data loss is unacceptable.

15. Production Implementations​

PostgreSQL​

Streaming replication from WAL; synchronous_standby_names for sync subset; hot standby reads. Failover via Patroni, repmgr, or cloud managed service. Implementation choice: quorum vs any sync standby.

MySQL / MariaDB​

Binlog replication (async default); semi-sync plugin waits for one ack; Group Replication provides consensus-based multi-primary option—different topology when enabled.

MongoDB replica set​

Single primary elected via Raft-like protocol; oplog tailing; read concern levels (local, majority) change observed consistency—not same as replication mode alone.

Amazon RDS / Aurora​

Aurora storage-layer replication (6 copies across AZs); read replicas share storage—lower lag than traditional binlog shipping for many workloads. Verify current AWS documentation for durability claims.

Redis replication​

Async by default; Redis Sentinel for failover; recent versions add partial sync—in-memory constraints differ from disk databases.

Distinction: Managed services hide failover but do not eliminate CAP and lag tradeoffs—read SLAs carefully.

16. Alternatives and Tradeoffs​

TopologyWrite conflictsFailover complexityRead scale
Primary-secondaryNoneModerateHigh via replicas
Multi-leaderYesHigherHigh
Leaderless (quorum)Yes (concurrent)Lower single-node roleHigh
Chain replicationNoneChain head failureSequential

Choose primary-secondary when conflict-free writes and clear ordering matter; avoid when multi-region write latency dominates and conflicts are manageable.

17. Common Misconceptions​

MisconceptionReality
"Replicas are always consistent"Async lag always possible.
"Failover is instant"Detection + election + DNS takes time.
"Sync means no data loss"Sync to one follower still loses if both fail.
"Read replica offloads all load"Replication and apply still cost primary.
"More replicas = more durable"Durability depends on commit quorum policy.
"Primary-secondary = strongly consistent reads"Only with correct routing and sync/majority reads.

18. Principal Architect Perspective​

  1. State RPO/RTO explicitly for every tier—async is a business decision.
  2. Session consistency for UX: route post-write reads to primary or tracked replica.
  3. Fencing is non-negotiable for automated failover—budget STONITH or cloud API disable.
  4. Replication ≠ backup—replicas propagate deletes and corruption.
  5. Plan shard growth before primary hits write ceiling—migration is painful.

Kleppmann's framing: replication for high availability vs disconnected operation vs latency—primary-secondary excels at HA and read scale, not at multi-master write locality.

Capacity planning: Treat replication lag as a queue. If apply rate < write rate sustained, lag grows without bound—eventually reads are useless and failover RPO explodes. Principal reviews should include lag SLOs alongside CPU metrics.

Log shipping formats matter: Statement-based replication replays SQL on followers—non-deterministic functions (NOW(), UUID()) break correctness unless handled. Row-based replication ships row changes—safer for determinism but higher volume. Logical decoding (PostgreSQL) enables selective replication to analytics subscribers but adds operational surface area. Architecture reviews should state which format is in use and whether triggers and cascading deletes replicate as expected.

Read scaling caveats: Followers reduce read load on the primary only when queries are actually routed away. Connection poolers, ORM defaults, and sticky sessions often accidentally send most traffic to the primary—negating replica investment. Lag-aware routing requires measuring per-replica lag and excluding outliers from the read pool. For reporting workloads, snapshot isolation on a delayed replica is acceptable; for authorization checks after password change, it is not.

19. Architecture Review Exercise​

Scenario: Global SaaS; single US primary; EU read replicas; async replication; users edit documents then share links immediately.

Review prompts:

  1. Can EU user A see EU user B's edit instantly via replica?
  2. What happens if US primary fails during peak EU morning?
  3. Should document open-after-save read from primary?
  4. Cross-region write latency acceptable for all features?
  5. Failover automation vs manual for RPO > 0?

Expected findings: Stale reads break share flows; need read-your-writes or regional primary for write-heavy EU; document RPO in status page playbook.

20. Whiteboard Explanation​

90-second version:

"Primary-secondary means one leader takes all writes and ships an ordered log to followers. Followers can serve reads but may lag. Sync replication waits for follower ack before telling the client commit—that reduces data loss on failover but adds latency. Async is faster but you can lose the last seconds of writes when the primary dies. Failover promotes a replica, but you must fence the old primary to avoid split-brain. For interviews, always mention replication lag: users who write then read from a replica might not see their data. PostgreSQL and MySQL are classic examples; MongoDB uses the same idea with Raft election. It's the default because you avoid write conflicts—at the cost of a write bottleneck on one node."

21. Interview Questions​

  1. Explain primary-secondary replication.

    • Signals: Single writer, log shipping, followers apply in order.
  2. Sync vs async replication tradeoffs?

    • Signals: Latency vs durability/RPO; follower ack timing.
  3. What is replication lag?

    • Signals: Delay primary→follower; stale reads.
  4. How prevent split-brain?

    • Signals: Fencing, epoch, consensus, STONITH.
  5. Read-your-writes with replicas?

    • Signals: Route to primary, version check, session stickiness.
  6. RPO with async failover?

    • Signals: Unreplicated commits lost; quantify lag window.
  7. Can followers accept writes?

    • Signals: No in pure model; split-brain if yes without coordination.
  8. Statement vs row-based replication?

    • Signals: Determinism, trigger side effects, correctness.
  9. Why single leader?

    • Signals: Avoid write conflicts; total order.
  10. MongoDB read concern majority?

    • Signals: Wait for replicated oplog entries—not same as sync writes.
  11. Chain replication vs primary-fanout?

    • Signals: Chain reduces primary fanout; head failure sensitivity.
  12. When not use primary-secondary?

    • Signals: Global write latency, write scale beyond one node.
  13. Replication vs backup?

    • Signals: Replica mirrors live state including mistakes.
  14. Semi-sync replication purpose?

    • Signals: Balance latency and durability—one follower ack.

22. Interview Follow-Ups​

  1. Design failover for zero RPO?

    • Signals: Sync quorum, consensus (Paxos/Raft), accept write latency.
  2. User sees stale profile after update—debug?

    • Signals: Read replica routing, lag metrics, cache layers.
  3. Promote replica with 5min lag—impact?

    • Signals: Rollback appearance; client retries; incident comms.
  4. Compare Aurora to Postgres streaming.

    • Signals: Storage vs log shipping; shared storage replicas.
  5. Shard vs bigger primary?

    • Signals: Write ceiling, operational complexity, cross-shard queries.

22A. Production Low-Level Design (Interview + Game Day)​

Design sync quorum replication for payment ledger with fenced failover. Production LLD and Game Day Standard.

Control vs data plane​

PlaneRole
ControlPatroni/etcd leader election, fencing tokens
DataPrimary sync to 2/3 replicas before commit ack

Read policy: balance reads from primary or quorum; never stale for auth.

Step-by-step flow:

StepMechanismOps hook
1Write on primary only—
2–5Quorum durable before ack—
6RPO≈0 on commit—
  • Failover: STONITH old primary; fence token on storage
  • SLO: commit p99 < 50ms in-region

22B. Game Day Playbooks​

#DrillInjectDetectMitigateVerify
GD-1Split brainNetwork partitionTwo primariesFencing + quorumOne writer
GD-2Async replica readRoute read to laggingStale balancePrimary reads for moneyCorrect
GD-3Failover flappingUnstable etcdLeader thrashIncrease TTL + pre-voteStable
GD-4Replication lagReplica disk slowLag alarmThrottle writesLag bounded

GD-1 — Old primary writes after failover​

23. Strong Answer Example​

Question: "Design HA for a payment ledger with strict durability."

Answer this as a 45-minute design discussion using the PRACTICE framework. Below is how a strong student sounds aloud—sync quorum + fencing, not async DR by default.

Student interview script (PRACTICE)​

P — Problem framing​

"Payments need RPO≈0 on commit in-region. Sync replicate to majority before ack. Failover with fencing so old primary cannot write. Async cross-region only with explicit RPO acceptance."

R — Requirements​

TypeRequirement
DurabilityCommitted survives single failure
SafetyNo split-brain writes
PerfCommit p99 budget

A — Assumptions​

AssumptionWhy it matters
3 AZ sync quorum1 failure tolerant
Patroni + etcdLeader election

C — Capacity (sketch)​

5K tx/s; 2 sync replicas; commit ~2× RTT ~10ms

T — Topology (draw this)​

Step-by-step flow:

StepClaim
1Write primary
2Sync quorum ack
3Failover fence old

I — Invariants​

"No commit ack until quorum durable; no write without current fence token."

C — Constraints​

ConstraintResponse
Async DRDocument RPO>0
Stale readsNot for balances

E — Evolution​

Sync 3-node → chaos primary kill → prove balances.

Closing line​

"Sync quorum for RPO≈0; fence on failover; never read stale replicas for money."

Scoring signals​

StrongWeak
Names mechanism + failure path + metricsHand-wavy scaling; buzzwords only

Compact outline (2 min)​

See closing line in script above.

24. Weak Answer Example​

Question: "Design HA for a payment ledger with strict durability."

"Set up a read replica in another AZ. If primary fails, failover happens automatically. RDS handles it."

Why weak: No sync/quorum discussion, no split-brain/fencing, no read staleness on balance checks, assumes managed failover implies zero RPO.

25. Hands-On Exercise​

Exercise: Lag and failover simulator

  1. Deploy PostgreSQL primary + async replica (Docker or local).
  2. Generate continuous writes; measure lag with pg_stat_replication.
  3. Kill primary mid-write burst; promote replica; count missing rows vs primary WAL if recoverable.
  4. Implement application routing: post-write reads to primary for 2s.
  5. Repeat with synchronous_standby_names enabled; compare RPO and p99 write latency.

Success criteria: Quantify RPO difference async vs sync; document one stale-read user scenario.

26. Knowledge Check​

  1. Who accepts writes in primary-secondary? (Primary only.)
  2. Async replication RPO on failover? (>0 if unreplicated commits exist.)
  3. Split-brain cause? (Two primaries without fencing.)
  4. Purpose of replication log? (Ordered change stream for followers.)
  5. Read replica stale read cause? (Replication lag.)
  6. Sync replication latency cost? (Follower RTT + fsync.)

27. Flashcards​

#FrontBack
1Primary-secondaryOne writer; followers replicate log.
2Replication lagDelay before follower matches primary.
3Sync replicationCommit ack after follower durable.
4Async replicationCommit ack before follower catch-up.
5Split-brainTwo primaries—safety violation.
6FencingPrevent old primary accepting writes.
7RPOData loss window on failover.
8Read-your-writesClient sees own writes—needs routing.
9Hot standbyReplica ready for promotion.
10WAL/binlogOrdered log shipped to followers.
11Semi-syncWait for one follower ack.
12Replication ≠ backupReplicas copy errors and deletes.

28. Cheat Sheet​

PRIMARY-SECONDARY
- 1 writer, N followers, ordered log
- Writes: primary only
- Reads: primary or replicas (may lag)

SYNC vs ASYNC
- Sync: lower RPO, higher write latency
- Async: faster, stale reads + loss risk

FAILOVER
- Detect → elect → fence old primary
- Check lag before promote

CONSISTENCY
- Not automatic on replicas
- read-your-writes: route or version

SYSTEMS
- Postgres WAL, MySQL binlog, Mongo oplog

WHEN
- Read scale, conflict-free writes
- Not: global write latency, write bottleneck

30. References​

Primary sources​

  • Martin Kleppmann, Designing Data-Intensive Applications (O'Reilly), Chapter 5 — leader-based replication, sync/async, failover.
  • Corbett, J. C., et al. (2012). "Spanner: Google's Globally-Distributed Database." OSDI — evolution beyond single-leader with TrueTime (contrast case).

Production documentation​

Textbooks and papers​

Distinction​

Claim typeSource
Leader-follower modelKleppmann (DDIA Ch. 5)
Postgres/MySQL behaviorOfficial documentation
RPO on async failoverGeneral distributed systems principle
Aurora replication detailsAWS documentation—verify current