What is database replication, and what are its types?
MediumConceptNoSQL, Replication & Sharding
Answer
Replication keeps copies of the same data on several database servers, called replicas. It improves availability, because another replica takes over when one fails, and read performance, because reads are spread across replicas.
- Master-slave (primary-replica): all writes go to the primary, which streams its changes to read-only replicas. It is simple and common, as in MySQL and PostgreSQL replication.
- Multi-master: several nodes accept writes, which improves write availability but needs conflict resolution when two nodes change the same row.
- Synchronous replication waits for replicas to confirm before a commit succeeds (no data loss, higher latency); asynchronous replication does not wait (faster, but replicas lag and recent writes can be lost if the primary fails).
- Replication is not a backup: an accidental
DELETEis replicated too.
Example:
App --writes--> Primary --change log--> Replica 1, Replica 2
App --reads---> Replica 1 or Replica 2
With asynchronous replication, a user who updates their profile and immediately reads from a lagging replica may see the old value. Sending that user's reads to the primary for a short time avoids this.
More in NoSQL, Replication & Sharding
| # | Problem | Platform | Difficulty | Open | 1st1st Visit | 2nd2nd Visit | RevRevise |
|---|---|---|---|---|---|---|---|
| 1 | What is the difference between SQL and NoSQL databases? | Concept | Easy | Read the answer to What is the difference between SQL and NoSQL databases? | |||
| 2 | What are the different types of NoSQL databases? | Concept | Easy | Read the answer to What are the different types of NoSQL databases? | |||
| 3 | What is the CAP theorem? | Concept | Medium | Read the answer to What is the CAP theorem? | |||
| 4 | What is database replication, and what are its types? | Concept | Medium | Read the answer to What is database replication, and what are its types? | |||
| 5 | What is sharding, and how is it different from partitioning? | Concept | Medium | Read the answer to What is sharding, and how is it different from partitioning? |