Database Replication

Replication keeps copies of the same data on more than one database node. The immediate reason is durability — a disk failure on one machine shouldn't mean the data is gone — but in practice replication is used just as often to scale reads and to keep serving traffic through a node failure without an outage.

Leader and followers

The common setup has one leader node that accepts all writes, and one or more follower nodes that receive a continuous stream of the leader's changes and apply them locally. Reads can be served from any follower, spreading read traffic across the whole cluster, while writes always go through the single leader — which keeps write ordering simple at the cost of a ceiling on write throughput.

A leader replicating writes to two follower nodes, which serve reads

Synchronous vs. asynchronous

The leader can wait for a follower to confirm it received a write before telling the client the write succeeded (synchronous), or it can acknowledge the client immediately and let the write reach followers in the background (asynchronous). This is a direct trade-off between durability and latency: synchronous replication means a write is never acknowledged unless it is already safe on a second node, but every write now waits on a network round trip; asynchronous replication is fast, but a leader crash before a write reaches its followers loses that write permanently.

Replication mode trade-offs
ModeWrite latencyData loss on leader failure
SynchronousHigher (waits for follower ack)None, for acknowledged writes
AsynchronousLower (no wait)Possible: unreplicated writes are lost
Semi-synchronousModerate (waits for one of several)Unlikely, not impossible

Replication lag

Asynchronous followers are always slightly behind the leader; how far behind is the replication lag. Under normal load it might be milliseconds, but a slow follower, a network hiccup, or a burst of writes can widen that gap. Lag matters because a client that writes through the leader and immediately reads from a lagging follower can see stale data — or even see their own write disappear and reappear. Systems that care about this either read from the leader right after a write ("read-your-writes" consistency), route a given user's reads to the same follower for a while, or simply accept the staleness where it's harmless, like a public dashboard.

What happens when the leader fails?

A follower must be promoted to take over as the new leader. Automated failover systems monitor the leader's health and, on losing it, pick the follower with the least lag, promote it, and repoint the other followers and the application at it. The tricky part is avoiding "split brain" — two nodes both believing they're the leader after a network partition — which is why production failover almost always goes through a consensus mechanism or an external coordinator rather than a simple timeout.