Data in a Distributed World
In a distributed system, data is often replicated across multiple nodes to ensure high availability and fault tolerance. But this replication introduces a new problem: how do you ensure that all the nodes agree on the state of the data, especially when failures occur?
This is the problem of consensus. A consensus algorithm is a process for a group of nodes to agree on a single value or state. Quorum is a core concept used in many consensus protocols.
A quorum is the minimum number of nodes in a cluster that must participate in an operation for it to be considered successful. It's a form of voting. Instead of requiring all nodes to agree (which would make the system unavailable if even one node failed), you only require a majority.
This concept is most famously used in leaderless replication systems (like Cassandra or DynamoDB) to provide tunable consistency.
Let's define the variables:
N = The number of replicas for a piece of data.W = The write quorum. The minimum number of replicas that must acknowledge a write for it to be considered successful.R = The read quorum. The minimum number of replicas that must respond to a read request.By tuning W and R, you can control the trade-off between consistency and performance.
W + R > NThis is the key to achieving strong consistency in a leaderless system. If the sum of your write quorum and your read quorum is greater than the total number of replicas, you are guaranteed that your read set will have at least one node in common with your write set. This ensures that any read will always see the most recent successful write.
Example:
N = 3 (We have 3 replicas for our data).W = 2 and R = 2.W + R = 4, which is greater than N = 3. This configuration guarantees strong consistency.How it works:
W=2 replicas have confirmed they have stored the data.R=2 replicas.You can adjust W and R to optimize for read performance, write performance, or consistency.
W=1, R=N): A write is acknowledged as soon as one replica receives it. This is very fast. However, a read must wait for a response from all replicas to be sure it's getting the latest data. This is good for write-heavy, read-light workloads.W=N, R=1): A write must be confirmed by all replicas before it's considered successful. This is slow. But a read can be served from any single replica, as it's guaranteed to be consistent. This is good for read-heavy, write-light workloads.W=2, R=2 for N=3): This offers a good balance between read and write performance while still guaranteeing strong consistency.Quorum is a mechanism, but it's part of a broader class of consensus algorithms. These are formal protocols for achieving agreement in a distributed system, even in the presence of failures.
You are not expected to implement these algorithms in an interview, but knowing what they are and what problem they solve is important.
Paxos is one of the first and most famous consensus algorithms. It's known for being mathematically correct but also notoriously difficult to understand and implement in practice.
Raft is a more modern consensus algorithm that was designed to be easier to understand and implement than Paxos. It's functionally equivalent to Paxos but has a simpler state machine.
How Raft Works (High-Level):
Where are they used? Consensus algorithms like Raft are used in many modern distributed systems to manage critical state, such as:
In a system design interview, when you talk about needing to manage state reliably across multiple nodes (e.g., for leader election or managing shard metadata), mentioning that you would use a proven consensus algorithm via a tool like ZooKeeper or etcd is a very strong signal. It shows you understand the problem of consensus and know that you shouldn't try to solve it yourself.