Consistency Levels: ALL, EACH_QUORUM, QUORUM, and LOCAL_QUORUM
Learn the various Consistency levels featured in Apache Cassandra, and how they impact data consistency, availability, and performance.
The main Apache Cassandra consistency levels are listed in the table below, ordered by strongest to weakest consistency.
CL | Replica(s) contacted for read/write | Availability vs. Data Accuracy |
| Every replica in the cluster |
|
| Majority (51%) replicas in each datacenter. Heavy operation. Rarely used |
|
| Majority (51%) of replicas across all datacenters ((RF1+RF2+..RFn) /2) +1 |
|
| Closest 51% replicas in the same datacenter (RF/2)+1 |
|
| 3 replicas closest to coordinator |
|
| 2 replicas closest to coordinator |
|
| 1 replica closest to coordinator |
|
| Closest replica to coordinator in the same datacenter |
|
| For write operations only. No acknowledgement required. Hint is stored by coordinator & write succeeds even if all replicas are down |
|
We will demonstrate each consistency level with a read and write query. To display how consistency level and the cluster node count affect query success, the INSERT and SELECT statements are executed on two configurations - a three node Cassandra cluster and a one node Cassandra cluster. Records are written to and read from courses_by_category table, Cassandra partition in the VideoCourses keyspace with a RF of 2.
Three node Cassandra cluster
The following screenshot demonstrates the cluster configuration with the nodetool status command displaying one datacenter titled datacenter1 with three nodes (127.0.0.1, 127.0.0.2, 127.0.0.3). The VideoCourses keyspace is created with a replication factor RF of 2 and is selected for subsequent INSERT and SELECT queries.
The following screenshot demonstrates that the CONSISTENCY; statement displays the current consistency level. The default consistency level is ONE. The nodetool getendpoints command with arguments videocourses, courses_by_category, and Cassandra displays the two replica nodes responsible for saving the Cassandra partition of the courses_by_category table in the videocourses keyspace.
Single node Cassandra cluster
The following screenshot demonstrates the cluster configuration with the nodetool status command displaying one datacenter titled datacenter1 with a single node (127.0.0.1). The same VideoCourses keyspace is created with a replication factor RF of 2 and is selected for subsequent INSERT and SELECT queries. Notice the warning stating the replication factor for the keyspace is higher than the node count.
The screenshot shows the CONSISTENCY; statement displaying the current level (default is ONE). The nodetool getendpoints command identifies the replica node for the Cassandra partition of the courses_by_category table in the videocourses keyspace. If a node is added to the cluster, Cassandra would automatically replicate the partition on it to satisfy the replication factor of 2.
The ALL consistency level
The consistency level ALL is the strictest consistency level and is used when consistency is more critical than availability or performance, such as in financial transactions. The key features of the ALL consistency level are listed below:
Write acknowledgment awaited from all replica nodes.
Slowest write.
Data read from all replica nodes.
Absolute consistency - strongest consistency.
Slowest read.
Not advisable as one replica failure adversely affects availability.
Provides the strongest consistency while sacrificing availability and performance.
For a multi-datacenter deployment with two datacenters, DC1 with RF 3 and DC2 with RF 3, the coordinator awaits acknowledgment from all six replicas. Thus, the operation fails even if one of the six replicas is down.
The following statement sets the consistency for all read and write queries in the current session to ALL
CONSISTENCY ALL;
The screenshots below illustrate the execution of the INSERT and SELECT statements with ALL consistency on a three-node and single-node cluster, respectively. The replication factor for the keyspace is set to two. For demonstration clarity, ALL is set as a value in the title column of the INSERT statement.
On a three-node cluster with all nodes up and running, both the ...