Search code examples
cassandradatastax-java-driver

How to set WRITE consistency explicitly with Datastax java driver?


With datastax java driver to connect to Cassandra, I wish to set explicitly WRITE consistency, but seems like we can set consistency level only for queries. Below is the sample code. How do i mention write consistency from driver lever ?

Cluster cluster = Cluster
                        .builder()
                        .addContactPoint(host)
                        .withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.ONE))
                        .withRetryPolicy(DefaultRetryPolicy.INSTANCE)
                        .withCredentials(userName,password)
                        .withLoadBalancingPolicy(
                                new TokenAwarePolicy(DCAwareRoundRobinPolicy.builder().build()))
                        .build();

Solution

  • We have completely different requirements for reads and writes (reads have really tight SLA regarding latency numbers and writes are not that important to us to finish fast).

    We decided to split sessions, we created two Cluster objects and out of those we created two sessions, one for read and one for write. When we are writing we are using writeSession and we write with CL QUORUM while when we read we use readSession which is tuned for latency requirements, with CL ONE, speculative executions and tight socket read timeout.

    Long story short, you can define session specific for all your writes and define consistency level on Cluster object. Be aware only that this will implicate some more connections from driver to Cassandra cluster.