# Consistency Models of NoSQL Databases
## 5 NoSQL Consistency Implementations
### Cassandra  
Cassandra scales up by distributing data across a set of nodes, designated as a cluster. Each node is capable of answering client requests. When a node is working on a client request, it becomes the _coordinator_ for that request. It will be responsible for asking to other nodes for the requested data and answering back to the application. Cassandra partitions data across the cluster by hashing the _row key_. Each node on the ring stores a subset of hashes, in such a way that \"the largest hash value wraps around the smallest hash value\" [26]. Because of the randomness of the hash functions, data tends to be evenly distributed across the ring. Replication is the strategy Cassandra uses to achieve a high-available system. Two concepts describe a replication configuration in Cassandra, _replication strategy_ and _replication factor_[34]. The _Replication strategy_ determines which nodes replicas are placed. The _replication factor_ determines how many different nodes have the same data. If the replication factor is R, the node that is responsible for that specific key range copies the data it owns to the next R-1 neighbors, clockwise as in Figure 4.  
Cassandra was initially designed to be eventually consistent, high-available and low-latency. However, its consistency can be tuned to match the client's requirements. The following configuration constants describe some of the different write consistency levels [35]:  
* _ALL_. Data is written on all replica nodes in the cluster before the coordinator node acknowledges the client. (Strong Consistency, high latency)
* _QUORIUM_. Data is written on a given number of replica nodes in the cluster before the coordinator node acknowledges the client. This number is called the quorum. (Eventual Consistency, low latency)
* _LOCAL_QUORIUM_. Data is written on a quorum of replica nodes in the same data center as the coordinator node before this last one acknowledges the client. (Eventual Consistency, low latency)
* _ONE_. Data is written in at least one replica node. (Eventual Consistency, low latency)
* _LOCAL_ONE_. Data is written in at least one replica node in the same data center as the coordinator node. (Eventual Consistency, low latency)  
Analogous to the write consistency levels, the following configuration constants describe some of the read consistency levels [35]:  
Figure 4: Cassandra Ring. * _ALL_. The coordinator node returns the requested data to the client only after all replicas have responded. (Strong consistency, less availability)
* _QUORUM_. The coordinator node returns the requested data to the client only after a quorum of replicas has responded. (Eventual consistency, high-availability)
* _LOCAL QUORUM_. The coordinator node returns the requested data to the client only after a quorum of replicas has responded from the same datacenter as the coordinator. (Eventual consistency, high-availability)
* _ONE_. The coordinator node returns the requested data to the client from the closest replica node. (Eventual consistency, high availability)
* _LOCAL ONE_. The coordinator node returns the requested data to the client from the closest replica node in the local datacenter. (Eventual consistency, high availability)  
On the default configuration, Cassandra updates all the replica nodes that have been queried in some reading request to reflect the latest value. This routine is called _Read Repair_ and, because a single read triggers it, it puts little stress on the cluster [36]. For reading consistency levels of ONE, the coordinator only asks to one node for the information. Therefore, it cannot _Read Repair_ when only one version of the data object is being considered. However, Cassandra has a configuration called _Read Repair Chance_. For instance, given a _Read Repair Chance_ of 0.1 and a _Replication Factor_ of 3, 10% of the reads will trigger a _Read Repair_ and hit the three replicas so that the update data is propagated to all three replicas. Some confusion may raise about the difference between _Replication Factor_ and _Write Consistency Level_.