how to set cassandra read and write consistency
Solution 1
How would I set different consistency levels for read and write?
If you just want to change consistency level for your current session, use CONSISTENCY
.
If you want to change the consistency level programamtically, use the cassandra driver for your client language.
Since the Consistency Level can be set per-statement, you can either set it on every statement, or use PreparedStatements. If you're using the Java driver, you can configure a global Consistency level for BOTH reads and writes (but not only reads and only writes).
Where is that default [consistency level] set? and is it for read or write?
If you want to set up a different consistency level for reads than writes, you'll have to do it on a per-statement basis. Use QueryOptions().setConsistencyLevel
to set a global consistency level (for the Java driver). It is for BOTH reads and writes.
To set the consistency level for your current session, use the CONSISTENCY command from the cassandra shell (CQLSH).
For example:
Set CONSISTENCY to force the majority of the nodes to respond:
CONSISTENCY QUORUM
To see your current consistency level, just run CONSISTENCY;
from the shell:
ty@cqlsh> consistency;
Current consistency level is ONE.
For programming client applications, set the consistency level using an appropriate driver.
To set a per-insert consistency level using the Java driver, for example, call QueryBuilder.insertInto
with setConsistencyLevel
.
For example:
PreparedStatement pstmt = session.prepare(
"INSERT INTO product (sku, description) VALUES (?, ?)");
pstmt.setConsistencyLevel(ConsistencyLevel.QUORUM);
To set a global consistency level for reads AND writes using the Java driver, do something like:
QueryOptions qo = new QueryOptions().setConsistencyLevel(ConsistencyLevel.ALL);
- Since the Java driver only executes CQL statements, which can be either reads or writes to Cassandra, it is not possible to globally configure the Consistency Level for only reads or only writes. To do so, since the Consistency Level can be set per-statement, you can either set it on every statement, or use PreparedStatements. More info in the Queries and Results document.
More resources on read/write consistency levels
-
How consistency level is configured
- The different consistency levels are also explained here ^
- Cassandra's Java Driver documentation
- See the "Consistency levels" section in configuring consistency with the Java Driver
Other resources on consistency: You may also want to look into your replica placement strategy and replication factor (which are other forms of consistency). I included links below for good measure:
- Background on data replication in Cassandra
- How to update replica strategy and replication factor using CREATE KEYSPACE
- How consistency affects performance
Solution 2
By default, the Consistency Level is ONE for all R/W Operations.
Setting CL is done on a per query (read or upsert) basis by adding the CONSISTENCY XXXX
syntax on the query as seen here:
https://cassandra.apache.org/doc/latest/cql/dml.html#insert
and
https://cassandra.apache.org/doc/4.0/tools/cqlsh.html
bhomass
Interested in machine learning, neural network, Spark architecture, streaming, and NoSql.
Updated on April 28, 2020Comments
-
bhomass about 4 years
I can not find the documentation for this. I know there is a consistency command in cqlsh, but there is no distinction between read and write consistency. How would I set different consistency levels for read and write?
Furthermore, there is a mention of a "default" consistency level. Where is that default set? and is it for read or write?
-
Chris Gerlt over 8 yearscheckout the documentation for the driver you are using like java driver: docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/…
-
JSelser almost 7 yearsso...where you able to find how its done? google is not helping
-
-
bhomass over 8 years3 responses. I don't see the answer in any of them. my question is how to set separate read and write consistencies. So I believe the right answer is not to use "using" clause any more, but rather use the consistency command, but my question is how to set different level for read vs. write?
-
bhomass over 8 yearsyou just repeated my comment, while not answering the question. I am bewildered this is such a mystery. It can't be that you are forced to set the same consistency level for both read and write. That would make no sense.
-
mahendra singh over 8 yearsUsing cqlsh you can not set . But in java program you can set . In java driver program you find Write conf and Read Conf and also can make Two different cluster and session one for reading and one for writing.
-
bhomass over 8 yearsOk, next time I come across a Datastax guy, I will definitely post this question to him or her. It definitely sounds like a hold in the product spec.
-
Rdesmond almost 8 years"ONE to LOCAL_ONE". Not sure I agree with that; default consistency without setting anything from cqlsh is ONE (output "Current consistency level is ONE").
-
adutra almost 8 yearsYou are right, the python driver now defaults to LOCAL_ONE, but cqlsh still defaults to ONE: github.com/apache/cassandra/blob/trunk/bin/cqlsh.py#L757
-
user1401472 over 6 yearsBoth these links are no longer accessible! Can you please update this?