Database / Apache Cassandra Intermediate and Advanced interview questions
What is the difference between a partition key and a clustering key?
The partition key decides which node (and its replicas) a row physically lives on. Cassandra hashes the partition key with the configured partitioner to get a token, and that token maps to a position on the ring. Every row sharing the same partition key value lands on the same set of replicas.
The clustering key only matters once you are already inside a partition. It controls the on-disk sort order of the rows within that partition, so range scans and "latest N" style queries stay fast without touching other partitions.
| Partition Key | Clustering Key |
| Determines data placement across nodes. | Determines sort order within a partition. |
| Used to route queries to the correct replica. | Used for range/slice queries inside a partition. |
| Must be an equality match in WHERE clause (no ranges). | Supports range operators like >, <, and ORDER BY. |
CREATE TABLE sensor_data ( sensor_id text, reading_time timestamp, value double, PRIMARY KEY (sensor_id, reading_time) ); -- sensor_id = partition key, reading_time = clustering key
Getting this split wrong is one of the most common Cassandra data-modeling mistakes: too little in the partition key creates hot, oversized partitions, while putting sorting columns in the partition key breaks range queries entirely.
More Related questions...