high and low cardinality in Cassandra
Asked Answered
G

1

12

I keep coming across these terms: high cardinality and low cardinality in Cassandra.

I don't understand what exactly they mean. what effects they have on queries and what is preferred. Please explain with example since that will be be easy to follow.

Gustafsson answered 3/8, 2014 at 2:49 Comment(0)
S
22

The cardinality of X is nothing more than the number of elements that compose X. In Cassandra the partition key cardinality is very important for partitioning data.

Since the partition key is responsible for the distribution of the data across the cluster, choosing a low cardinality key might lead to a situation in which your data are not distributed.

Imagine you have a cluster of 20 nodes storing comments -- the Replication Factor (RF) is 2. Each comment has it's own vote going from 1 to 5. Now, since you want to easily retrieve comments by votes, you might be tempted to choose vote as partition key.

CREATE TABLE comments(vote int, content text, id uuid, PRIMARY KEY(vote, id));

In this situation the only key responsible for data distribution is vote, which has a very low cardinality since it can contains only 5 values (1,2,3,4,5). This means that, in the best situation 5 different nodes will be the owners of the 5 different partitions (which are "all comments with vote 1" ... "all comments with vote 5"), and again in best situation, with a RF of 2, 10 different nodes will hold your data. As you can see you have a 20 nodes cluster which isn't used more than 50% in best situation.

Data distribution is very important, that's why partition key cardinality matters a lot

HTH, Carlo

Savage answered 3/8, 2014 at 6:14 Comment(3)
I've edited my post -- as you can see for each vote you will retrieve a lot of comments. So it's a very wide row situations. If you have 10 millions of comments, let's say equally distributed by votes, for each vote partition you will retrieve 2 millions result. Which is a very wide row.Savage
#25103469Gustafsson
What does the acronym RF mean? Edit: For those wondering, it means replication factor. I think the answer's phrasing could be improved to read: "Imagine you have a cluster of 20 nodes storing comments, with a replication factor (RF) of 2."Dx

© 2022 - 2024 — McMap. All rights reserved.