Search code examples
cassandradatastax-enterprisedatastax-startup

Cassandra Read/Write CONSISTENCY Level in NetworkTopologyStrategy


I have setup cassandra in 2 data centers with 4 nodes each with replication factor of 2.

Consistency level is ONE (set by default)

I was facing consistency issue when trying to read data at consistency level of ONE. As read in DataStax documentation, Consistency level (read + write) should be greater than replication factor.

I decided to change the write consistency level to TWO and read consistency level as ONE which resolves the inconsistency problem in single data center. But in case of multiple data center, the problem would be resolved by consistency level as LOCAL_QUORUM.

How would i achieve that write should be (LOCAL_QUORUM + TWO) so that i should write to the local data center and also on 2 nodes.


Solution

  • Just write using LOCAL_QUORUM in the datacenter you want. If you have a replication factor of 2 in each of your datacenter then the data you are writing in the "local" datacenter will eventually be replicated in the "other" datacenter (but you have no guaranty of when).

    LOCAL_QUORUM means: "after the write operation returns, data has been effectively writen on a quorum of nodes in the local datacenter"

    TWO means: "after the write operation returns, data has been writen on at least 2 nodes in any of the datacenter"

    If you want to read the data you have just written with LOCAL_QUORUM in the same datacenter, you should use LOCAL_ONE consistency. If you read with ONE, then there is a chance that the closest replica is in the "remote" datacenter and therefore not yet replicated by Cassandra.

    This also depends on the load balancing strategy configured at the driver level. You can read more about this here: https://datastax.github.io/java-driver/manual/load_balancing/