Replication?

Keeping a copy of the same data on multiple machines. Each node that stores the copy is called Replica.
Advantages of Replication
  1. Lower latency for users: Put a replica near the user(in India). He donot need to wait for reading from Virginia.
  2. Availability: If one node crashes, another replica still serves data.
  3. Read throughput: Writes go to the leader; many followers share the read load. Ten replicas can serve roughly ten times the reads of one node.
  4. Parallel processing: Leader handles writes. Followers serve read queries (reports, product listing, “my orders”).

Types of Replication

1. single leader, Multiple followers
2. multi leader
3. leaderless

1. Master/Slave / Leader/Follower

Clients write only to the leader. The leader writes locally, then ships a replication log (change stream) to followers.
Followers apply those changes in the same order as the leader. Clients usually read from any follower.
Examples: Built in Postgres, MySQL, MongoDB, Kafka partition replicas

Master Slave DB

2. Master/Master / Active/Active / Leader/Leader

Two or more leader nodes. A client can write to any one of them. The other leaders act as followers for those writes and pull the change stream. Useful for multi-datacenter writes; the cost is write conflicts (two leaders update the same row).

High Availability (Node Down)

Any node in the system can go down(leader or follower):
- crash
- Planned maintenance (rebooting a machine to install a ker nel security patch).

a. Follower Restarted

Follower reads last transcation(LSN) from replication log
Follower connects to master and reqeusts all data that happens after LSN

b. Leader Down & Restarted (FAILOVER)

Leader Down:
  ELECTION: 1 of the followers needs to be promoted to be the new leader, leader is chosen by a majority of the remaining replicas. best candidate for leadership is usually the replica with the most up-to-date data.
  All clients need to be reconfigured to send their writes to the new leader, and the other followers need to start consuming data changes from the new leader.
SPLIT BRAIN:
  It could happen that two nodes both believe that they are the leader, and both accept writes, data is likely to be lost or corrupted. Some systems shutdown 1 leader

Implementation of Replication Logs

1. Statement-based replication

Leader logs every DB request (INSERT, UPDATE, or DELETE) that it executes to its disk in a file
And sends this log file to its followers which it applies to its DB
Issues:
1. Any statement that calls a nondeterministic function(eg: NOW(), RAND()) will generate different results on master and replica
  Fix: leader can replace any nondeterministic function calls with a fixed value
2. if statement depends on some condition. Eg: (UPDATE … WHERE some condition) will break since condition might not have been written on replica
3. Many other edge cases
Other replication methods are now generally preferred now

2. Write-ahead log (WAL)

Write to disk 1st then DB. Then send log to replicas
Suppose node crashes while writing, so when it recovers it can check Log file and actual Disk content so see what's completed and not.