Skip to main content

Command Palette

Search for a command to run...

Replication in System Design: Master-Slave vs. Multi-Master vs. Leaderless

Updated
8 min read
Replication in System Design: Master-Slave vs. Multi-Master vs. Leaderless

Today, we're diving deep into the critical concept of replication, exploring the nuances of Master-Slave, Multi-Master, and Leaderless architectures. Understanding these patterns is fundamental for building resilient, scalable, and highly available distributed systems.

At its core, replication is the process of maintaining multiple copies of data across different nodes or servers. This serves several vital purposes:

  • High Availability: If one node fails, other replicas can seamlessly take over, preventing downtime.

  • Durability: Data is protected against single points of failure.

  • Scalability: Read heavy workloads can be distributed across multiple replicas, improving performance.

  • Disaster Recovery: Replicas in different geographical locations can protect against regional outages.

Let's break down the different replication models.

1. Master-Slave Replication (or Primary-Replica)

This is perhaps the most traditional and straightforward replication model.

How it Works?

In a Master-Slave setup, one node is designated as the master (or primary), and all other nodes are slaves (or replicas).

  • Writes: All write operations (inserts, updates, deletes) are directed exclusively to the master.

  • Reads: Read operations can be served by either the master or any of the slaves.

  • Replication: The master asynchronously (or sometimes synchronously, though less common for performance reasons) propagates all data changes to its slaves. Slaves maintain a copy of the master's data.

Key Characteristics

  • Single Write Point: Simplifies conflict resolution as only one node accepts writes.

  • Read Scalability: Easy to scale reads by adding more slaves.

  • Data Consistency: Generally provides strong consistency for writes on the master. Slaves might experience eventual consistency due to replication lag.

Failure Scenarios & Recovery

  • Master Failure: This is the critical point. If the master fails, one of the slaves must be promoted to become the new master. This process, known as failover, can be manual or automatic. During failover, there will be a period of unavailability for writes.

  • Slave Failure: If a slave fails, it doesn't impact write availability, and read traffic can be rerouted to other slaves or the master.

Real World Examples

  • MySQL Replication: A classic example, widely used. Applications write to the master, and reads are distributed across replicas.

  • PostgreSQL Streaming Replication: Similar principles, offering robust primary-replica setups.

  • Redis with Sentinel/Cluster: Redis can operate in a master-slave configuration, with Redis Sentinel providing automatic failover.

Pros

  • Simplicity: Easy to understand and set up.

  • Strong Write Consistency: All writes go to one place.

  • Good for Read-Heavy Workloads: Can easily scale read throughput.

Cons

  • Single Point of Failure for Writes: Master failure impacts write availability.

  • Replication Lag: Slaves can fall behind the master, leading to stale reads (eventual compromising consistency).

  • Write Scalability Limit: Writes are bottlenecked by the master's capacity.

2. Multi-Master Replication

Multi-master replication attempts to address the single point of failure and write scalability limitations of master-slave.

How it Works?

In this model, multiple nodes are designated as masters, and they can all accept write operations.

  • Writes: Any master can accept write operations.

  • Reads: Reads can be served by any master.

  • Replication: Changes made on one master are replicated to all other masters.

Key Characteristics

  • No Single Write Point of Failure: Increased write availability.

  • Improved Write Scalability: Writes can be distributed across multiple masters.

  • Complex Conflict Resolution: This is the primary challenge. If the same data item is modified concurrently on different masters, how one can resolve the conflict?

Conflict Resolution Strategies:

  • Last Write Wins (LWW): The modification with the latest timestamp prevails. Simple but can lead to lost updates.

  • Merge: Attempt to intelligently merge conflicting changes. Can be complex to implement for arbitrary data structures.

  • Application Specific Logic: The application is responsible for detecting and resolving conflicts.

  • Multi-Version Concurrency Control (MVCC): Allows multiple versions of data to exist, and clients read a consistent snapshot.

Real World Examples

  • Galera Cluster (for MySQL/MariaDB): Provides synchronous multi-master replication, ensuring strong consistency and no data loss on node failure.

  • Cassandra (with specific configurations, though more accurately described as leaderless): While Cassandra is truly leaderless, a conceptual understanding of multi-master helps here where any node can accept writes. We'll dive deeper into Cassandra next.

  • Active-Active setups in some enterprise databases: Often involves complex synchronisation mechanisms.

Pros

  • High Write Availability: No single point of failure for writes.

  • Better Write Scalability: Distribute write load.

  • Better Read Scalability: Reads can be served by any master.

Cons

  • Conflict Resolution Complexity: The hardest part to get right. Can lead to data inconsistencies if not handled carefully.

  • Increased Network Overhead: More replication traffic between masters.

  • Potential for Deadlocks/Race Conditions: If not designed carefully.

3. Leaderless Replication (or Decentralised/Dynamo style)

Leaderless replication takes decentralisation to the extreme, with no designated master node. All nodes are peers and can accept reads and writes.

How it Works?

  • Writes: A client can send a write request to any node. That node then coordinates the replication of that write to a configurable number of other nodes.

  • Reads: A client can send a read request to any node. That node then queries a configurable number of other nodes to get the latest version of the data.

  • Quorums: This model heavily relies on quorums for ensuring data consistency and availability.

    • Write Quorum (W): The minimum number of replicas that must acknowledge a successful write before it's considered complete.

    • Read Quorum (R): The minimum number of replicas that must be queried for a read operation.

    • Number of Replicas (N): The total number of nodes where data is replicated.

    For strong consistency, the rule W + R > N must hold. If W + R <= N, eventual consistency is more likely, and data conflicts might arise.

Key Characteristics

  • High Availability & Durability: No single point of failure; data remains available even if multiple nodes fail.

  • Scalability: Highly scalable for both reads and writes by adding more nodes.

  • Tunable Consistency: Developers can choose their desired level of consistency by adjusting W, R, and N.

  • Version Vectors for Conflict Resolution: To handle concurrent writes, leaderless systems often use version vectors (or vector clocks). These allow systems to determine if one version of data is an ancestor of another, or if they are concurrent conflicts.

Failure Scenarios & Recovery

  • Node Failure: If a node goes down, other nodes can still serve requests using quorum rules. When the node comes back online, it uses anti-entropy protocols (like Merkel trees) to sync up with the latest data from its peers.

  • Hinted Handoff: If a replica is temporarily unavailable during a write, the coordinating node might temporarily store the write for the unavailable replica (a "hint") and deliver it when the replica comes back online.

Real World Examples

  • Apache Cassandra: The quintessential example of a leaderless, Dynamo-style database. It offers tunable consistency and high availability.

  • Amazon DynamoDB (internal architecture inspiration): Based on the original Amazon Dynamo paper, which popularised this replication model.

  • Riak: Not so prominent example of a NoSQL database implementing leaderless replication with consistent hashing and vector clocks.

Pros

  • Extremely High Availability: Tolerant to multiple node failures.

  • High Scalability: Scales horizontally for both reads and writes.

  • Tunable Consistency: Flexibility to prioritise consistency or availability/performance.

  • Simpler Operational Model (in some ways): No complex failover logic for a single master.

Cons

  • Eventual Consistency (often): Achieving strong consistency without compromising performance/availability requires careful quorum tuning (e.g., W + R > N).

  • Complex Conflict Resolution: Version vectors and application-level merging can be intricate.

  • Higher Latency for Strong Consistency: If W and R are set high for strong consistency, it increases latency.

  • Data Stale Reads: Possible if read quorum is not high enough or if replication lags.

Choosing the Right Replication Model

The "best" replication model depends entirely on the application's specific requirements, particularly concerning consistency, availability, and partition tolerance (the CAP theorem).

  • Master-Slave Config

    • Best for: Read-heavy workloads, applications requiring strong write consistency, simpler operational overhead, and where some write downtime during failover is acceptable.

    • Examples: Traditional relational databases, logging systems where order is critical.

  • Multi-Master Config

    • Best for: Geographically distributed applications requiring high write availability in multiple regions, or when write scalability is crucial and conflicts are rare or easily resolvable.

    • Examples: Global e-commerce platforms, collaborative editing tools (with careful conflict resolution).

  • Leaderless Config

    • Best for: Extremely high availability, high scalability, and applications that can tolerate eventual consistency or handle complex conflict resolution. Often chosen for distributed data stores where performance and uptime are paramount.

    • Examples: IoT data collection, large-scale analytics, social media feeds, session stores.

Conclusion

Replication is a cornerstone of robust system design. Each model "Master-Slave, Multi-Master, and Leaderless" offers distinct trade-offs in terms of consistency, availability, and operational complexity. By understanding these differences and the underlying mechanisms (like quorums and conflict resolution), one can make informed decisions to build systems that meet ones specific reliability and performance goals.

Which replication strategy have you found most effective in your projects? Share your experiences in the comments below!

System Design

Part 8 of 8

Transition from coder to designer. This series bridges code and high-scale products, breaking down the ‘why’ behind the ‘how' from load balancing fundamentals to the complexities of microservices and distributed databases.

Start from the beginning

System Design - Beyond the Code

Why System Design is the Definitive Mindset Shift In the early stages of a software engineering career, the world is defined by the IDE. Success is measured by the elegance of a function, the efficiency of an algorithm, or the passing of a local unit...