Cassandra: Lessons Learned
Join the DZone community and get the full member experience.
Join For Free
after working with
cassandra
for couple of months i thought of jotting down my cassandra experience
including what we did, what i liked and not so liked. but i need to put a
little disclaimer up front. we are currently using cassandra 0.7
(“what !#$*?? have you been living under a rock? get your self a life
and upgrade your darn installation” you would say. i know. i know..
. we are working on it.). so some of the stuff mentioned here would not
relate to later versions although the concepts we used are still mostly
applicable. (i think kudos should go to cassandra people for churning
out several quick releases with some cool new features.)
that being said let me go through what we have been doing with cassandra. our use case is roughly as follows. we are using cassandra as the persistence layer of our business activity monitoring solution at wso2 which is rewritten to scale better (about which i am hoping to blog some where in the line of “a scalable big data ready bam solution - wso2 bam” some time soon.). the data are pumped from servers being monitored as events consisting of key value pairs and get written to cassandra store after some minimal required processing at the receiver.
figure 1 : event format
these data are then indexed to facilitate the data analysis . here our use case differs from normal cassandra use cases where the queries are known in advance and we start from query and build our cassandra data model. since what we are building is a generic data analytic framework we don’t know what queries users want to perform on their stored data in cassandra. but then we thought if we can build set of indexes up front according the queries to be done it would solve our dilemma. so we decided to give users the ability to define what indexes to be built using an xml configuration which is roughly as follows.
<index name="serverbytime"> <part name="server"/> <part name="time"/> </index>
here we are building a composite index which can serve queries such as “give me the events that came from server ‘esb12′ during the time period ’08:00′ to ’09:00′”. the indexing happens as described below.
figure 2 : indexing model
1.
read an event from primary column family. (basically a row from primary column family)
2 . if event contain server and time keys get the corresponding values of these keys and concatenate them to create row key of index column family (in this case serverbytime).
3. create index column family if not present. add a new row having created row key if not already existing in the index column family and add column containing event id to it.
4. add newly created row key as a column in the index row of index column family.
so as can be seen we are using a column family per index. cassandra doesn’t sort rows on row keys if you are using randompartitioner which is the recommended partitioner for better performance. but columns within rows are always sorted using column key. we use this property to store index so that it can be queried using range queries. each column family has a special row called “ index row ” which stores all the row keys of the column family. when it is required to query the index, a range query on this row is done first to fetch the required row keys. let’s say that we wanted to do a query to fetch “all the events from servers esb12 to esb15 during the time periods “08:00 to 09:00″. let’s see how this is done using our indexing model.
1. get the range values for required sub indexes. in this case “esb12″ to “esb15″ for server and “08:00″ to “09:00″ for time.
2. create the range first and last values by concatenating values of sub indexes. the range first value would be “esb12—08:00″ and the range last would be “esb15—09:00″. here ‘—’ is the constant delimiter we are using during the concatenation of row key parts.
3. do the range query on index row and get the row keys in the range.
4. fetch each row to using row key to get the result. actually what we are getting here is set of event ids. to get actual event details another look up in primary column family using the event id is required.
here using this model we are able to do range queries on multiple indexes using this method. one advantage of this over native secondary index facility present from version 0.8 onward is that the first index query isn’t restricted to an equality operation (e.g. server equals ‘esb12′ etc.). but the order of sub indexes making up of composite index (e.g: row key part order. in this case server—time ) determines the types of queries which can be made. for instance we cannot query just using time. for that we would need to create another composite index starting with time as the first sub index.
we also had the requirement of fetching rows in batches from column families with each row being fetched exactly once to be processed. we achieved this using another special row having all the row insertion times to order rows according to the time of storage and then fetching rows in batches by doing range queries on this time stamp row with batch size limit.
all in all it’s rather involved scheme to get what we wanted out of
cassandra. not a big selling point i would say. also concatenating and
creating row keys seemed little hackish to me. also we had to come up
with some tricks to make batching work with range queries. may be it’s
not that strange cassandra is also fondly known as “hackendra” among
some of us (has got kind of nice ring to it i feel
) since sometimes it’s hard not to have that pesky feeling “am i doing a
hack here???” at the back of your mind when working with cassandra. but
by no means it means cassandra is evil. we like cassandra for its fast
writes and scalable architecture. but in real life when it comes to
relationships not every thing is perfect. but it doesn’t mean we don’t
care. our relationship with cassandra is no different.
.
source: http://chamibuddhika.wordpress.com/2011/11/27/cassandra-lessons-learnt/
Opinions expressed by DZone contributors are their own.
Comments