Why don't we use a column with type Int when I have some statements like x = x ++; what is the difference between using int or counter?
Since a regular Int column will require read-before-write and a lock for operations like x=x++
Indeed, for a distributed database, when you can have concurrent updates with the same value, the only way to guarantee consistent behavior for x=x++ is:
- lock current record
- read the current value of x, increase it by 1
- write the new value of x
- release the lock
The type of counter allows you to simultaneously increase / decrease the value without using read-before-write and blocking
source share