knaxuslearn · build · ship

Projects / Chat store / Phase 3

Phase 3 of 4 · about 2.5 hours

Partitions that do not grow forever

You end with 20,000 messages in day buckets, and a look at where each bucket lives.

One room with ten years of history is one partition with millions of rows. Large partitions are slow to read, hard to repair and a known way to hurt a cluster. The usual fix is to add a bucket to the partition key, here the day.

A day in the partition key keeps each partition small.

Step 3.1: Day buckets and 20,000 messages (about 40 minutes)

The partition key is now two columns, (room_id, day), and every query must name both. The generator on the host writes a CSV file with 20,000 messages over four rooms and five days. Run it, copy the file into the container, then load it with COPY:

bash scripts/cassandra-chat/gen.sh messages.csv
docker cp messages.csv chat-cassandra:/tmp/messages.csv
CREATE TABLE IF NOT EXISTS chat.messages_by_room_day (
  room_id text,
  day     date,
  sent_at timestamp,
  author  text,
  body    text,
  PRIMARY KEY ((room_id, day), sent_at)
) WITH CLUSTERING ORDER BY (sent_at DESC);
COPY chat.messages_by_room_day (room_id, day, sent_at, author, body) FROM '/tmp/messages.csv' WITH HEADER = false;
SELECT count(*) FROM chat.messages_by_room_day;

The count should be 20,000.

Step 3.2: Read one bucket (about 25 minutes)

SELECT count(*) FROM chat.messages_by_room_day WHERE room_id = 'room1' AND day = '2026-11-03';
SELECT sent_at, author, body FROM chat.messages_by_room_day WHERE room_id = 'room1' AND day = '2026-11-03' LIMIT 3;

Every bucket holds 1,000 messages, so the count is 1,000 and the first rows are the latest of that day.

Step 3.3: The shape of the data (about 20 minutes)

Grouping by the partition key shows all the partitions. Four rooms times five days gives twenty:

SELECT room_id, day, count(*) AS msgs FROM chat.messages_by_room_day GROUP BY room_id, day;

This reads the whole table. It is fine on 20,000 rows and a mistake on two billion.

Step 3.4: Where a partition lives (about 25 minutes)

Cassandra hashes the partition key into a number called a token. Each machine in a cluster owns ranges of tokens, and that is how it knows where a partition lives. Let's list a few partitions and look at the token of one:

SELECT DISTINCT room_id, day FROM chat.messages_by_room_day LIMIT 6;
SELECT token(room_id, day) AS ring_position, room_id, day FROM chat.messages_by_room_day WHERE room_id = 'room1' AND day = '2026-11-01' LIMIT 1;

The token is a big number with no relation to the order of the rooms or days. That is deliberate: the hash spreads partitions evenly over the machines.

The hash of the partition key decides which machine owns it.

Step 3.5: Watch a read (about 25 minutes)

TRACING ON makes Cassandra print the steps it took:

TRACING ON;
SELECT sent_at, author FROM chat.messages_by_room_day WHERE room_id = 'room2' AND day = '2026-11-04' LIMIT 2;
TRACING OFF;

Look at the lines that mention memtables or sstables. A read looks in memory first, then in files on disk, and merges what it finds. A read of one partition touches a small number of places.

Step 3.6: What ALLOW FILTERING costs (about 20 minutes)

Count the messages by user7, who is not part of the key. First without the keyword, then with it:

SELECT count(*) FROM chat.messages_by_room_day WHERE author = 'user7';
SELECT count(*) FROM chat.messages_by_room_day WHERE author = 'user7' ALLOW FILTERING;

The first fails, as in Step 1.5. The second works and returns 400, because every 50th message is from user7. But to find them Cassandra read all 20,000 rows. On a table this small that is fine, and on a big one it is the query that takes a cluster down.

Phase 3 done

Partitions are bounded, you saw where they live, and you know what the escape hatch costs. Next, what happens when you delete.