Quick revision
The short answer
With PRIMARY KEY (chat_id, sent_at), every message of a chat lands in one partition, so a few huge group chats grow partitions of millions of rows. Reading, compacting and repairing those partitions loads large structures into the JVM heap, which causes long GC pauses and timeouts, and the load concentrates on the replicas that own them. We redesign with a time bucket in the partition key, PRIMARY KEY ((chat_id, day), sent_at), so partitions stay bounded, read the newest buckets first and page backwards, expire old messages with TTLs and time-window compaction instead of deletes, and track partition size against a target in design reviews.
What to remember
In this answer, we saw that a partition key of chat_id alone lets a few huge group chats build partitions of millions of rows, and that reading, compacting and repairing them strains the heap, causes GC pauses and concentrates load on a few replicas. Adding a day bucket to the partition key bounds every partition, readers fetch the newest buckets first and page backwards, TTLs with time-window compaction expire old data without tombstone-heavy deletes, and partition size targets and alerts keep the design honest.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
With PRIMARY KEY (chat_id, sent_at), every message of a chat lands in one partition, so a few huge group chats grow partitions of millions of rows. Reading, compacting and repairing those partitions loads large structures into the JVM heap, which causes long GC pauses and timeouts, and the load concentrates on the replicas that own them. We redesign with a time bucket in the partition key, PRIMARY KEY ((chat_id, day), sent_at), so partitions stay bounded, read the newest buckets first and page backwards, expire old messages with TTLs and time-window compaction instead of deletes, and track partition size against a target in design reviews.
Why this question is asked
Cassandra tables often work perfectly in testing and fail only for a handful of real users, because the problem is not the average partition but the biggest one. Group chats with tens of thousands of members can generate millions of messages, and with this primary key all of them end up in a single partition. Interviewers ask this to check that we understand partition sizing, one of the most important design rules in Cassandra. In this answer, we will see why huge partitions hurt, redesign the table with a time bucket, write the reading code that pages across buckets, deal with tombstones from deleted messages, and set up checks so this does not happen again.
1A Quick Recap of Partitions in Cassandra
In Cassandra a table's partition key decides which nodes store the data, and the clustering columns sort rows within the partition. Here chat_id is the partition key and sent_at is the clustering column. So every message of chat 77 lives in one partition on, say, three replicas, sorted by time. For small chats this is ideal: reading the latest 50 messages is one slice from one partition.
The trouble starts with the biggest chats. Cassandra will accept a partition of any size, but in practice we keep partitions bounded, commonly well under 100 MB and ideally much smaller, and we avoid partitions that grow forever, which is exactly what chat_id alone gives us.
2Why Huge Partitions Cause Timeouts and GC Pauses
Cassandra runs on the JVM. When a read touches a partition, the node uses the partition's index to find the requested slice, and for very large partitions that index data and the rows read are large objects on the heap. Compaction, which merges the SSTable files of a table, must read and rewrite a whole partition at once, and repair, which compares replicas, builds data structures over whole partitions. A partition of millions of rows and several gigabytes turns each of these into a heavy operation that allocates lots of memory, and the garbage collector responds with long pauses. During a pause the node does not answer, so reads time out, even for unrelated small chats on the same node.
The load is also uneven. The three replicas that own a huge chat do far more work than the rest of the cluster, and adding nodes does not split a partition. That is why only a few chats time out while the cluster looks mostly healthy.
Notice the gap between the median and the maximum: typical partitions are tiny, a few are thousands of times larger.
3Redesign: Add a Time Bucket to the Partition Key
We bound each partition by adding a time bucket to the partition key. With a day bucket, each partition holds one chat's messages for one day.
Now a very busy chat with 300,000 messages a day has partitions of 300,000 rows, and a normal chat has a few rows per day. The parenthesised pair (chat_id, day) is the composite partition key, so both values are needed to read. If even one day is too much for the busiest chats, we can choose a smaller bucket, such as an hour, or add a bucket number for chats flagged as large; the point is to choose the bucket so the largest partitions stay within the target.
4Reading the Latest Messages and Paging Backwards
The reader now has to know which buckets to read. Opening a chat reads today's bucket first; if it has fewer messages than the page needs, it continues with yesterday, and so on, up to a limit. Scrolling up passes the oldest sent_at seen as a cursor.
- Read newest bucket (chat_id, day)LIMIT what is still needed
- Have 50 messages?YesReturn page and cursorNoMove to the previous day
- Quiet chat?YesUse chat_active_days to skip empty daysNoRead the next bucket
3 steps in order. Play them, or step through them one at a time.
Notice the limit on empty days. A quiet chat may have no messages for weeks, and reading many empty buckets one by one is slow. A small table chat_active_days with partition key chat_id and one row per day that has messages lets the reader jump straight to the buckets that exist.
5Deleting Old Messages Without Tombstone Trouble
In Cassandra a delete does not remove data immediately. It writes a marker called a tombstone, which hides the old value and is kept for gc_grace_seconds, 10 days by default, so that replicas that missed the delete learn about it during repair. A read that scans over many tombstones has to load and skip each one; Cassandra logs a warning above a threshold, 1,000 by default, and aborts the query above another, 100,000 by default (check your version's settings). A job that deletes old messages row by row in a huge partition is a reliable way to hit those limits.
With day buckets, retention works better. A default TTL makes every message expire on its own, and TimeWindowCompactionStrategy groups data into SSTables by time window, so once every row in a window has expired and gc_grace_seconds has passed, the whole SSTable is dropped at once without merging. If users can delete single messages, that still creates tombstones, but bounded partitions keep the number per read small.
6Preventing It Next Time
The fix should also change the process. In design reviews we estimate the largest partition, not the average one: messages per day of the busiest chat, times row size, times the bucket length, and compare it with a target such as 10 MB or 100,000 rows. In production we alert on the maximum partition size from table histograms and on large-partition warnings in the logs.
The migration itself follows the usual path: write new messages to both tables, backfill old messages into day buckets with a batch job such as one using the Spark Cassandra connector, compare counts per chat, then switch readers and drop the old table.
7Follow-up questions
What causes the GC pauses exactly, and would a different collector fix it?
Large reads, compactions and repairs of huge partitions allocate many large objects quickly, which pushes the collector into long pauses. Modern collectors such as G1 or ZGC reduce pauses, but they do not remove the work, and the replicas stay overloaded. Bounding the partition fixes the cause; tuning the collector only treats a symptom.
How would you migrate without losing messages sent during the backfill?
Start dual writes first, so every new message reaches both tables, and only then backfill history. The backfill can overwrite rows that dual writes already created, which is harmless because Cassandra writes are upserts keyed by the primary key.
8Conclusion
In this answer, we saw that a partition key of chat_id alone lets a few huge group chats build partitions of millions of rows, and that reading, compacting and repairing them strains the heap, causes GC pauses and concentrates load on a few replicas. Adding a day bucket to the partition key bounds every partition, readers fetch the newest buckets first and page backwards, TTLs with time-window compaction expire old data without tombstone-heavy deletes, and partition size targets and alerts keep the design honest.
Open with
The problem is unbounded partitions. With PRIMARY KEY (chat_id, sent_at), every message of a chat goes into one partition, so the largest group chats have millions of rows and gigabytes in a single partition.
Then, if they want more
Reads of those partitions, and compaction and repair, which work on whole partitions, allocate large structures on the JVM heap, which gives long GC pauses, and during a pause the node stops answering, so reads time out. The replicas owning those chats also take far more load, and adding nodes cannot split a partition.
I would redesign the table with a time bucket in the partition key, PRIMARY KEY ((chat_id, day), sent_at) with clustering order descending, so even the busiest chat has a bounded partition per day; if a day is still too big I would use hours for large chats.
Readers query today's bucket first and page backwards day by day, with a small table of active days so quiet chats do not scan empty buckets. For retention I would use a default TTL with TimeWindowCompactionStrategy, so whole SSTables expire, instead of deleting rows and creating tombstones.
Then I would migrate with dual writes and a backfill, and add partition size targets to design reviews and alerts on the maximum partition size.
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
- Redis documentation, MongoDB manual
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 "A Cassandra table messages_by_chat with PRIMARY KEY (chat_id, sent_at) works well for...": https://scrivio-interview-library.netlify.app/databases/nosql-fundamentals/67bd38001c/