How to set consistency level in a Cassandra query?

Member

by natalia , in category: SQL , a year ago

How to set consistency level in a Cassandra query?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by austyn.beer , a year ago

@natalia 

In Cassandra, you can specify the consistency level for a query using the ConsistencyLevel enum in the org.apache.cassandra.consistency package. This enum defines the following values:

  • ANY: The query will return a result as soon as it has received a response from any node in the cluster.
  • ONE: The query will return a result as soon as it has received a response from at least one node in the cluster.
  • TWO: The query will return a result as soon as it has received a response from at least two nodes in the cluster.
  • THREE: The query will return a result as soon as it has received a response from at least three nodes in the cluster.
  • QUORUM: The query will return a result as soon as it has received a response from a majority of nodes in the cluster (rounded up).
  • ALL: The query will return a result only after it has received a response from all nodes in the cluster.
  • LOCAL_QUORUM: The query will return a result as soon as it has received a response from a majority of nodes in the local datacenter (rounded up).
  • EACH_QUORUM: The query will return a result as soon as it has received a response from a majority of nodes in each datacenter (rounded up).
  • SERIAL: The query will return a result only after it has received a response from all nodes in the cluster, and the data has been written to the commit log and the memtable on all nodes.
  • LOCAL_SERIAL: The query will return a result only after it has received a response from all nodes in the local datacenter, and the data has been written to the commit log and the memtable on all nodes in the local datacenter.
  • LOCAL_ONE: The query will return a result as soon as it has received a response from at least one node in the local datacenter.


To set the consistency level for a query, you can use the withConsistency method of the Statement class. For example:

1
2
3
4
5
6
import org.apache.cassandra.consistency.ConsistencyLevel;

// ...

Statement stmt = new SimpleStatement("SELECT * FROM my_table WHERE key = 'abc'");
 stmt.setConsistencyLevel(ConsistencyLevel.QUORUM);


Alternatively, you can also set the consistency level for a query by using the setConsistencyLevel method of the QueryOptions class, which you can pass as an argument to the execute method of a Session object. For example:

1
2
3
4
5
6
7
8
import org.apache.cassandra.consistency.ConsistencyLevel;
import org.apache.cassandra.query.QueryOptions;

// ...

Statement stmt = new SimpleStatement("SELECT * FROM my_table WHERE key = 'abc'");
 QueryOptions options = QueryOptions.forConsistencyLevel(ConsistencyLevel.QUORUM);
 session.execute(stmt, options);


It's important to note that the consistency level you choose for a query can affect the performance and availability of your Cassandra cluster. For example, using a high consistency level like ALL or SERIAL can result in slower performance, as the query will not return until it has received a response from all nodes in the cluster