Beginner → Intermediate20 min read· Topic 2.4

Database replication

Single-leader, multi-leader, leaderless replication, sync vs async, replication lag

🔄Key Takeaways

  • 1
    Single-leader (primary-replica) is the most common pattern — one writer, many readers
  • 2
    Multi-leader enables writes at multiple locations but creates conflict resolution complexity
  • 3
    Leaderless (Dynamo-style) uses quorum reads/writes for tunable consistency
  • 4
    Sync replication = strong consistency but slower; Async = fast but risk of data loss on failover

Why Replicate?

Replication copies data across multiple nodes for three reasons: (1) High availability — if one node dies, others serve traffic, (2) Performance — read replicas handle read-heavy workloads, (3) Durability — data survives hardware failures.

The fundamental challenge: how do you keep multiple copies of data in sync, especially when the network is unreliable?

Replication Topologies

One primary (leader) accepts all writes. Changes are replicated to follower replicas. Reads go to any replica.

Most common pattern. Used by PostgreSQL, MySQL, MongoDB (default), Redis.

Async replication: primary doesn't wait for replicas to confirm → fast writes but followers may lag (replication lag = stale reads).

Sync replication: primary waits for at least one replica to confirm → slower writes but guarantees at least two copies.

Quorum Math
Quorum condition: W + R > N

Example configurations (N=3):
  W=2, R=2 → Consistent reads (overlap = 1 node)
  W=3, R=1 → Fast reads, slower writes
  W=1, R=3 → Fast writes, slower reads
  W=1, R=1 → Fastest but NO consistency guarantee

For strong consistency: W + R > N
For high write availability: W = 1 (any single node can accept writes)
For high read availability: R = 1

Trade-off: lower W or R = higher availability/speed but weaker consistency
⚠️Replication Lag Pitfalls
With async replication, a user might write to the primary and immediately read from a replica that hasn't received the update yet. Solutions: read-your-own-writes (route reads to primary for recently updated data), monotonic reads (always read from the same replica), or causal consistency.

Advantages

  • High availability through redundancy
  • Read scalability via replicas
  • Durability — data survives node failures

Disadvantages

  • Replication lag causes stale reads
  • Conflict resolution in multi-leader is complex
  • Sync replication reduces write throughput

🧪 Test Your Understanding

Knowledge Check1/1

In a quorum system with N=5, W=3, R=3, is consistency guaranteed?