Search code examples
cassandraeventual-consistencynosql

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?


Solution

  • 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.