← All Concepts
Databases

Database Replication

Maintains copies of data on multiple servers for high availability, fault tolerance, and read scalability.

**Database replication** copies data from a primary (master) to one or more replicas (slaves). **Replication types:** - **Single-leader (master-slave)**: One primary handles writes, replicas serve reads. Most common. - **Multi-leader**: Multiple nodes accept writes. Good for multi-region. Complex conflict resolution. - **Leaderless**: Any node accepts reads/writes. Quorum-based consistency. (Cassandra, DynamoDB) **Sync vs Async replication:** - **Synchronous**: Primary waits for replica ack. Strong consistency, higher latency. - **Asynchronous**: Primary doesn't wait. Lower latency, risk of data loss. - **Semi-synchronous**: Wait for at least one replica ack. **Replication lag:** Time between write to primary and availability on replica. Can cause read-your-write issues. **Failover:** If primary fails, a replica is promoted. Automatic (cluster manager) or manual.

Common Use Cases

  • Read scaling: offload reads to replicas
  • High availability: failover to replica on primary failure
  • Disaster recovery: replica in different region
  • Analytics: run queries on replica without affecting primary

Advantages

  • +High availability through automatic failover
  • +Read scaling by distributing reads across replicas
  • +Geographic distribution for lower latency
  • +Data durability through redundancy

Disadvantages

  • -Replication lag can cause stale reads
  • -Write scaling is not improved (still single writer in leader-based)
  • -Failover can be complex and may lose recent writes
  • -Multi-leader conflicts are hard to resolve

Related Concepts

CAP TheoremDatabase Shardingeventual consistency