Core Concepts

Data Replication

How copying database changes to another machine helps you handle stale reads and recover when the primary fails.

Free~15 min

Replication copies database changes to other machines. Those copies can serve reads or help the database recover after a failure. The design has to specify what the other machines have received or saved when the application reports a successful write.

Sync vs async

In the single-writer setup here, the primary accepts changes. A replica receives a record of those changes and applies them to its own copy. If the primary crashes right after confirming a nickname update, can the next primary still show the new nickname?

  • Synchronous replication makes the primary wait for a configured acknowledgment from a replica before confirming the write. Waiting for a durable copy adds network and storage delay, and the primary may have to block or reject writes if too few replicas respond.
  • Asynchronous replication lets the primary confirm its local commit before a replica acknowledges the change. If the primary fails and a replica that lacks the change takes over, the saved nickname can revert.

An acknowledgment can mean the data reached the replica, reached durable storage, or was applied to the copy readers use. For PostgreSQL, configured synchronous standbys can acknowledge after a durable log flush or, with remote_apply, after the change becomes visible there. The default streaming setup uses asynchronous replication. At failover, promote a standby that retained the required writes. An acknowledgment from one standby tells us nothing about what a different standby has received.

Redis can wait for replication on demand. After SET, issue WAIT 1 100 on the same connection to wait up to 100 ms for one replica to acknowledge. Check the returned count. This improves the chance an acknowledged write survives but still leaves possible loss during failover. Placement also matters. A nearby standby can survive a machine failure with a smaller latency cost. A standby in another region can protect against a wider failure at the cost of another long network trip when the primary waits for it.

Sources: PostgreSQL synchronous commit . Redis WAIT

Primary and replicas

The common topology is one primary (accepts writes) and one or more replicas (receive a stream of changes and apply them). Clients write to the primary. Reads can go to the primary or to a replica.

Replicas buy two things:

  1. Durability / availability - a second (or third) copy survives a primary disk failure.
  2. Read capacity - fan reads across replicas so the primary is not the only place queries land.

All writes still consume the primary's CPU and storage, including its write-ahead log. Replicas can free primary resources by taking reads. Once writes alone saturate the primary, extra copies leave that bottleneck in place.

Replication lag and stale reads

Replication lag is the time between a primary commit and the replica applying it. Lag can grow when the replica is busy or the network slows down.

If the customer refreshes after changing their nickname, a replica that has yet to apply the update still returns the old value. That is a stale read.

If a user must see their own change immediately, send their read to a current primary that retained the write, or wait for a replica to apply the specific version. A lag alarm can take a slow replica out of the read pool. For a chosen PostgreSQL standby, remote_apply lets the commit wait for visibility there before reporting success. Another standby can still be behind.

Failover

When the primary dies or becomes unreachable, something has to promote a replica and point clients at it. That process is failover.

Step through detect → fence → promote → redirect. Watch the writer count: it dips to zero during the fence, then returns to one on the new primary - never two.

Fencing means removing the old primary's ability to accept writes before handing that authority to a replacement. Otherwise a broken network link can make the old primary look dead while some clients can still reach it. Both primaries can then accept conflicting writes, a failure called split-brain.

A controller can fence the old primary by powering it off or by requiring it to use an ownership lease that expires when it cannot renew. Leader Election and Consensus Algorithms (both forthcoming) explain how the system chooses a replacement and coordinates ownership.

Warning
Promote without fencing is how you get two primaries - a flaky network makes the old primary look dead to the orchestrator while it is still serving clients. Fence it before the new primary opens for writes.

Failover tools can automate detection, promotion, and routing updates. Check fencing separately. A client on the same side of a partition as the old primary can still write to that primary in some Redis Sentinel failure cases. Those writes may be discarded when the primary rejoins. Redis Sentinel failover

Multi-primary (briefly)

Multi-primary setups let several nodes accept writes. That can let two regions write locally. If both edit the same customer's nickname before learning about the other edit, the system needs a rule to choose a value or merge the changes.

Choose a multi-writer setup when local writes in several regions are important and you can explain how every conflicting update is resolved. An application with records that must change together may find that easier to enforce with one writer at a time.

When replication alone is not enough

Replication gives the system another copy and can relieve read load. To spread writes across machines, shard the dataset so each shard owns a different set of rows. A shard can have its own primary and replicas.

The replication factor is the number of copies. With two current copies in independent failure domains, losing either still leaves a copy. That assumes the surviving copy has the changes you need. Recovering reads and continuing writes also depend on the system's promotion, routing, and acknowledgment rules. The P2P crawler uses two copies of its work assignment to cover takeover. It keeps fetched content in object storage.

See also

  • Database Sharding - splits the write path across machines; replication keeps each shard durable.
  • Scalability - replicas scale reads; horizontal write split is the next ladder rung.
  • Consistent Hashing - how many systems place data (and replicas) on a ring so join/leave moves a bounded slice.
  • Write-Ahead Log - the primary's WAL is what replicas stream; it is also the write-path ceiling.
  • Latency vs Throughput - sync replication buys durability with write latency; async does the reverse.
  • CAP Theorem - ask which reads and writes remain available during a network partition, and who can still serve or acknowledge them.
  • Availability - uptime NFRs and N-1 headroom that make multi-AZ replicas useful.
  • Consistency Models - lag is how eventual consistency shows up on a replica read.
  • Quorum (forthcoming) - how read and write acknowledgment rules interact with the number of copies.
  • Leader Election (forthcoming) - how a cluster picks the next primary without split-brain.
  • Consensus Algorithms (forthcoming) - Raft/Paxos as the machinery behind leases and single-writer guarantees.
  • Failover - the operational playbook around promote, DNS, and client reconnect.

Used in

  • Batched Inference Service - the result store illustrates what can disappear when Redis promotes a replica that has yet to receive a result. Waiting for an extra acknowledgment reduces that risk.
  • Web Crawler (P2P) - each node replicates its primary URL range to its ring successor (RF=2); on crash, gossip detects the failure and the successor promotes the hot replica - calibrated to the crash-to-takeover window, not to permanent durability (content already lives in S3).

Done reading? Mark it so it sticks in your dashboard.

Discussion