Nick
Nick

Reputation: 109

Cassandra Update statement

How can I perform UPDATE CQL query on Cassandra like this one:

UPDATE table_name SET COUNT = COUNT + 1 WHERE ID = ?

as fast as possible, if for example I send 100-300 records where 0 - 10 will be updated. Total records could be

SELECT count(*), min("count"), max("count") FROM table_name;
-- result 300000 | 1 | 120

I've tested BatchStatement.Type.UNLOGGED vs executeAsync. In general batch win by performance in 2 times but it is not recommend to use, see CASSANDRA-9283. I know Cassandra is used for Insert statements but maybe exist some good patterns for my case?

Upvotes: 1

Views: 85

Answers (1)

Chris Lohfink
Chris Lohfink

Reputation: 16400

Counter updates require paxos rounds and a read before write. Its the slowest possible operation in Cassandra. You instead might want to do something like an event sourced counter. Have a ((count_key, yymmdd) id) and you insert a new timeuuid for each record. Keep the current count in memory and increment it somewhere (maybe even just using redis). Then at end of each day write the total for that day in Cassandra. In case of an outage or something you can iterate through the events counting and restore the in memory count.

Upvotes: 3

Related Questions