LearnThatStack Ace your next interview
Cassandra · question
Question 4 of 41

What is the difference between a partition key and clustering key?

beginner
← All Cassandra questions
Re-explain
Visual ↓

The partition key picks a row's partition and the nodes that store it, and the clustering key sorts the rows inside that partition. Cassandra hashes the partition key to pick the replica nodes, and every row with the same partition key lives in one partition.

CREATE TABLE events (
    user_id UUID,
    event_time TIMESTAMP,
    event_type TEXT,
    data TEXT,
    PRIMARY KEY (user_id, event_time, event_type)
);

Here user_id is the partition key, and event_time and event_type are clustering keys. All events for one user are stored together, sorted by time and then by type.

An efficient query gives the full partition key, because by default CQL refuses a query that would scan every partition. It can then restrict the clustering keys in the order they are declared, such as every event after a given time. You fix the clustering order when you create the table and can't change it later.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
See it - interactive diagram
Partition key groups rows, clustering keys sort them
CREATE TABLE events (
user_id UUID,
event_time TIMESTAMP,
event_type TEXT,
data TEXT,
PRIMARY KEY (user_id,
event_time, event_type)
);
INSERT INTO events (user_id,
event_time, event_type)
VALUES (?, ?, ?);
SELECT * FROM events
WHERE user_id = ?
AND event_time > ?;
SELECT * FROM events
WHERE event_time > ?;
Partition u1hash(u1) picks its nodes
9:00 view10:00 buy
Partition u2hash(u2) picks its nodes
9:00 buy
Console
1step

user_id is the partition key, so each user's rows share one partition. The clustering keys, event_time then event_type, sort the rows inside it.

1 / 6

The partition key decides which partition and nodes hold a row. The clustering keys sort rows inside it, so one user's time range is one continuous read.

© LearnThatStack - diagrams may not be republished without permission.

Other questions have diagrams like this too. Sign in to ask for one where it's missing - it's free.

Create a free account
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Cassandra cheatsheet.

← Back to all Cassandra questions
Pro · $10/mo

36 of 41 Cassandra answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime