Load/Bearing
Fundamentals · 06 of 22

Database replication

Copies of your database give you more read capacity and a standby for when the primary dies. They cost consistency.

Replication keeps the same data on several machines. It serves two goals that are easy to mix up: scaling reads (more machines can answer queries) and availability (if one machine dies, another has the data). It does nothing for write throughput, because every copy has to apply every write.

APPreads + writesPRIMARYall writesREPLICA 1readsREPLICA 2reads
Leader–follower replication: writes go to the primary, which streams its changes to read-only replicas.

Leader–follower

This is the most common setup (PostgreSQL streaming replication, MySQL replication, and managed services such as RDS and Cloud SQL). One node, the primary or leader, accepts writes. It records every change in a log, and the replicas (followers) replay that log. The app sends writes to the primary and spreads reads across the replicas.

Synchronous or asynchronous

ModeThe primary confirms a write after…Trade-off
Asynchronousit's written locallyFast. A replica can be seconds behind, and failover can lose the last writes.
Synchronousevery replica has itNo data loss, but one slow replica slows every write.
Semi-synchronousat least one replica has itThe usual compromise: one up-to-date standby, the rest async.

Replication lag and its bugs

With async replication, a replica is always a little behind, usually milliseconds and sometimes seconds under load. That causes some classic bugs:

Failover

When the primary dies, a replica has to be promoted:

  1. Detect the failure, usually after several missed heartbeats. Too eager and you fail over on a network blip; too slow and the outage lasts longer.
  2. Choose the most up-to-date replica.
  3. Promote it and point writers at it, through DNS, a proxy or service discovery.
  4. Fence the old primary so that if it comes back, it can't accept writes too. Two nodes that both think they're primary is called split brain, and it corrupts data.

Two numbers describe how good your failover is. RTO (recovery time objective) is how long writes are unavailable. RPO (recovery point objective) is how much recent data you can lose. Async replication has an RPO above zero.

Beyond one leader

Replicas or a cache?

Both reduce load on the primary. A cache is cheaper per read served and faster, but it only helps with repeated reads and needs invalidation. Replicas handle any query, including ad-hoc and analytical ones, and double as failover standbys, but each one costs as much as a database. Many systems use both.

In the game: each replica adds 1,500 reads/s. In Four Nines, the primary dies at 38 s. With a replica, you lose 2 seconds of writes while it's promoted. Without one, every database request fails for 12 seconds. In Stale Reads, 15% of reads are users checking their own edits: sending every read to any replica shows them old data, sending everything to the primary overloads it, and routing only post-write reads to the primary fixes both.

Interview checklist

Practise it

Level 5: Stale Reads

Put this into practice against live traffic. Your progress in the game is saved in your browser.

Play level 5

Further reading