Database replication
How a database keeps live copies of itself on other machines: why bother, the choice between waiting for the copies or not, what a lagging copy does to reads, and what happens when the main one dies.
In 30 seconds
Replication keeps the same data on several database servers. One, the primary, takes the writes and sends each change to the others, the replicas.
The copies can share the reads, and one can take over if the primary fails.
The primary can wait for the copies before confirming a write (synchronous: safe, slower) or confirm first and copy afterwards (asynchronous: fast, but replicas run slightly behind and a crash can lose the newest writes).
Database replication, step by step
This is single-primary replication, the most common kind. Timings are illustrative. Multi-primary and leaderless designs exist, and real failover also needs a safe way to decide the primary is truly gone.
- Step 1: One copy is fragile. With a single database server, every read and every write lands on one machine. If it crashes the whole application is down until it returns, and if its disk is lost so is the data. It is also a ceiling: all traffic queues at the same place.
- Step 2: Copy every change. Replication keeps the same data on several machines. In the usual arrangement one server, the primary, accepts all writes and records each change in a numbered log. The others, the replicas, receive that log and apply the same changes in the same order, so they end up holding the same data.
- Step 3: Why: spread the reads. Every copy can answer reads, so 900 reads a second become 300 per machine, and adding replicas adds read capacity. It does not add write capacity: each of the 100 writes a second still goes through the primary and is then repeated on every replica. And if one machine fails, the others still hold the data.
- Step 4: Synchronous: wait for copies. In synchronous replication the primary does not tell the application a write succeeded until a replica confirms it has the change. A confirmed write therefore exists on more than one machine and survives the loss of the primary. The cost is waiting for the network on every write, and stalling if the replica cannot answer.
- Step 5: Asynchronous: answer first. In asynchronous replication the primary confirms the write as soon as it is safe on its own disk and sends the change to the replicas afterwards. Writes are fast and keep working when a replica is slow or offline. The price: for a moment only the primary has the newest change, and if it dies in that moment the change is gone.
- Step 6: Lag and stale reads. The gap between the primary and a replica is the replication lag. Usually it is milliseconds, but under load it can stretch to seconds. A read sent to a lagging replica returns old data: a user who has just bought the mug may still see stock 4. A common fix: send a user’s reads to the primary for a short while after they write.
- Step 7: Failover. When the primary fails, a replica is promoted to take its place and the application is pointed at it. The one with the newest log is chosen, here B. Anything the old primary had confirmed but not yet shipped is lost, which is the risk of asynchronous replication. The old primary must not rejoin as a second primary by mistake.
Try it yourself
Learn this first
Learn next
- Consistency models (strong vs eventual)What a read may return after a write when the data lives on three machines. Intermediate
- Change data captureHow other systems follow a database by reading its log of changes. Intermediate
- Disaster recovery (RPO and RTO)How much data you can lose, how long you can be down, and what each costs. Intermediate
Spotted a mistake on this page?