Database replication explained: primary-replica, multi-primary and replication lag
Replication keeps copies of your database on more than one server. It's used for availability (survive a server failure), read scaling and disaster recovery.
Primary-replica (leader-follower)
- One primary accepts writes; replicas copy its changes and serve reads.
- Simple and widely supported.
- If the primary fails, a replica is promoted, either automatically or manually.
Multi-primary (multi-leader)
- Several nodes accept writes, often in different regions.
- Lower write latency for distributed users.
- The hard part is conflict resolution when two nodes change the same data at the same time.
Synchronous vs asynchronous
- Synchronous: the primary waits for at least one replica to confirm before acknowledging a write. Safer, but slower.
- Asynchronous: the primary acknowledges immediately and replicas catch up. Faster, but recent writes can be lost if the primary fails.
Replication lag and its surprises. With async replicas, a user can save something and then not see it if the next read goes to a replica that hasn't caught up. Common fixes:
- Read your own writes from the primary for a short period
- Route reads that need freshness to the primary
- Monitor lag and remove lagging replicas from rotation
Replication isn't backup. A bad DELETE replicates instantly. You still need point-in-time backups.
Have you been bitten by replication lag in production? How does your team test failover?
Comments · 3
The "save something and then not see it" scenario is a good example for beginners. It looks like a bug in the app but it's really just a replica that hasn't caught up yet.
Multi-primary sounds appealing for latency, but conflict resolution is where the real cost shows up. Many teams are better served by one primary and a clear plan for failover and lag-sensitive reads.
Remember that replication is not a backup. A bad delete replicates too.