Cassandra Primary Keys

December 11, 2017

Cassandra schemas can be a bit hard to design and are especially important to design correctly because of the distributed nature of Cassandra. Many new users of Cassandra try to design schemas similar to relational databases because of CQL’s similar syntax to SQL.


CREATE TABLE example (
  key uuid PRIMARY KEY

Composite Key

CREATE TABLE example (
  key1 text,  // partition key:  determines how data is partitioned across nodes
  key2 int,   // clustering key: determines how data is sorted within a partition
  PRIMARY KEY(key1, key2)

The goals for designing cassandra keys (stolen from the datastax documentation) are:

  1. Spread data evenly around the cluster
  2. Minimize the number of partitions read