1
votes

I am using cassandra.

I have two column families A and B. Both the column families have same data but both have different primary keys. Now I am using a batch statement to update the rows in these two tables.

Table schema is as follows:

Primary Key of table A [id1(partition key) id2(partition key) id3(clustering key)]

Primary Key of table B [id1(partition key) id2(partition key) state(clustering key) id3(clustering key)]

I want to update the state of both the tables. State is cluster key in B and in table A, it is simple column.

What I do is fetch the state from A and consider it as old state.

Then in batch what I do is first delete row from table A, then Delete row from Table B, Insert new row in table A and the insert new row in table B.

Note : Using the old state that is fetched from A, I make primary key of B and then delete from B and the insert new row in B.

It is working fine but for parallel requests it is not. If 2 requests are coming for same primary key from 2 different instances, then I am getting the problem. Table B gets the two entries with old and new state.

So How can I solve that in cassandra?

1

1 Answers

0
votes

Cassandra 2.0 and above supports Lightweight transactions.

where you have "IF NOT EXISTS" condition while you are inserting.

In your case, you can't check it when you are fetching the state from table A, but you can restrict it while inserting which will not allow duplicates in your case. E.g.

insert into A(id1, id2, state, id3)
values('val1', 'val1', 'val3', 'val4')
IF NOT EXISTS

So first one which executes will pass, but the second one will fail. So handle a retry / any failure mechanism from your client based on your business requirement.

Check this docs for more information: https://www.datastax.com/dev/blog/lightweight-transactions-in-cassandra-2-0