Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

In Cassandra, explain this table's primary key:

SQL
CREATE TABLE messages (chat_id uuid, sent_at timeuuid, sender text, body text, PRIMARY KEY ((chat_id), sent_at)) WITH CLUSTERING
ORDER BY (sent_at DESC);

Which queries are efficient and which are not allowed without ALLOW FILTERING?

chat_id is the partition key: Cassandra hashes it with the Murmur3 partitioner to decide which nodes store all of that chat's messages. sent_at is a clustering column: inside the partition, rows are stored sorted by it, newest first because of CLUSTERING ORDER BY (sent_at DESC). So queries that give chat_id, optionally with a range on sent_at and a LIMIT, are efficient, while queries by sender or by sent_at without chat_id would need to scan every partition and are refused unless we add ALLOW FILTERING, which is a sign we need another table.

Why this question is asked

Cassandra's primary key is the most important line in any table definition, because it decides both where data lives in the cluster and which queries are possible at all. Developers coming from relational databases often write a query that looks harmless and get an error asking for ALLOW FILTERING, then add it and ship a query that scans the whole cluster. Interviewers ask this to see whether we can read a key and predict which queries it supports. In this answer, we will take the key apart, see where a chat's messages are stored and in what order, go through the efficient queries, look at the ones Cassandra refuses and why, and see how to serve those properly.

1Taking the Primary Key Apart

The primary key here is ((chat_id), sent_at). The inner parentheses hold the partition key, and everything after it is a clustering column. Together they identify one row uniquely, so one message is one row: a given chat at a given time.

A timeuuid is a version 1 UUID that contains a timestamp, so it sorts by time and is still unique even if two messages arrive in the same millisecond. That is why it suits sent_at better than a plain timestamp, which could collide and silently overwrite a message.

2Where the Partition Lives

The partition key is what Cassandra uses to place data. The default partitioner, Murmur3Partitioner, hashes chat_id into a 64-bit token, and each node owns ranges of tokens on a ring. With a replication factor of 3, the node owning the token and the next two replicas in the ring (by data center and rack rules) store a copy of the partition.

So all messages of one chat, however many there are, live together on the same three nodes, and messages of different chats are spread across the cluster. Any query that includes chat_id = ? can go straight to those replicas.

3How Rows Are Ordered Inside the Partition

Inside a partition, rows are stored on disk sorted by the clustering columns. CLUSTERING ORDER BY (sent_at DESC) means the newest message comes first, which matches what a chat screen shows. Reading the latest messages is therefore a read of the first few rows of the partition, with no sorting at query time.

Partition chat_id = 7a1e... stored on disk
sent_at (timeuuid, DESC)senderbody
2026-11-06 10:15:03mayasee you at 6
2026-11-06 10:14:51raviworks for me
2026-11-06 10:12:40mayadinner tonight?
2026-11-05 21:02:10ravithanks!
One partition per chat, rows sorted newest first by the clustering column.

4Queries the Key Serves Well

Here are the queries this table was designed for.

SQL
-- latest 50 messages of a chat: one partition, first 50 rows
SELECT * FROM messages WHERE chat_id = ? LIMIT 50;
-- older messages for scrolling back: a range inside one partition
SELECT * FROM messages WHERE chat_id = ? AND sent_at < ? LIMIT 50;
-- messages since the client last synced
SELECT * FROM messages WHERE chat_id = ? AND sent_at > maxTimeuuid('2026-11-06 10:00:00+0000');
-- oldest first, by reversing the stored order
SELECT * FROM messages WHERE chat_id = ? ORDER BY sent_at ASC LIMIT 50;

Each one names the partition, so it touches only that chat's replicas, and then reads a contiguous slice of sorted rows. That is the fastest kind of read Cassandra does. Notice that ORDER BY is allowed only on clustering columns and only in the stored order or its exact reverse.

5Queries Cassandra Refuses

Now the queries that look reasonable but do not match the key.

SQL
SELECT * FROM messages WHERE sender = 'maya';
-- InvalidRequest: ... Cannot execute this query as it might involve data filtering
-- and thus may have unpredictable performance. If you want to execute this query
-- despite the performance unpredictability, use ALLOW FILTERING
SELECT * FROM messages WHERE sent_at > maxTimeuuid('2026-11-06 00:00:00+0000');
-- same error: no partition key, so every partition would be read

Without chat_id, Cassandra does not know which nodes to ask. It would have to contact every node, read every partition and filter rows, which on a table of billions of messages is a full cluster scan. Rather than do that silently, Cassandra refuses and makes us say ALLOW FILTERING explicitly. sender is not part of the key at all, and sent_at is only meaningful within a partition, so neither can locate data on its own.

6Why ALLOW FILTERING Is a Warning Sign

ALLOW FILTERING is acceptable in a few narrow cases: inside a single partition, where filtering a few hundred rows is cheap, or for a one-off admin query on a small table. In production on a large table it means a query will get slower as data grows until it times out.

The Cassandra answer is to design another table for that query and write to both. If we need messages by sender, we create messages_by_sender with PRIMARY KEY ((sender), sent_at, chat_id), and the application inserts each message into both tables. Storage is cheap; scanning a cluster is not.

7Follow-up questions

What happens to a partition that grows to millions of messages?

A partition is stored on the same replicas and is read, compacted and repaired as a unit, so a very large partition, hundreds of megabytes or more, makes reads slow, puts pressure on compaction and the heap, and creates a hot spot on its three nodes. A busy group chat could reach that. The fix is to bound partitions by adding a time bucket to the partition key, such as PRIMARY KEY ((chat_id, month), sent_at), so each partition holds one month of one chat, and the client reads the current month first and moves to earlier months when the user scrolls back.

Is a secondary index on sender a good alternative?

A secondary index avoids a second table, but a query by sender alone still has to ask many nodes, because the index is local to each node. It can work for low-volume queries combined with the partition key, but for a frequent query a dedicated table is predictable and faster.

Why use timeuuid instead of timestamp for sent_at?

Two messages in the same chat with the same millisecond would have the same primary key, and in Cassandra an insert with an existing key is an overwrite, so one message would vanish. A timeuuid adds uniqueness while keeping time order.

8Conclusion

In this answer, we saw that chat_id is the partition key, hashed by Murmur3 to choose the replicas that store a chat's messages, and that sent_at is a clustering column that sorts rows inside the partition, newest first. Queries that give chat_id, with an optional range on sent_at, ORDER BY in stored or reverse order and LIMIT, are efficient. Queries by sender or by time across chats would scan every partition, so Cassandra requires ALLOW FILTERING, and the right fix is a second table keyed for that query.

What to say in the interviewAbout 80 seconds aloud

Open with

The key has two parts. chat_id in the inner parentheses is the partition key: Cassandra hashes it with Murmur3 into a token, and the nodes owning that token range store the whole partition, so all messages of one chat live together on the same replicas. sent_at is a clustering column: inside the partition rows are kept sorted by it, here descending, so the newest message is first.

Then, if they want more

And since it is a timeuuid, two messages in the same millisecond do not collide. Efficient queries name the partition: WHERE chat_id equals something with a LIMIT for the latest messages, or with a range on sent_at for scrolling back or syncing, and I can order ascending as the exact reverse.

What Cassandra will not run without ALLOW FILTERING is anything that does not give chat_id, such as WHERE sender equals maya, or a range on sent_at across all chats, because it would have to read every partition on every node.

ALLOW FILTERING in production on a big table is a warning sign; the proper fix is another table designed for that query, like messages_by_sender, written alongside this one. I would also watch partition size, and bucket by month if some chats grow huge.

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.
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, explain this table's primary key:": https://scrivio-interview-library.netlify.app/databases/cassandra-and-dynamodb/e93b105369/