0
votes

i try to update record in Cassandra using CQL, and noticed for some reason i cannot change the column to its old values, here are the steps i performed,

  1. insert a brand new record with column token set to value1

    insert into instrucment(instrument_id, account_id, token) values('CDX-IT-359512FD43D3', 'CDX-IT-970A44E2DAF4','value1') USING TIMESTAMP 1605546853130000

  2. update the record to set column token to value2

    insert into instrucment(instrument_id, token) values('CDX-IT-359512FD43D3', 'value2') USING TIMESTAMP 1605546853130000

  3. update the record to set column token back to value1

    insert into instrucment(instrument_id, token) values('CDX-IT-359512FD43D3', 'value1') USING TIMESTAMP 1605546853130000

step 1 & 2 worked fine, but step3 failed, DB record showed the column token is still value2, why is that? is that because Cassandra think the value1+ timestamp 1605546853130000 is an old record thus wont' update it ?

1

1 Answers

0
votes

You are updating the same row (same partition key) with different values.

Cassandra normally determines the valid record for a row by timestamp. The record with the most recent timestamp 'wins'.

See here for more information how updates work: https://docs.datastax.com/en/cassandra-oss/3.0/cassandra/dml/dmlWriteUpdate.html

Since you are inserting with the same timestamp, you are simulating concurrent writes to the same row, concurrent to the same milisecond. If you are not setting the timestamp explicitly for your inserts, such concurrency is very unlikely.

In such truly concurrent cases Cassandra needs to turn to other methods to determine the 'winner'. Cassandra breaks the timestamp tie by comparing the byte values in a deterministic manner. In your case, the record with value2 wins.