Study notes from DS201: Foundations of Apache Cassandra™ and DataStax Enterprise.
Consistency Level
Cassandra distributes and stores data across multiple nodes according to the Replication Factor (RF). The Consistency Level controls how many replica nodes must successfully acknowledge a read or write operation before the operation is considered complete and the client is notified.
The Consistency Level is used to adjust the tradeoff between availability and consistency. Setting a high consistency level guarantees data consistency but may reduce availability and performance. Conversely, setting a low consistency level improves availability and performance but may temporarily compromise data consistency.
Main Consistency Levels
| Consistency Level | Description |
|---|---|
ANY | Read or write operations are considered complete when they succeed on at least one replica (including the coordinator node itself). The lowest consistency level, maximizing availability. |
ONE | Read or write operations are considered complete when they succeed on at least one replica. Unlike ANY, it waits for a response from other replica nodes, not the coordinator node itself. |
TWO | Read or write operations are considered complete when they succeed on at least two replicas. |
THREE | Read or write operations are considered complete when they succeed on at least three replicas. |
QUORUM | Read or write operations are considered complete when they succeed on a majority of replicas (RF / 2 + 1). For example, with RF=3, responses from 2 replicas are required. |
ALL | Read or write operations are considered complete when they succeed on all replicas. The highest consistency level, minimizing availability. |
LOCAL_ONE | Read or write operations are considered complete when they succeed on any one replica in the local data center. Reduces latency by avoiding cross-data center communication. |
LOCAL_QUORUM | Read or write operations are considered complete when they succeed on a majority of replicas in the local data center. Useful in multi-data center environments for guaranteeing consistency within the local data center while avoiding cross-data center latency. |
EACH_QUORUM | Read or write operations are considered complete when they succeed on a majority of replicas in each data center. Useful in multi-data center environments for guaranteeing consistency within each data center. |
Read and Write Consistency
In Cassandra, the Consistency Level for reads and writes can be set independently. For example, to guarantee “strong consistency,” the relationship between RF and Consistency Level is important.
- Strong consistency: When the setting satisfies
Read Consistency Level + Write Consistency Level > RF, strong consistency is guaranteed, always reading the latest data. For example, with RF=3, writing withQUORUM(2) and reading withQUORUM(2) gives2 + 2 = 4 > 3, guaranteeing strong consistency.
Selecting the appropriate Consistency Level based on application requirements is critical for using Cassandra effectively.