System design fundamentals learning path Level 2 This level · 4 of 20
Medium system-design fundamentals

How does database replication work, and what is replica lag?

The 30 second answer

Replication keeps copies of the data on several nodes for availability and read scaling. Most systems use one leader that takes writes and followers that copy its log; multi-leader and leaderless setups accept writes in several places but must resolve conflicts. With asynchronous replication a follower can be behind, which is replica lag, so a user may not see their own write if the next read goes to a replica.

Could you say this in an interview?

The three topologies.

  • Single leader (primary and standbys). All writes go to the leader, which ships its write-ahead log to followers. PostgreSQL streaming replication works this way, and a standby that also serves read-only queries is called a hot standby. Simple and no write conflicts; the leader is the write bottleneck and failover must be handled.
  • Multi-leader. Several nodes accept writes, typically one per region, and replicate to each other. Lower write latency across regions, but concurrent writes to the same row conflict and need a rule such as last-writer-wins or a merge.
  • Leaderless. Any replica accepts reads and writes, as in Amazon's Dynamo design. With N replicas, a write waits for W of them and a read asks R of them; the Dynamo paper notes that choosing R + W > N gives a quorum-like system, and that its users typically used N = 3.

Synchronous versus asynchronous.

  • Synchronous: the leader waits for a standby to confirm before reporting commit. No committed data is lost if the leader dies, but each write waits for the slowest required standby. In PostgreSQL this is synchronous_standby_names, and synchronous_commit can even be lowered per transaction.
  • Asynchronous: the leader commits locally and ships later. Fast, but a failover can lose the last writes, and followers lag. Amazon RDS read replicas are asynchronous.

Replica lag and what to do about it. Lag is usually small but can grow under load or long-running queries. Symptoms: a user saves a profile and the next page, served from a replica, shows the old one. Fixes:

  • Read-your-writes routing: read from the leader for a short time after a user writes, or for that user's own data.
  • Pass a position (for example a PostgreSQL LSN) and only read from a replica that has replayed past it.
  • Monitor lag; PostgreSQL exposes it by comparing replay and received positions on the standby.

Common mistakes interviewers listen for.

  • Calling replicas a backup. A bad DELETE replicates too; you still need backups and point-in-time recovery.
  • Sending all reads to replicas without thinking about read-after-write.
  • Failover without fencing, so the old leader keeps accepting writes (split-brain); see the leader election question below.
Sources (5)
  1. PostgreSQL: Comparison of different high availability, load balancing and replication solutionspostgresql.org
  2. PostgreSQL: Log-shipping standby servers and synchronous replicationpostgresql.org
  3. PostgreSQL: Replication configuration (synchronous_standby_names)postgresql.org
  4. AWS: Working with DB instance read replicasdocs.aws.amazon.com
  5. DeCandia and others: Dynamo, Amazon's highly available key-value store (SOSP 2007)allthingsdistributed.com
Previous
Next