Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
How database replication works: leader-follower, multi-leader and leaderless setups, sync vs async replication, replication lag and failover.
Quick answer: Replication means keeping copies of the same data on several database servers. In the most common setup, one server (the leader or primary) accepts all writes and sends a stream of changes to the others (followers or replicas), which apply them in the same order. Replicas provide high availability (if the leader fails, a replica takes over), read scaling (queries can be spread across replicas) and lower latency for distant users. The main complication is replication lag: replicas are usually slightly behind the leader, so a read from a replica can return stale data.
Replication does not scale writes. Every write still goes through the leader and must be applied on every replica. For write scaling you need sharding.
This is the standard design for PostgreSQL, MySQL, SQL Server, MongoDB and many others.
| Method | What is sent | Notes |
|---|---|---|
| Statement-based | The SQL statement itself | Breaks on non-deterministic functions such as NOW() or RANDOM() |
| Physical (WAL shipping) | The low-level write-ahead log | Exact byte-for-byte copy; replicas must run a compatible version |
| Logical (row-based) | Row-level changes: "row 42 now has these values" | Works across versions and for feeding other systems (change data capture) |
The PostgreSQL high availability documentation compares these approaches.
When does the leader tell the client "committed"?
| Asynchronous | Synchronous | |
|---|---|---|
| Leader confirms | As soon as it has written locally | After a replica confirms it has the data |
| Write latency | Low | Higher (adds a network round trip) |
| If the leader dies | Recently committed writes may be lost | No committed data is lost |
| If a replica is slow or down | No effect on writes | Writes stall |
A common compromise is semi-synchronous: require confirmation from at least one replica (or a majority), and let the rest follow asynchronously. This is durable without letting one slow replica stop all writes.
With asynchronous replication, followers trail the leader by milliseconds, or by much more under load. This creates visible anomalies:
Common fixes:
This behaviour is a form of eventual consistency.
When the leader fails, a follower must take over. The steps:
Each step has pitfalls:
Leader election is typically delegated to a consensus system. See how consensus algorithms like Raft work.
Several nodes accept writes and replicate to each other. This is used for multi-region deployments and for apps that work offline and sync later.
The catch is write conflicts: two leaders change the same row at the same time. Strategies for resolving them:
Multi-leader setups are powerful and notoriously tricky. Avoid them unless you need them.
In systems inspired by Amazon's Dynamo, such as Cassandra and Riak, any replica accepts writes. The client (or a coordinator) sends each write to all N replicas and waits for W acknowledgements. Reads query several replicas and wait for R responses.
If W + R > N, the sets of nodes written to and read from must overlap, so at least one response contains the latest value. For example, with N = 3, choosing W = 2 and R = 2 tolerates one node being down.
Replicas that missed writes are repaired in the background and during reads. These systems stay available during failures, in exchange for weaker consistency guarantees. The trade-off is the subject of the CAP theorem.
A replica is not a backup. If someone runs DELETE FROM users without a WHERE clause, that mistake replicates to every copy within milliseconds. You need both:
A copy of the database that receives changes from the primary and serves read-only queries, taking load off the primary.
The delay between a write being committed on the leader and being applied on a replica. During that time, the replica returns older data.
Replication keeps full copies of the same data on several servers. Sharding splits the data so each server holds a different part. They are often combined: each shard is replicated.
Asynchronous replication adds almost no delay. Synchronous replication adds the time to reach a replica and get an acknowledgement.
Replication keeps a system running when machines fail and lets reads spread across many servers. The price is living with copies that are not always identical. Decide how much data you can afford to lose and how stale a read can be, and those two answers will choose your replication mode for you.