Replication in Distributed Systems: Techniques and Trade-offs

Bhagwati Malav
Hash#Include
Published in
10 min readJul 9, 2024

Replication is a cornerstone concept in distributed systems, ensuring data availability, fault tolerance, and performance optimization. This article provides an in-depth understanding of replication, covering various replication strategies, their challenges, and use cases. Let’s delve into the intricacies of replication in distributed systems.

Replication in distributed systems enhances availability, improves read scalability, and increases throughput.

We will discuss three possible replication models here.

1.1 Leader-Follower Replication

Leader-follower replication, also known as master-slave replication, is a widely used model where one node (leader) receives all write requests, and other nodes (followers) replicate the leader’s data. This section explores the details of leader-follower replication, including synchronous vs. asynchronous vs. semi-synchronous replication, handling node outages, failover mechanisms, election processes, and replication implementation methods.

Synchronous vs. Asynchronous vs. Semi-Synchronous Replication

Synchronous Replication

In synchronous replication, the leader waits for confirmation from followers before acknowledging a write request. This ensures strong consistency as all replicas have the same data before the write is confirmed. The major advantages are:

  • Strong Consistency: Ensures that all nodes have identical data.
  • Immediate Data Availability: Data is instantly available across all replicas.

However, the trade-offs include:

  • Higher Latency: Waiting for acknowledgments increases the time to respond to write requests.
  • Reduced Availability: If followers are slow or fail, the leader might not be able to confirm writes.

Asynchronous Replication

In asynchronous replication, the leader immediately acknowledges write requests without waiting for followers’ confirmations. This reduces latency but can result in temporary inconsistencies. The benefits are:

  • Lower Latency: Immediate acknowledgment improves response times.
  • Higher Availability: The leader can continue operating even if followers are slow or fail.

The downsides are:

  • Eventual Consistency: Followers might lag behind the leader, leading to temporary data inconsistencies.
  • Data Loss: In case of a leader failure before replication, recent writes might be lost.

Semi-Synchronous Replication

Semi-synchronous replication is a hybrid approach where the leader waits for confirmation from at least one follower before acknowledging a write request. This balances latency and consistency. Key advantages include:

  • Balanced Consistency and Latency: Ensures some level of consistency without significantly increasing latency.
  • Improved Fault Tolerance: Reduces the risk of data loss compared to asynchronous replication.

Challenges include:

  • Complexity: Managing the balance between synchronous and asynchronous behavior can be complex.
  • Potential Latency: Though reduced, waiting for one follower can still introduce some latency.

Node Outage Handling

Replica Failover

When a follower node fails, the system continues to function without it. The leader keeps track of the changes, and once the node recovers, it catches up with the leader’s data through log-based replication or other mechanisms.

  • Log-Based Replication: Uses logs to replay changes and synchronize the follower with the leader.
  • Snapshot Replication: Periodic snapshots of the data are sent to followers to ensure they are up-to-date.

Leader Failover

Leader failure is critical and requires an election process to choose a new leader. Nodes in the system regularly send heartbeats to the leader and vice versa. If a node doesn’t receive a heartbeat from the leader within a specified timeframe, it assumes the leader has failed. Common algorithms for leader election include:

  • Raft: A consensus algorithm that simplifies leader election and log replication, ensuring consistency. Raft is easier to understand and implement compared to Paxos, focusing on leader election and log replication in a clear manner.
  • Paxos: A more complex but widely used consensus algorithm ensuring fault tolerance in distributed systems. Paxos ensures that a single value is agreed upon by a majority of nodes, which is essential for electing a new leader.
  • Zab (ZooKeeper Atomic Broadcast): Used by Apache ZooKeeper to maintain distributed coordination. Zab ensures atomic broadcast, which is crucial for maintaining a consistent state across distributed nodes.

Challenges of Leader Failover

  • Complexity: Implementing reliable failover mechanisms and consensus algorithms can be complex.
  • Latency: The failover process might introduce a temporary delay as the system transitions to a new leader.
  • Split-Brain Scenarios: In case of network partitions, there is a risk of split-brain, where two nodes mistakenly assume the leader role. This can be mitigated through proper election protocols and quorum mechanisms.

Replication Implementation Methods

Statement-Based Replication

Replicates SQL statements executed on the leader to followers. It’s simple but can lead to inconsistencies due to non-deterministic functions.

  • Advantages: Easy to implement, low overhead.
  • Disadvantages: Non-deterministic statements (e.g., using random numbers, timestamps) can cause inconsistencies.

Write-Ahead Log (WAL) Replication

Replicates the leader’s log entries to followers before committing transactions. This ensures consistency but requires careful log management.

  • Advantages: Ensures a consistent order of operations, reliable.
  • Disadvantages: Can introduce latency due to log management overhead.

The main disadvantage of WAL replication is that the log operates at a very low level, detailing which bytes were changed in specific disk blocks. This tight coupling with the storage engine means that if the database’s storage format changes between versions, it usually becomes impossible to run different versions of the database software on the leader and the followers.

Row-Based Replication

Replicates individual row changes instead of SQL statements. It’s more precise and consistent than statement-based replication.

  • Advantages: Precise replication, consistency at the row level.
  • Disadvantages: Higher overhead due to the need to replicate each row change.

Because a logical log is independent of the storage engine’s internal workings, it can be more easily maintained for backward compatibility. This allows the leader and followers to run different versions of the database software, or even use different storage engines.

Trigger-Based Replication

Uses database triggers to capture changes and propagate them to followers. This method can introduce overhead but provides flexibility.

  • Advantages: Flexible, can replicate complex changes.
  • Disadvantages: Can introduce significant overhead, complex to implement.

Problems with Replication

Replication can introduce several consistency challenges, including:

Reading Your Own Writes

A user may not immediately see their recent writes due to asynchronous replication. Ensuring read-after-write consistency requires specific mechanisms like read-your-own-write guarantees. Possible solutions include:

  • Read From Leader: Ensure that reads follow recent writes by always reading from the leader.
  • Read from latest Updated Replica: The client can store the timestamp of its latest write. This enables the system to make sure that any replica handling reads for that user includes updates up to that timestamp. If a replica is not sufficiently updated, the read can either be managed by another replica or delayed until the current replica is up to date. This timestamp could be a logical one (such as a log sequence number that indicates the order of writes) or based on the system clock (which would require precise clock synchronization).

Monotonic Reads

Ensuring that subsequent reads reflect increasing timelines of updates. Without monotonic reads, users might see old data after seeing newer data. eg: making the same query twice, first to a follower with minimal lag and then to a follower with more lag.

One way to achieve monotonic reads is to ensure that each user always reads from the same replica (although different users can read from different replicas).

Consistent Prefix Reads

Ensuring that reads reflect a prefix of the write history. This prevents scenarios where reads might show a later write while missing earlier writes. Strategies include:

  • Causal Consistency: Track dependencies between writes to ensure reads reflect a consistent state.
  • Timestamps and Vector Clocks: Use timestamps or vector clocks to maintain the order of operations.

1.2 Multi-Leader Replication

Leader-based replication has a significant drawback: there’s only one leader, meaning all writes must be directed through it. If you lose connection to the leader, perhaps due to a network issue, you won’t be able to write to the database.

To address this limitation, the leader-based replication model can be extended to allow multiple nodes to accept writes. In this multi-leader configuration (also known as master-master or active-active replication), replication works the same way: each node that processes a write forwards the data change to all other nodes. In this setup, each leader also acts as a follower to the other leaders, allowing for greater flexibility and fault tolerance.

Use Cases

Multi Data Center

Multi-leader replication is ideal for geographically distributed data centers, ensuring low latency and high availability by allowing local writes. Benefits include:

  • Low Latency: Local writes reduce latency for users in different regions.
  • High Availability: Multiple leaders ensure that the system remains operational even if one leader fails.

Challenges include:

  • Data Consistency: Ensuring consistency across data centers is complex.
  • Conflict Resolution: Handling conflicting writes requires robust strategies.

Client with Offline Operation

Applications that need to operate offline can benefit from multi-leader replication by synchronizing changes once the connection is restored. Benefits include:

  • Offline Functionality: Users can continue working offline and sync changes later.
  • Data Availability: Ensures data availability even without a continuous network connection.

Challenges include:

  • Conflict Resolution: Managing conflicts between offline and online changes.
  • Data Synchronization: Efficiently synchronizing data when the connection is restored.

Collaborative Editing

Real-time collaborative applications, like document editors, can use multi-leader replication to handle concurrent updates from multiple users. Benefits include:

  • Real-Time Collaboration: Supports concurrent edits by multiple users.
  • High Availability: Multiple leaders ensure that the application remains available.

Challenges include:

  • Conflict Resolution: Merging concurrent changes without data loss or inconsistency.
  • Consistency: Maintaining a consistent view for all users.

Handling Write Conflicts

Synchronous vs. Asynchronous Conflict Detection

Synchronous Conflict Detection:

In synchronous conflict detection, write conflicts are detected at the time of the write operation. When a write is made to one leader, it immediately communicates with the other leaders to ensure consistency before confirming the write. This method has the advantage of catching conflicts early, preventing the propagation of conflicting data. However, it can introduce latency, as each write operation requires communication with multiple nodes.

  • Advantages: Immediate conflict resolution, ensures data consistency before confirmation.
  • Disadvantages: Increased latency due to the need for immediate inter-node communication.

Asynchronous Conflict Detection:

Asynchronous conflict detection, on the other hand, allows writes to be accepted immediately, with conflict detection and resolution happening later. Each leader processes writes independently and replicates the changes to other nodes in the background. Conflicts are detected when nodes synchronize, and resolution mechanisms must be in place to handle these conflicts after the fact.

  • Advantages: Lower latency for write operations, as immediate communication is not required.
  • Disadvantages: Potential for conflicts to propagate, requiring more complex conflict resolution strategies.

Conflict Avoidance

To minimize the chances of write conflicts in a multi-leader setup, several strategies can be employed:

  1. Partitioning Data: By partitioning data such that each leader is primarily responsible for a specific subset of the data, the likelihood of conflicting writes is reduced. This is effective in scenarios where data can be cleanly partitioned.
  2. Assigning Ownership: Similar to partitioning, this involves assigning ownership of specific data items to specific leaders. Writes are directed to the leader that owns the data, reducing the chances of conflict.
  3. Operational Transformation: Techniques like operational transformation (OT) are used to ensure that conflicting operations can be transformed and merged in a consistent manner. This is common in collaborative editing applications.
  4. Timestamps and Versioning: Using timestamps or version numbers for writes allows the system to track the order of operations, helping in identifying and resolving conflicts.

Converging Toward a Consistent State

Even with strategies to avoid conflicts, they are inevitable in a multi-leader replication system. The key is to ensure that the system can converge toward a consistent state after conflicts are detected. Common strategies for achieving this include:

  1. Last Write Wins (LWW): In this approach, the system resolves conflicts by accepting the write with the most recent timestamp. While simple, it can lead to data loss if important changes are overwritten.
  2. Conflict-Free Replicated Data Types (CRDTs): CRDTs are data structures designed to automatically resolve conflicts in a way that ensures eventual consistency. Examples include counters, sets, and maps that merge changes from different nodes deterministically.
  3. Application-Level Conflict Resolution: Sometimes, the best way to resolve conflicts is at the application level, where the logic can take into account the specific requirements and context of the application. This may involve merging changes, alerting users to conflicts, or applying domain-specific rules.
  4. Manual Intervention: In cases where automated conflict resolution is not feasible, conflicts can be flagged for manual intervention. Administrators or users can then review and resolve these conflicts based on the context.

1.3 Leaderless Replication

Leaderless replication distributes the responsibility of data management across all nodes, eliminating the single point of failure associated with leaders. This section covers its principles, benefits, and challenges.

Principles

  • Quorum-Based Reads and Writes: Operations are successful if a majority (quorum) of nodes agree on the result. Common quorum strategies include (N/2 + 1) for reads and writes, where N is the total number of nodes.
  • Read Quorum (R): Minimum number of nodes that must agree for a read to be successful.
  • Write Quorum (W): Minimum number of nodes that must agree for a write to be successful.
  • Gossip Protocols: Nodes communicate using gossip protocols to ensure data consistency and availability without a central coordinator. Gossip protocols help in: Data Dissemination: Spread data changes throughout the network. Membership Management: Keep track of active nodes and their status.

Benefits

  • Fault Tolerance: Leaderless replication provides high fault tolerance since no single node is critical for operations. If some nodes fail, the system can still function correctly.
  • Scalability: The system can easily scale by adding more nodes, distributing the load evenly. Each node can handle reads and writes independently, improving performance.

Challenges

Consistency: Achieving strong consistency is challenging and often requires trade-offs with availability (CAP theorem). Techniques to address consistency issues include:

. Consistent Hashing: Distribute data evenly across nodes to minimize hotspots.

. Vector Clocks: Track the causal relationships between operations to ensure consistency.

Conflict Resolution: Similar to multi-leader replication, handling conflicts in leaderless replication requires robust strategies. Approaches include:

  • Version Vectors: Use version vectors to detect and resolve conflicts.
  • Dynamo-Style Quorums: Employ techniques from Amazon’s DynamoDB to handle eventual consistency and resolve conflicts.

Conclusion

Replication is an essential aspect of distributed systems, providing the backbone for fault tolerance, data availability, and scalability. As we’ve explored, each replication strategy — leader-follower, multi-leader, and leaderless — comes with its own set of benefits and challenges. Selecting the appropriate strategy depends on your specific use case, consistency requirements, and desired trade-offs between availability and performance.

In summary:

  • Leader-Follower Replication is ideal for scenarios requiring strong consistency and simpler conflict resolution.
  • Multi-Leader Replication suits applications needing high availability and low latency across multiple regions.
  • Leaderless Replication is best for systems demanding high fault tolerance and scalability without a single point of failure.

Understanding and implementing these replication strategies effectively will enable you to build robust, high-performance distributed systems capable of meeting the demands of modern applications.

References :
1. Designing Data-Intensive Applications https://www.oreilly.com/library/view/designing-data-intensive-applications/9781491903063/

2. https://www.slideshare.net/junrao/kafka-replication-apachecon2013

3. http://yoshinorimatsunobu.blogspot.co.uk/2014/04/semi-synchronous-replication-at-facebook.html

4. http://messagepassing.blogspot.co.uk/2011/10/eventual-consistency-detecting.html

5. https://www.allthingsdistributed.com/files/amazon-dynamo-sosp2007.pdf

--

--