Replication can serve the purpose of reducing latency by placing data geographically close to users, increasing availability by not relying on a single node, and increasing read throughput by having multiple nodes with the same data.
The most common form of replication is single leader replication, where one node accepts all the writes and passes them on to “follower” nodes. Reads can then be read by the leader or by any followers.
Requiring a leader to pass all of it’s writes on to follower nodes can lead to replication lag. There are several problems associated with this, and they also have solutions:
A user could write to the leader and then read from a follower before the leader has written to that follower, making it appear as if they haven’t written anything. This is solved by read-after-write consistency.
If a user first reads data from a fresh replica, then from a stale replica, it may appear that data is going back in time. Monotonic reads fix this issue.
If two separate clients are writing to different partitions in the database database (imagine two people having a conversation and one observing it), it is possible for the observer to read from the partitions out of order and experience the conversation out of sync. Fixing this requires consistent prefix reads guarantee that if the database always applies writes in the same order, any reads will occur in the same order.
Certain use cases require multi-leader replication:
Multi data center operation
Clients with offline operation (example: calendars on devices, local db with leader and other db with leader)
Real-time collaboration.
This brings a challenge of dealing with write conflicts, which requires the system to have some form of conflict resolution.
The third strategy for replication is leaderless replication (also called Dynamo-style data stores), in which clients can write to several nodes, and also read from several nodes in parallel in order to detect and correct nodes with stale data. These types of data stores use “quorums” to decide whether data is up to date. If W + R > N, where W is the number of nodes required for the write to be successful, R is the number of nodes we query from, and N is the total number of replicas, we expect to get an up to date values form the data store. You can configure these values in Dynamo-style systems to lead to higher availability and lower latency, but it may come at the cost of more stale values. The biggest issue with leaderless replication is dealing with concurrent writes.