Say we always read from r nodes and write to w nodes in a n node system then as long as the following inequality holds the writes to the system will be durable and the read from the system will return the latest value:
R + W > N
For our three node system, where N=3 we can choose R=1 and W=3. With these values we'll write to all the nodes in the system and if any of the nodes is down then the write request will fail. Reading from any of the nodes in the system will naturally return the latest value since the write was recorded at all the nodes. As a corollary of the above inequality we can further say:
Generally, N is set to an odd number but N can also be even. For instance, if N=4, then one configuration that can work is R=3 and W=2 to make R+W (3+2=5) greater than N=4. We can reason that if a write is recorded to two nodes and both of them go offline then any read request will fail since the read request must be responded to by at least three nodes. Reading and writing in this manner is called as quorum reads and quorum writes respectively. The reads and writes are generally sent to all the nodes but the R and W values determine how many nodes do we wait to receive response from before considering the write or the read as successful. Reads will fail when fewer than R nodes are available and writes will fail when fewer than W nodes are available.
It is interesting to consider what happens if we flip the inequality as follows:
R + W <= N
In the above scenario, the system’s availability improves since now the reads and writes can go through even if N/2 (rounded down) nodes are offline. However, consequently, the probability of stale reads also goes up.
It might seem that using quorum reads and writes guarantees the latest data from a system, however, that may not hold true in a few edge cases depending on the implementation details of the system. Some of these cases are discussed below:
In practice, systems using leaderless replication such as Dynamo, advertise eventual consistency and don’t guarantee that reads return the latest values. Generally, with quorum reads and writes the read guarantees discussed previously such as reading one’s writes, monotonic reads and consistent prefix reads aren’t promised. Stronger guarantees require transactions or consensus, as we shall learn later.