Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cassandra concurrent writes

How does Cassandra guarantee eventual consistency when concurrent writes happen?

For example, Client A writes to tableA.rowA.colA, while at the same time Client B writes to tableA.rowA.colA.

The coordinator nodes distribute the request to the replica nodes say NodeA NodeB and NodeC.

On NodeA, ClientA request arrives first. On NodeB, ClientB request arrives first. Then, will it be forever inconsistent?

like image 950
user1947415 Avatar asked Dec 24 '22 17:12

user1947415


1 Answers

Cassandra follows a "Last Write Wins" policy. The timestamp used can be set manually but by default is set client side by the requester see Datastax Java Driver docs. The order in which writes arrive is irrelevant. If write A has an earlier timestamp than write B then it will always be overwritten by write B. The only ambiguous case is when the timestamps match exactly. In that case the greater value wins.

The eventually consistent portion of this is:

  • Assuming A has an earlier timestamp than B
  • If A arrives on Replica 1 and B arrives on Replica 2, the correct state is B
  • Replica 1 will respond A until it receives the information about B from Replica 2
  • When B is replicated the Replica 1 will respond B as well.

Most use-cases involve not storing state in Cassandra so these sorts of issues do not arise.

like image 175
RussS Avatar answered Jan 04 '23 12:01

RussS