Projects / Chat store
Cassandra · about 10 hours · 4 phases
Build a chat message store in Cassandra
When we open a chat app and scroll up, the old messages of a room appear in order, newest first, and they load fast even when the room has years of history. A database that has to search for them would get slower as the history grows. Cassandra takes the opposite route: you decide the question first, and it lays the data out on disk so that exactly that question is a quick read. In about 10 hours we will build the answer ourselves: a message store designed around the queries a chat app needs, with rules for what you cannot ask, a second table for a second question, rooms split into day buckets so no partition grows forever, and a check that two copies of the same data agree.
You work in four phases of about 2.5 hours each. Every phase ends with something you can run and see. By the end you will have:
- a room table that returns the newest messages first and refuses questions it was not built for,
- a second table for "all messages by one person", written together with the first,
- a unique username check, messages that expire, and a counter,
- 20,000 messages in day buckets, with the partition you hit shown by its token,
- two tables that drift apart after a delete, and a check that finds it.
You need to know basic SQL. Cassandra's query language, CQL, looks like SQL and is not SQL, and this project is about the differences.
The four phases
Design the table around the question
You end with a table that answers "newest messages in a room" in one read, and an error that shows what it refuses.
- 1.1 A keyspace15 min
- 1.2 The room table30 min
- 1.3 Five messages20 min
- 1.4 Read a room25 min
- 1.5 A question the table refuses20 min
- 1.6 A time range inside a room25 min
- 1.7 There is no update, only write25 min
One table per question
You end with two tables that hold the same messages, written together, plus a unique username, expiring data and a counter.
- 2.1 A table for the second question20 min
- 2.2 Write both together35 min
- 2.3 Read by author20 min
- 2.4 A username can be taken once30 min
- 2.5 Data that expires25 min
- 2.6 A counter25 min
Partitions that do not grow forever
You end with 20,000 messages in day buckets, and a look at where each bucket lives.
- 3.1 Day buckets and 20,000 messages40 min
- 3.2 Read one bucket25 min
- 3.3 The shape of the data20 min
- 3.4 Where a partition lives25 min
- 3.5 Watch a read25 min
- 3.6 What ALLOW FILTERING costs20 min
Deletes, consistency and drift
You end with two tables that disagreed after a delete, and a check that found it and fixed it.
- 4.1 Delete one message30 min
- 4.2 Spot the drift25 min
- 4.3 Fix it20 min
- 4.4 Consistency levels30 min
- 4.5 Read the settings15 min