Database replication copies data from a primary database to one or more replicas in real time. Read replicas handle read queries to reduce load on the primary. Replicas also provide failover — if the primary goes down, a replica takes over with minimal downtime.

How Database Replication Works

A typical setup: one primary handles all writes, three read replicas handle read queries. Your web app sends writes to the primary and reads to replicas. If the primary fails, a replica is promoted to primary within seconds. PostgreSQL streaming replication and MySQL replication handle this natively.

Key Concepts

  • Primary-Replica — One primary accepts writes; replicas receive a copy and serve reads
  • Synchronous vs Async — Synchronous waits for replica confirmation (safer, slower). Asynchronous is faster but risks data loss on primary failure
  • Failover — Automatic promotion of a replica to primary when the primary becomes unavailable

Frequently Asked Questions

Replication vs sharding?

Replication copies all data to multiple servers (for read scaling and availability). Sharding splits data across servers (for write scaling and storage). Use replication first — it's simpler.