Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strong Consistency in Cassandra

According to datastax article, strong consistency can be guaranteed if, R + W > N where R is the consistency level of read operations W is the consistency level of write operations N is the number of replicas

What does strong consistency mean here? Does it mean that 'every time' a query's response is given from the database, the response will 'always' be the last updated value? If conditions of strong consistency is maintained in cassandra, then, are there no scenarios where the data returned might be inconsistent? In short, does strong consistency mean 100% consistency?

Edit 1

Adding some additional material regarding some scenarios where Cassandra might not be consistent even when R+W>RF

  1. Write fails with Quorum CL
  2. Cassandra's eventual consistency
like image 240
Vishal Sharma Avatar asked Jan 08 '18 13:01

Vishal Sharma


People also ask

What is strong consistency in Cassandra?

Strong consistency can be achieved if W + R > RF, where R – read CL replica count, W – write CL replica count, RF – replication factor. In this scenario, you get a strong consistency since all client reads always fetches the most recent written data.

Is Cassandra highly consistent?

Meeting the requirements of performance, reliability, scalability and high availability in production Cassandra is an eventually consistent storage system. Eventually consistent implies that all updates reach all replicas eventually.

What is consistency level in Cassandra?

The Cassandra consistency level is defined as the minimum number of Cassandra nodes that must acknowledge a read or write operation before the operation can be considered successful. Different consistency levels can be assigned to different Edge keyspaces.

How do you maintain consistency in Cassandra?

Maintaining consistency requires balancing availability and partitioning. Fortunately, Apache Cassandra lets us tune this balancing according to our needs. In this blog, we are going to see how we can tune consistency levels during reads and writes to achieve faster reads and writes.


4 Answers

Cassandra has tunable consistency with some tradeoffs you can choose.

R + W > N - this simply means there must be one overlapping node in your roundtrip that has the actual and newest data available to be consistent.

For example if you write at CL.ONE you will need to read at CL.ALL to be sure to get a consistent result: N+1 > N - but you might not want CL.ALL as you can not tolerate a single node failure in your cluster.

Often you can choose CL.QUORUM at read and write time to ensure consistency and tolerate node failures. For example at RF=3 a QUORUM needs (3/2)+1=2 nodes available, so R+W>N will be 4>3 - your requests are consistent AND you can tolerate a single node failure.

One thing to keep in mind - it is really important to have thight synchronized clocks on all your nodes (cassandra and application), you will want to have ntp up and running.

like image 186
Mandraenke Avatar answered Oct 16 '22 14:10

Mandraenke


Yes. If R + W consistency is greater than replicas then you will always get consistent data. 100% consistency. But you will have to trade availability to achieve higher consistency.

Cassandra has concept of tunable consistency (set consistency on query basis).

like image 20
undefined_variable Avatar answered Oct 05 '22 20:10

undefined_variable


While this is an old question, I thought I would chip in to set the record straight.

R+W>RF does not imply strong consistency

A system with **R+W>RF* will only be eventually consistent. The claims for strong consistency guarentee break during node failures or in between writes. For example consider the following scenario:

Assume that there are 3 nodes A,B,C with RF=3, W=3, R=2 (hence, R+W = 5 > 3 = RF)

Further assume key k is associated to value v i.e. (k,v) is stored on the database. Suppose the following series of actions occur:

  • t=1: (k,v1) write request is sent to A,B,C from a user
  • t=2: (k,v1) reaches A and is written to store at A
  • t=3: Reader 1 sends a read request for key k, which is replied to by A and B
  • t=4: Reader 1 receives response (k,v1) - by latest write wins rule
  • t=5: Reader 1 sends another read request which gets served by nodes B and C
  • t=6: Reader 1 receives response (k,v), which is an older value INCONSISTENCY
  • t=7: (k,v1) reaches C and is written to store at C
  • t=8: (k,v1) reaches B and is written to store at B

This demonstrates that W+R>RF cannot guarantee strong consistency. To ensure strong consistency you might want to use another algorithm such as paxos or raft that can help in ensuring that the writes are atomic. You can read an interesting article on the same here (Do checkout the FAQ section)


Edit:

Cassandra does have some internal mechanism (called the blocking read repairs) - that trigger synchronous writes before response from the db is sent back to client. This kind of synchronous read repair occurs in case of inconsistencies amongst the nodes queried to achieve read consistency level and ensures something known as Monotonic Read Consistency [See below for definitions]. This causes the (k,v1) in above example to be written to node B before response is returned in case of first read request and so the second read request would also have an updated value. (Thanks to @Nadav Har'El for pointing this out)

However, this still does not guarantee strong consistency. Below are some definitions to clear it of:

Sequential/Strong Consistency: the result of any execution is the same as if the reads and writes occur in some order, and the operations of each individual processor appear in this sequence in the order specified by its program [as defined by Leslie Lamport]

Monotonic Read Consistency: once you read a value, all subsequent reads will return this value or a newer version

Sequential consistency would require the client program/reader to see the latest value that was written since the write statement is executed before the read statement in the sequence of program instructions.

like image 3
Prakhar Agrawal Avatar answered Oct 16 '22 14:10

Prakhar Agrawal


For both reads and writes, the consistency levels of ANY , ONE , TWO , and THREE are considered weak, whereas QUORUM and ALL are considered strong.

like image 2
Horia Avatar answered Oct 16 '22 12:10

Horia