2

Consider the following batch statement in Cassandra:

 BEGIN BATCH
 INSERT INTO users (userID, password, name) VALUES ('user2', 'ch@ngem3b', 'second user')

 UPDATE users SET password = 'ps22dhds' WHERE userID = 'user2'

 DELETE * FROM users WHERE userID = 'user2'
 INSERT INTO users (userID, password, name) VALUES ('user2', 'ch@ngem3c', 'Andrew')
 APPLY BATCH;

Will the above statements in Cassandra batch ensures row-level isolation (userID is the row key) as the row key is the same?

2 Answers2

2

One important thing to note is that within a batch without timestamps specified per statement all of the statements will be executed at the same timestamp.

This means all four statements you wrote

INSERT INTO users (userID, password, name) VALUES ('user2', 'ch@ngem3b', 'second user')

UPDATE users SET password = 'ps22dhds' WHERE userID = 'user2'

DELETE * FROM users WHERE userID = 'user2'

INSERT INTO users (userID, password, name) VALUES ('user2', 'ch@ngem3c', 'Andrew')

All happen at the same time, in this case the highest value for the commonly modified cell is used. While all four statements are applied the outcome is most likely not what you are expecting.

Basically C* will see

INSERT ('user2', 'ch@ngem3b', 'second user')
INSERT ('user2', 'ps22dhds', 'second user')
INSERT ('user2', 'Tombstone', 'Tombstone')
INSERT ('user2', 'ch@ngem3c', 'Andrew')

In this case since they all have the same timestamp C* resolves the conflict by choosing the largest value for the cells and you will end up with, (unless I got the byte ordering wrong here)

('user2', 'ps22dhds', 'second user')

Instead for this kind of operation consider using the Check And Set (CAS) operations in C*.

http://www.datastax.com/dev/blog/lightweight-transactions-in-cassandra-2-0

RussS
  • 16,476
  • 1
  • 34
  • 62
  • Will the above issue occur if my **userID** is the partition key and **password** is the column key? As the compound primary key is different in each case, it will insert the data as a new column right? – Akhil K Kamal Jun 02 '15 at 06:00
1

Starting with Cassandra 2.0.6, all batch statements for a single partition will be executed as a single update operation. This would involve row-level isolation.

Stefan Podkowinski
  • 5,206
  • 1
  • 20
  • 25
  • http://stackoverflow.com/questions/30565887/cassandra-isolation-in-same-row-key-mutation/30570036#30570036 – Jobs Jun 01 '15 at 14:11