docs/features/counters.rst
Counters are useful for any application where you need to increment a count, such as keeping a track of:
A counter is a special data type (column) that only allows its value to be incremented, decremented, read, or deleted. As a type, counters are a 64-bit signed integer. Updates to counters are atomic, making them perfect for counting.
A table with a counter column is created empty and gets populated and initialized by updates rather than inserts. A new record with a counter set to null is created on the first update statement with a particular PK value and then immediately incremented by a given number (treating null as a 0). For example:
.. code-block:: none
cqlsh:mykeyspace> CREATE TABLE cf (pk int PRIMARY KEY, my_counter counter); cqlsh:mykeyspace> SELECT * FROM cf; pk | my_counter ----+------------
.. code-block:: none
cqlsh:mykeyspace> UPDATE cf SET my_counter = my_counter + 0 WHERE pk = 20; cqlsh:cdc_test> SELECT * FROM cf; pk | my_counter ----+------------ 20 | 0
.. code-block:: none
cqlsh:mykeyspace> UPDATE cf SET my_counter = my_counter + 6 WHERE pk = 0; cqlsh:mykeyspace> SELECT * FROM cf; cqlsh:cdc_test> SELECT * FROM cf; pk | my_counter ---+------------ 0 | 6 20 | 0
However, counters have limitations not present in other column types:
idempotent <Idempotent>. In the case of a write failure, the client cannot safely retry the request.Example: ........
#. Create a table. The table can only contain the primary key and the counter column(s):
.. code-block:: shell
cqlsh:mykeyspace> CREATE TABLE cf (pk int PRIMARY KEY, my_counter counter);
#. Increment the count:
.. code-block:: shell
cqlsh:mykeyspace> UPDATE cf SET my_counter = my_counter + 3 WHERE pk = 0;
cqlsh:mykeyspace> SELECT * FROM cf;
pk | my_counter
---+-------------
0 | 3
#. Decrement the count:
.. code-block:: shell
cqlsh:mykeyspace> UPDATE cf SET my_counter = my_counter - 1 WHERE pk = 0;
cqlsh:mykeyspace> SELECT * FROM cf;
pk | my_counter
---+-----------
0 | 2
#. Delete the row:
.. code-block:: shell
cqlsh:mykeyspace> delete from cf where pk = 0;
cqlsh:mykeyspace> select * from cf;
pk | my_counter
---+-----------
Remember that once deleted, counter column values should not be used again. If you reuse them, proper behavior is not guaranteed.
More Information ................
blog <http://www.scylladb.com/2017/04/04/counters/>_ on counters, or see the data type :ref:description <counters>.ScyllaDB University: Advanced Data Modeling lesson <https://university.scylladb.com/courses/data-modeling/lessons/advanced-data-modeling/>_ - Covers advanced Data Modeling topics. We recommend that you start with the basic data modeling lesson first. It covers the application workflow, query analysis, denormalization, and more, providing practical hands-on examples.