I'm new in Cassandra. I can't understand what is the advantage of using counter in a table (or even in a different table if the non-counter columns are not part of the composite PRIMARY KEY)? Why we don't use a column with Int type, when I will have some statements like x=x++; what is the different between using int or counter?
Is that possible to use increments or decrements for Int Type in Cassandra at all?
Asked
Active
Viewed 5,204 times
9
1 Answers
16
Why we don't use a column with Int type, when I will have some statements like x=x++; what is the different between using int or counter?
Because using a normal Int column would require read-before-write and lock for operations like x=x++
Indeed, for a distributed database when you can have concurrent updates on the same value, the only way to guarantee consistent behaviour for x=x++
is:
- lock the current record
- read the current value of x, increment it by 1
- write back the new value of x
- release the lock
Counter type allows concurrent increment/decrement on the value without neither requiring read-before-write nor locking

doanduyhai
- 8,712
- 27
- 26
-
Thanks for good answer. But, I need more information. We can manage the consistency-level in CQL; is this possible when we have Counter? if we select the high CL, then the Counter would require Locking? in the case that the consistency is important for our scenario, which approach is better? using INT? or using Counter with high CL? – Elnaz Feb 16 '16 at 05:58
-
1You can use Consistency Level with Counter. It has NOTHING to do with locking. Please look at these slides to understand Consistency Level: http://www.slideshare.net/doanduyhai/cassandra-introduction-nantesjug/24 – doanduyhai Feb 16 '16 at 07:12
-
I didn't understand if using Counter type itself can Guarantee its Consistency or not. when we just use x=x++; how it can understand if it has increased before or not. can LTW help us in this situation? if yes, how? thanks. – Elnaz Feb 16 '16 at 08:23
-
1You can use QUORUM consistency level for writing and reading counter values. It guarantees that the increment/decrement has been applied to a majority of replicas when writing and it guarantees you to read the **latest** written value. LWT is not supported for counters. – doanduyhai Feb 16 '16 at 08:36
-
1Wondering how counter column is internally working to avoid read-update(increment)-write operation and locks – user3366706 Mar 21 '18 at 19:02