AlgoInsight

Q&A

Data replication: Master-Slave

By AlgoInsight Team2026-06-07databases

Overview

As an application grows, a single database struggles to handle all traffic. Master-Slave replication scales reads by maintaining copies of the data: writes go to the master, which streams its changes to read-only replicas. Most apps are read-heavy, so distributing reads across replicas dramatically increases capacity, while the master remains the single source of truth for writes.

graph TD
    APP[Application] -->|writes| M[(Master / Primary)]
    APP -->|reads| S1[(Replica 1)]
    APP -->|reads| S2[(Replica 2)]
    M -->|replicate changes| S1
    M -->|replicate changes| S2
    style M fill:#1e1b4b,color:#fff

How It Works

  1. All writes (INSERT/UPDATE/DELETE) go to the master.
  2. The master records changes in a replication log (e.g. MySQL's binlog, PostgreSQL's WAL).
  3. Replicas continuously pull/receive that log and apply the same changes, staying in sync.
  4. The application routes reads to replicas and writes to the master.

Benefits

The Central Trade-off: Replication Lag

Replication is usually asynchronous - the master doesn't wait for replicas to confirm. So a replica may be slightly behind ("lag"), and a read right after a write might return stale data:

1. User updates profile  → written to MASTER
2. Page reloads, reads   → from a REPLICA that hasn't applied the change yet
3. User sees their OLD profile (eventual consistency)

This read-your-writes problem is the classic gotcha. Mitigations:

Sync vs Async vs Semi-sync

Mode Behavior Trade-off
Asynchronous Master doesn't wait for replicas Fast writes, possible lag/data loss on failover
Synchronous Master waits for all replicas Strong consistency, slow writes
Semi-synchronous Waits for ≥1 replica Balance of safety and speed

Limitations

Related Patterns

Why This Matters in Interviews

System-design interviews use this constantly. Cover: writes → master, reads → replicas; why (most workloads are read-heavy); the master streams its change log to replicas; and the central trade-off of replication lag / eventual consistency with the read-your-writes problem and its fixes (read-from-master for fresh data, semi-sync). Crucially, state that replication scales reads, not writes - write scaling needs sharding - and that the master is a single point of failure requiring failover. This depth distinguishes a strong answer.

Common Pitfalls

References

More answers