Database.Middle.How does replication work in databases?
Database replication is the process of copying and maintaining database objects and data from one database (the primary/master) to one or more others (the replicas/slaves) to ensure redundancy, high availability, scalability, and disaster recovery.
🧠 Core Idea
One database acts as the source of truth, and changes (inserts, updates, deletes) are replicated to other databases.
[ Primary DB ]
|
| changes streamed
v
[ Replica DBs ]
🧱 Types of Replication
1. Master-Slave (Primary-Replica)
Writes go to the master, reads can go to slaves.
Replicas are read-only copies.
Used for read scaling and backups.
2. Multi-Master
All nodes can read and write.
Requires conflict resolution.
Used in distributed systems needing high availability across regions.
3. Synchronous vs. Asynchronous
Type
Description
Pros
Cons
Synchronous
Master waits for replica to confirm write
Strong consistency
Slower, higher latency
Asynchronous
Master does not wait
Fast write performance
Possible data lag on failure
⚙️ Common Replication Mechanisms
✅ Statement-Based Replication
Replicates SQL statements (e.g., UPDATE users SET ...)
MySQL supports this (older approach)
✅ Row-Based Replication
Replicates actual row data changes (e.g., before/after values)
More accurate, avoids non-determinism
✅ Logical Replication
Sends logical changes (insert/update/delete) — not raw bytes
PostgreSQL: pglogical, wal2json
✅ Physical Replication
Copies data blocks (pages) directly from the primary (e.g., PostgreSQL WAL)