简体   繁体   中英

how to avoid data overwrite (lost) write - update on same object by multiple thread in Cassandra

I'am running a multiple thread to do modification and update on the same object!

let's keep it simple:

I have an obj1: | id | counter | timestamp | :)

run() 3 Thread t1, t2 t3 to do counter++

I'am new in Cassandra so from your experience, how can I lock the object to do my update or witch solution you suggest for me to use to avoid the data overwrite in such situation!

looking to hear from you and thank you

There are no locks. Instead of mutating same object, write a log of mutations to reconcile. Look up CRDT or event sourcing and depending on scenario things can be implemented a little differently.

Alternatively you can use light weight transactions and keep retrying until your mutation works, but your throughput will be very poor in comparison. Depending on your concurrency/throughput requirements it may be sufficient though

UPDATE purchases SET balance=-200 WHERE user='user1' IF balance=-208;

If it fails retry with new IF/balance clause.

Worth mentioning there is a counter type: https://docs.datastax.com/en/cql/3.1/cql/cql_using/use_counter_t.html if your just incrementing or decrementing which would do your specific case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM