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.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
