Database replication
Database replication is the process of keeping copies of the same database on multiple database servers so that the data is available even if one server fails, and to improve performance and scalability.
Instead of having just one database:
Application
│
▼
Databaseyou have multiple copies:
Application
│
▼
Primary Database
/ \
▼ ▼
Replica 1 Replica 2All replicas contain the same data (or nearly the same, depending on the replication method).
Types of Database Replication
Master–Replica (Primary–Secondary)
This is the most common.
Write
Application ─────► Primary DB
│
Replicates Data
▼ ▼
Replica 1 Replica 2- All writes go to the primary.
- Replicas receive copies of the changes.
- Reads often go to replicas.
Multi-Primary (Master–Master)
This is the least common one.
Write Write
App A ─────► DB A ◄────────► DB B ◄───── App BBoth databases accept writes and replicate changes to each other.
Get Neha Goel’s stories in your inbox
Join Medium for free to get updates from this writer.
Challenge: If both databases update the same record at the same time, write conflicts must be resolved.
Synchronous vs Asynchronous Replication
Synchronous Replication
Primary
│
Write
│
▼
Replica
│
Acknowledgement
│
▼
Client gets successThe primary waits until the replica confirms it has received the data.
Advantages
- Data is always consistent.
- No data loss if the primary fails after the write is acknowledged.
Disadvantages
- Slower writes because the primary waits for replicas.
Asynchronous Replication
Primary
│
Write
│
Client gets success ✅
│
▼
Replica updates laterThe client receives success immediately, and the replica catches up afterwards.
Advantages
- Faster writes.
- Better performance.
Disadvantages
- If the primary fails before the replica receives the latest changes, some recent data may be lost.
That’s it for this topic. Happy Learning :)






