...

/

Consistency Levels: ALL, EACH_QUORUM, QUORUM, and LOCAL_QUORUM

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

ALL

Every replica in the cluster

  • Strongest consistency (maximum accuracy)
  • Minimum availability

EACH_QUORUM

Majority (51%) replicas in each datacenter. Heavy operation. Rarely used

  • In multi-datacenter deployments, EACH ensures same consistency across datacenters
  • 2nd strongest consistency
  • Weak availability

QUORUM

Majority (51%) of replicas across all datacenters

((RF1+RF2+..RFn) /2) +1

  • Strong consistency
  • Balance between availability & accuracy

LOCAL_QUORUM

Closest 51% replicas in the same datacenter (RF/2)+1

  • In multi-datacenter deployments, LOCAL avoids inter-datacenter communication
  • Increasing availability while sacrificing accuracy

THREE

3 replicas closest to coordinator

  • Weak consistency
  • Strong availability
  • Used when consistency requirements are not stringent

TWO

2 replicas closest to coordinator

  • Weak consistency
  • Strong availability
  • Minimum fault-tolerance

ONE

1 replica closest to coordinator

  • Weakest consistency
  • Maximum availability
  • Sacrifices accuracy for availability

LOCAL_ONE

Closest replica to coordinator in the same datacenter

  • In multi-datacenter deployments, when ONE is the desirable consistency and cross-datacenter communication is to be avoided
  • Weakest consistency
  • Maximum availability


ANY

For write operations only. No acknowledgement required. Hint is stored by coordinator & write succeeds even if all replicas are down

  • Maximum availability
  • Minimum accuracy

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.

Press + to interact
nodetool status command displaying cluster comprising three nodes & VideoCourses keyspace has RF 2
nodetool status command displaying cluster comprising three nodes & VideoCourses keyspace has RF 2

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.

Press + to interact
Default consistency level is ONE. "nodetool getendpoints" displays nodes that will save 'Cassandra' partition
Default consistency level is ONE. "nodetool getendpoints" displays nodes that will save 'Cassandra' partition

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.

Press + to interact
nodetool status command displaying cluster comprising a single node & VideoCourses keyspace is created with RF 2
nodetool status command displaying cluster comprising a single node & VideoCourses keyspace is created with RF 2

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.

Press + to interact
Retrieving the consistency level & “nodetool getendpoints" displays node responsible for saving the partition
Retrieving the consistency level & “nodetool getendpoints" displays node responsible for saving the partition

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 ...