Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cassandra Optimistic Locking

I have a cassandra table1:

CREATE TABLE Policy.table1 (
name VARCHAR ,
date TIMESTAMP ,
version_num INT,
PRIMARY KEY   (
         name
         )) WITH caching = 'all'
     --   and memtable_flush_period_in_ms = 7200 ;
     ;

I need to implement optimistic locking on tis table. When we read a row from table1 we remember its version_num. And when we want to update this row we compare current version_num value and value that we remembered. Also we need to increment version_num on each update.

Problems:

  1. We cannot put version_num into where clause, this will create an error: Bad Request: Non PRIMARY KEY version_num found in where clause:

     update table where name = 'abc' and version = 3
    
  2. We cannot set version_num as a part of primary key, because we need to update its value

  3. If we index version_num it will not help for update statements, same exception will be thrown
  4. The only way I see is to get current version_num value by Java, and if expected and actual version_num values are the same - than execute update. The problem is that in this case we have not atomic operation of checking version_num value and update the row.

Do you see any solution for this problem?

like image 575
dmreshet Avatar asked Jun 25 '14 10:06

dmreshet


People also ask

Does Cassandra support locking?

Cassandra does not use RDBMS ACID transactions with rollback or locking mechanisms, but instead offers atomic, isolated, and durable transactions with eventual/tunable consistency that lets the user decide how strong or eventual they want each transaction's consistency to be.

What is optimistic and pessimistic locking?

Optimistic locking , where a record is locked only when changes are committed to the database. Pessimistic locking , where a record is locked while it is edited.

How do you do optimistic locking?

In order to use optimistic locking, we need to have an entity including a property with @Version annotation. While using it, each transaction that reads data holds the value of the version property. Before the transaction wants to make an update, it checks the version property again.

What is optimistic locking database?

Optimistic locking is a technique for SQL database applications that does not hold row locks between selecting and updating or deleting a row. The application is written to optimistically assume that unlocked rows are unlikely to change before the update or delete operation.


1 Answers

The solution was found there: Cassandra 2.0, Lightweight transactions http://www.datastax.com/documentation/cassandra/2.0/cassandra/dml/dml_ltwt_transaction_c.html

In case I execute query:

update table1 set version_num = 5 where name = 'abc'  if version_num = 4;

I will receive a row with [applied] column. This row contains boolean value: true = update was successful, false = in other case.

like image 130
dmreshet Avatar answered Oct 12 '22 22:10

dmreshet