Skip to content

Database Replication

Replication means keeping copies of your data on multiple database servers. It’s essential for availability and read scaling.


flowchart LR
App["📱 Application"] --> Primary["Primary (Master)<br/>Writes + Reads"]
Primary -->|"Sync Replication"| Replica1["Replica 1<br/>Read-only"]
Primary -->|"Async Replication"| Replica2["Replica 2<br/>Read-only"]
Primary -->|"Async Replication"| Replica3["Replica 3<br/>Read-only<br/>🌍 Different region"]
Users["👥 Users"] --> App
style App fill:#7c3aed,color:#fff
style Primary fill:#059669,color:#fff
style Replica1 fill:#4f46e5,color:#fff
style Replica2 fill:#6366f1,color:#fff
style Replica3 fill:#8b5cf6,color:#fff

AspectSynchronousAsynchronous
Write completes when…All replicas confirmPrimary alone confirms
Data loss on failure?❌ None✅ Some (not-yet-replicated writes)
Write latencyHigher (wait for replicas)Lower (primary only)
Read consistencyAlways up-to-datePotentially stale replicas
Best forFinancial transactionsSocial media, analytics

TopologyHow It WorksTrade-off
Single PrimaryOne leader writes, many replicas readMost common. Leader is SPOF
Multi-PrimaryMultiple nodes accept writesComplex conflict resolution
Synchronous Multi-PrimaryWrite to all before confirmingHigh latency, very safe
Cascading ReplicaReplica replicates to another replicaReduces load on primary

ReasonExplanation
High availabilityIf primary fails, a replica can become the new primary
Read scalingDistribute read queries across replicas (most apps are read-heavy)
Disaster recoveryReplicas in different regions survive regional outages
AnalyticsRun heavy queries on replicas without impacting production

  • Sync replication is safer but slower (every write waits for replicas).
  • Async replication is faster but risks losing recent writes if the primary crashes.
  • Replicas add operational cost — more servers to manage.

  • Replication = making copies of your database so you don’t lose data if one server dies.
  • Primary handles writes. Replicas handle reads (most apps read way more than they write).
  • Sync is safe but slow. Async is fast but might lose a few writes during a crash.