Why we need replication ?
- High available. We can tolerate one machine goes down or Internet interruption.
- Lower Latency. place data geographically close to user.
- Scalability. We can handle higher volume of reads by performing reading in replica.
How many kinds of replication way we can do?
- Single-leader replication.
- Multi-leader replication.
- Leaderless replication
What’s the difference between these replication ways ?
Single-Leader replication
Upside:
- Easy to understand.
- No conflict resolution.
- We can do trade-off between durability and availability. Asynchronous, semi-Asynchronous, synchronous. And others like chain replication in Microsoft Azure.
Multi-Leader replication
Upside:
- Tolerate datacenter outage
- Processed in the local datacenter and better perceived performance.
Downside:
- Have to consider conflict resolution.
Note:
- The best way is to avoid conflict write like all write for a particular method go through the same leader.
Leaderless replication
Upside:
- No need to replica data from leader to follower.
- No failover.
Downside:
- You may read stale data under Quorum
- Can not get reading your writes, monotonic reads, or consistent prefix reads.
How can we replicate write data from leader to follower ?
- statement replicate like SQL. Downside: nondeterministic
- Write ahead log. Recording bytes change of blocks. Downside: couple to the storage
- Bin log. Row based replication. data change of table row.
What should we guarantee if large replication lag happens ?
Read-After-write: a. read from leader b.save the timestamp of update and wait until that.
Monotonic reads: make sure each user always make their reads from the same replica
consistent prefix reads: all writes to the same partition
How can we do conflict resolution ?
- losing data is acceptable, Last write wins (LWW) .
- Each operation with one unique key
- Version vector and merge concurrent written values.