4 min to complete
The Consistency Level (CL) determines how many replicas in a cluster must acknowledge a read or write operation before it is considered successful.
Some of the most common Consistency Levels used are:
- ANY – A write must be written to at least one replica in the cluster. A read waits for a response from at least one replica. It provides the highest availability with the lowest consistency.
- QUORUM – When a majority of the replicas respond, the request is honored. If RF=3, then 2 replicas respond. QUORUM can be calculated using the formula (n/2 +1) where n is the Replication Factor.
- ONE – If one replica responds; the request is honored.
- LOCAL_ONE – At least one replica in the local data center responds.
- LOCAL_QUORUM – A quorum of replicas in the local datacenter responds.
- EACH_QUORUM – (unsupported for reads) – A quorum of replicas in ALL datacenters must be written to.
- ALL – A write must be written to all replicas in the cluster, a read waits for a response from all replicas. Provides the lowest availability with the highest consistency.
Regardless of the Consistency Level, a write is always sent to all replicas, as set by the Replication Factor. Consistency Level controls when a client acknowledges an operation, not how many replicas are actually updated.
During a write operation, the coordinator communicates with the replicas (the number of which depends on the Replication Factor). The write is successful when the specified number of replicas confirm the write.
In the above diagram, the double arrows indicate the write operation request going into the coordinator from the client and the acknowledgment being returned. Since the Consistency Level is one, the coordinator, V, must only wait for the write to be sent to and responded by a single node in the cluster which is W.
Since RF=3, our partition 1 is also written to nodes X and Z, but the coordinator does not need to wait for a response from them to confirm a successful write operation. In practice, acknowledgments from nodes X and Z can arrive at the coordinator at a later time, after the coordinator acknowledges the client.
When our Consistency Level is set to QUORUM, the coordinator must wait for a majority of nodes to acknowledge the write before it is considered successful. Since our Replication Factor is 3, we must wait for 2 acknowledgments (the third acknowledgment does not need to be returned):
During a read operation, the coordinator communicates with just enough replicas to guarantee that the required Consistency Level is met. Data is then returned to the client.
The default CL value when using the CQL Shell is ONE.
The Consistency Level is tunable per operation in CQL. This is known as tunable consistency. Sometimes response latency is more important, making it necessary to adjust settings on a per-query or operation level to override keyspace or even data center-wide consistency settings. In other words, the Consistency Level setting allows you to choose a point in the consistency vs. latency tradeoff.