Counter Type in Cassandra


A distributed, highly scalable NoSQL database with high availability and fault tolerance for massive volumes of data is called Apache Cassandra. Support for a unique data type called Counter Type is one of Cassandra's distinguishing characteristics. In this post, we will examine Counter Type in Cassandra, discuss its advantages, and offer usage examples.

What does Cassandra's Counter Type mean?

For the purpose of storing counter values, Cassandra has a specific data type called Counter Type. In order to keep track of activities like likes, upvotes, downvotes, and page visits, counters are utilized. A counter value in Cassandra may only be increased or decreased; it is never fixed at a particular number. One or more counter columns are included in the implementation of Counter Type as a column family.

Counter Type in Cassandra's advantages

Through the replication of updates across all of the replicas in the cluster, Counter Type offers high availability and fault tolerance for counter values. This makes sure that even if part of the nodes fails, the changed value will still be accessible. The "increment and decrement" approach is a unique method for altering counter values that Cassandra offers. This technique makes sure that every replica gets updated with the counter's most recent value.

Operations of a Counter Nature in Cassandra

For updating counter values, Cassandra offers unique procedures. Read, decrement, and increment are some of these operations.

Increment − By performing this action, the value of a counter column is increased. The following syntax is used to increase a counter column −

UPDATE <table_name> SET <counter_column_name> = <counter_column_name> + <value> WHERE <row_key> = '<key>';

Example

Input Table

| user_id   | name      | likes |
|-----------|-----------|-------|
| user123   | John      | 5     |
| user456   | Jane      | 10    |
| user789   | Michael   | 2     |

For instance, you can use the following formula to increase a counter value for a user's likes −

UPDATE users SET likes = likes + 1 WHERE user_id = 'user123';

Output Table

| user_id   | name      | likes |
|-----------|-----------|-------|
| user123   | John      | 6     |
| user456   | Jane      | 10    |
| user789   | Michael   | 2     |

Decrement − To decrease the value of a counter column, apply this procedure. The following syntax is used to decrement a counter column −

UPDATE <table_name> SET <counter_column_name> = <counter_column_name> - <value> WHERE <row_key> = '<key>';

Example

Input Table

| user_id  | name       | age | likes | dislikes |
|----------|------------|-----|-------|----------|
| user123  | John Smith | 30  | 5     | 3        |
| user456  | Jane Doe   | 25  | 7     | 2        |
| user789  | Bob Johnson| 40  | 2     | 8        |

For instance, you can use the following formula to lower a counter value for a user's dislikes −

UPDATE users SET dislikes = dislikes - 1 WHERE user_id = 'user123';

Output Table

| user_id  | name       | age | likes | dislikes |
|----------|------------|-----|-------|----------|
| user123  | John Smith | 30  | 5     | 2        |
| user456  | Jane Doe   | 25  | 7     | 2        |
| user789  | Bob Johnson| 40  | 2     | 8        |

Read − This procedure is used to read a counter column's value. The following syntax is used to read a counter column −

SELECT <counter_column_name> FROM <table_name> WHERE <row_key> = '<key>';

Example

Input Table

users table:

| user_id   | likes     |
|-----------|-----------|
| user123   | 10        |
| user456   | 5         |
| user789   | 20        |

For instance, you can use the query below to determine the significance of a user's likes −

Output Table

| likes     |
|-----------|
| 10        |

Batch − Multiple counter columns can be updated using the batch procedure in a single batch. The following syntax is used to update several counter columns

BEGIN BATCH
   UPDATE <table_name> SET <counter_column_name1> = <counter_column_name1> + <value1> WHERE <row_key> = '<key1>';
   UPDATE <table_name> SET <counter_column_name2> = <counter_column_name2> + <value2> WHERE <row_key> = '<key2>';
APPLY BATCH;

Example

Input Table

+---------+-------+
| user_id | likes |
+---------+-------+
| user123 |  10   |
| user456 |  20   |
+---------+-------+

For instance, you may use the following command to increase the likes of two people at once

BEGIN BATCH
   UPDATE users SET likes = likes + 1 WHERE user_id = 'user123';
   UPDATE users SET likes = likes + 1 WHERE user_id = 'user456';
APPLY BATCH;

Output Table

+---------+-------+
| user_id | likes |
+---------+-------+
| user123 |  11   |
| user456 |  21   |
+---------+-------+

Limitations of Cassandra's Counter Type

Counter Type has various restrictions even though it offers high availability and fault tolerance for counter values. The inability to delete or change columns with non-counter values is one of the restrictions. As a result, a counter column can only have its value increased or decreased once it has been formed.

Another drawback is that using Counter Type might affect how well a Cassandra cluster performs. This is due to the fact that updating a counter column necessitates reading from and writing to several cluster nodes, which may increase network traffic and latency.

Conclusion

Cassandra's strong Counter Type feature enables fault-tolerant high availability storing and updating of counter values. For situations when you must keep track of the number of occurrences, such as likes, dislikes, and page visits, Counter Type was created. However, Counter Type has several restrictions, including the influence on the performance of a Cassandra cluster and the impossibility to delete or edit counter columns with non-counter values. You may increase the scalability and dependability of your Cassandra database by utilizing Counter Type wisely and by being aware of its advantages and limits.

Updated on: 26-Apr-2023

438 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements