1
votes

We have a Cassandra table where we are storing the number of sales of items in our Catalog. We are using counter data type for this purpose. We are still not sure if counter can handle race conditions. Multiple servers trying to increment the count for the same item.

Do counters handle race conditions or do we need to implement our own locking system for the updates? If they do not then what is the advantage of using a counter over an integer?

2
not race conditions exactly as the paxos round helps, but around write failures and network partitions theres scenarios that can lead to under and over counting. - Chris Lohfink

2 Answers

2
votes

You can use SERIAL queries, which make your queries linearizable at the cost of performance, which is something to keep in mind, even if consistency and data race safety is your primary concern. You can read more about the said query type in the following links:

1
votes

Counters by definitions aren't precise, so you shouldn't use them for inventory and similar things.

They aren't idemponent so they aren't retried in case of failures, so you can get undercount in case of default retry policy, or overcount if you implement custom retry policy...

The advantage of using counters that they may work in specific situations where precision isn't so important. They are much lightweight than LWTs.