Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

In Cassandra, what are the partition key and clustering columns, and why can you not query freely by any column as in SQL?

The partition key decides which nodes store a row: Cassandra hashes it to a token, and all rows with the same partition key are stored together on the same replicas. Clustering columns sort the rows inside a partition, which allows range queries such as the last week of readings within it. We cannot filter freely by any column because an efficient query must name the partition key so Cassandra knows which nodes to ask; anything else needs a table designed for that query, and ALLOW FILTERING, which forces a scan, is rarely acceptable in production.

Why this question is asked

Developers coming from SQL usually meet Cassandra's rules as errors: a query that works in PostgreSQL is rejected with a message telling them to add ALLOW FILTERING. Understanding why requires understanding how Cassandra spreads data across machines. Interviewers ask this concept question because every other Cassandra decision, from data modelling to performance, follows from it. In this answer, we will recap how Cassandra distributes data, explain the partition key and clustering columns with an example, see which queries work and why others are refused, look at ALLOW FILTERING and secondary indexes, and map the key in Spring Data Cassandra.

1A Quick Recap: How Cassandra Spreads Data

Cassandra is a distributed database: a cluster of nodes, often dozens, with no single primary. To decide where a row lives, Cassandra takes the row's partition key, runs it through a hash function (Murmur3 by default) to get a number called a token, and each node owns ranges of tokens. With a replication factor of 3, the row is stored on the node owning its token and two more. Any node can receive a query and act as coordinator, forwarding it to the replicas that hold the data.

So the partition key is the address of the data. If we know it, the coordinator computes the token and goes straight to three nodes. If we do not, the coordinator has no idea where matching rows are and would have to ask every node in the cluster.

2Partition Key and Clustering Columns in a Table

Here is a table of sensor readings. The primary key has two parts: the first, in its own parentheses, is the partition key; the rest are clustering columns.

Java
CREATE TABLE readings_by_sensor (
sensor_id text,
day date,
read_at timestamp,
value double,
PRIMARY KEY ((sensor_id, day), read_at)
) WITH CLUSTERING ORDER BY (read_at DESC);
-- partition key: (sensor_id, day) -> one partition per sensor per day, hashed to a token
-- clustering column: read_at -> rows inside the partition stored sorted, newest first
How rows are laid out
Partition (sensor_id, day)Token -> replicasRows, sorted by read_at DESC
(s-1, 2026-11-05)-6.1e18 -> nodes 2, 5, 710:05 21.4 | 10:00 21.3 | 09:55 21.1
(s-1, 2026-11-04)3.9e18 -> nodes 1, 4, 623:55 19.0 | 23:50 19.2 | ...
(s-2, 2026-11-05)8.2e17 -> nodes 3, 5, 810:05 18.7 | 10:00 18.6 | ...

All readings for sensor s-1 on one day sit together in one partition, on the same replicas, physically sorted by read_at. That sorting is what clustering columns are for: they order rows within a partition and make the primary key unique. Because the rows are stored in order, reading a time range inside the partition is a single sequential read on each replica.

3Which Queries Work, and Why Others Are Refused

A query is efficient when it gives the whole partition key with equality and, optionally, restricts clustering columns in order, with a range only on the last one it restricts.

SQL
-- works: full partition key, range on the clustering column
SELECT value FROM readings_by_sensor
WHERE sensor_id = 's-1' AND day = '2026-11-05' AND read_at > '2026-11-05 09:00'; -- 3 replicas, one slice
-- refused: no partition key, so Cassandra cannot find the nodes
SELECT * FROM readings_by_sensor WHERE value > 30;
-- InvalidRequest: Cannot execute this query as it might involve data filtering ...
-- use ALLOW FILTERING
-- refused: only part of the partition key
SELECT * FROM readings_by_sensor WHERE sensor_id = 's-1';

Notice that even sensor_id alone is refused. The token is computed from both partition key columns together, so without the day Cassandra cannot compute it. This is the deepest difference from SQL: a relational database can always fall back to scanning a table on one machine, while Cassandra refuses by default to silently scan a table spread over a whole cluster. The answer to a new query is a new table, such as readings_by_value_alert keyed by the attribute we search on, written alongside the first one. This is called query-first or query-driven modelling: we design one table per query.

4ALLOW FILTERING and Secondary Indexes

Adding ALLOW FILTERING makes Cassandra accept the query by reading candidate rows and discarding those that do not match. Without a partition key, that means reading data from every node, and its cost grows with the size of the table rather than with the result. On a development table with 1,000 rows it looks fine; on a production table with a billion rows it times out and loads the whole cluster. It is acceptable only in narrow cases, such as filtering a small number of rows inside one known partition.

Secondary indexes exist too, and Cassandra 5 introduced storage-attached indexes that make querying non-key columns more practical. They still have to consult many nodes when the partition key is not given, so they suit occasional queries or queries combined with a partition key, not the main high-volume path. Check the index options for your Cassandra version before relying on them.

5Mapping the Key in Spring Data Cassandra

Spring Data Cassandra maps the same structure with @PrimaryKeyColumn on the entity, or with a separate key class annotated @PrimaryKeyClass. The type attribute says whether a column is part of the partition key or a clustering column, and ordinal gives the order.

Java
@Table("readings_by_sensor")
public class Reading {
@PrimaryKeyColumn(name = "sensor_id", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
private String sensorId;
@PrimaryKeyColumn(name = "day", ordinal = 1, type = PrimaryKeyType.PARTITIONED)
private LocalDate day;
@PrimaryKeyColumn(name = "read_at", ordinal = 2, type = PrimaryKeyType.CLUSTERED,
ordering = Ordering.DESCENDING)
private Instant readAt;
private double value;
}
public interface ReadingRepository extends CassandraRepository<Reading, MapId> {
List<Reading> findBySensorIdAndDayAndReadAtAfter(String sensorId, LocalDate day, Instant from); // valid
List<Reading> findByValueGreaterThan(double v); // rejected by Cassandra at runtime unless @AllowFiltering
}

Spring Data does not protect us from bad queries: a derived method on a non-key column compiles, and Cassandra rejects it when it runs, unless we add @AllowFiltering, which brings back exactly the scan we wanted to avoid. The table design must come from the queries first.

6Follow-up questions

What is the difference between the primary key and the partition key?

The primary key is the full key that identifies a row: partition key plus clustering columns. The partition key is only its first part, the one that decides placement. In PRIMARY KEY ((sensor_id, day), read_at), the primary key has three columns and the partition key two.

Can I sort by a column that is not a clustering column?

Not on the server. Cassandra returns rows in clustering order within a partition, and ORDER BY can only reverse or follow that order. Sorting by anything else must be done in the application or through another table clustered by that column.

Why include the day in the partition key?

To keep partitions bounded. Without it, a sensor writing every few seconds for years would grow one enormous partition on three nodes. Bucketing by day caps each partition at one day of readings.

7Conclusion

In this answer, we saw that Cassandra places each row by hashing its partition key to a token owned by specific replicas, so all rows with the same partition key live together, and that clustering columns sort rows within a partition to allow range queries. Efficient queries must name the full partition key, because otherwise Cassandra cannot locate the data and would have to scan the cluster; ALLOW FILTERING forces that scan and is rarely acceptable. New queries get new tables, and Spring Data Cassandra maps the key with @PrimaryKeyColumn or @PrimaryKeyClass.

What to say in the interviewAbout 100 seconds aloud

Open with

The partition key is the first part of the primary key, and it decides where the data lives. Cassandra hashes it, with Murmur3 by default, into a token, each node owns token ranges, and with replication factor three the row lands on three nodes.

Then, if they want more

Every row with the same partition key is stored together on those replicas. Clustering columns are the rest of the primary key: they sort the rows inside a partition on disk, so a range query like readings after nine o'clock for one sensor on one day is a single sequential slice.

The reason I can't filter by any column like in SQL is that without the full partition key, the coordinator can't compute the token, so it doesn't know which nodes to ask, and the only way to answer would be to ask every node in the cluster. Cassandra refuses that by default and tells you to add ALLOW FILTERING.

Adding it makes the query work by scanning, which looks fine on a small test table and falls over on a billion rows, so in production it is rarely acceptable, except inside a known partition. The Cassandra way is query-first modelling: one table per query, written together.

Secondary indexes, including the newer storage-attached ones, help for occasional queries but are not the main path. In Spring Data Cassandra I map this with @PrimaryKeyColumn, type PARTITIONED or CLUSTERED, with ordinal and ordering, or a separate @PrimaryKeyClass.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring Data reference
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "In Cassandra, what are the partition key and clustering columns, and why can you not...": https://scrivio-interview-library.netlify.app/databases/spring-data-nosql/4e95b29f11/