Database Reference
In-Depth Information
Cassandra-powered applications (e.g., Number of page hits, number of active users,
etc.).
In Cassandra, a counter is a 64-bit signed integer. A write on counter will require a
read from replica nodes (this depends on consistency level, default is ONE). While
reading a counter column value, read has to be consistent.
Counter Column with and without replicate_on_write
Default value of
replicate_on_write
is true. If set to false it will replicate on one rep-
lica node (irrespective of replication factor). That might be helpful to avoid read-
before-write on serving write request. But any subsequent read may not be consistent
and may also result in data loss (single replica node is gone!).
Play with Counter Columns
In
Chapter 1
we discussed setting multiple clusters on a single machine. First let's start
with a cluster of three nodes on a single machine. (Please refer to the “Configuring
Multiple Nodes on a Single Machine” section in
Chapter 1
.) In this recipe we will dis-
cuss the do's and don'ts of using counter columns.
1.
Let's create a keyspace
counterkeyspace
:
create keyspace counterkeyspace with
replication = {'class' : 'SimpleStrategy',
'replication_factor' : 2 }
2.
Create a column family
counternoreptable
with
replic-
ate_on_write
as
false
:
create table counternoreptable(id text
PRIMARY KEY, pagecount counter) with
replicate_on_write='false';
3.
Update
pagecount
to increment by 2 as follows:
update counternoreptable set
pagecount=pagecount+2 where id = '1';