Append-heavy, time-ordered rows
Billions of rows written in order and read as ranges per key (messages, events, sensor data).
Say it like this
The workload is append-heavy and every read is a range per key, so I use a wide-column store with the conversation as partition key and the sequence as clustering key, which makes each history read one sequential scan.
Basic solution
Use a wide-column store with partition key + clustering key so a range read is one sequential scan.
One query, one table: partition and clustering keys
The partition key picks the node and the clustering key orders rows inside it, so a history read is one contiguous scan. The second query gets its own table written in the same batch.
How it works
- Model each table around one query. The partition key is the column that query filters on exactly (conversation_id); the clustering key is the column it reads in order (seq descending). Together they decide which node holds a row and where in that node's files it sits.
- A write hashes the partition key to pick the owning nodes, appends the row to a commit log on disk, and puts it in an in-memory sorted buffer (the memtable). It is acknowledged once the number of replicas required by the write quorum have done the same.
- When a memtable fills it is flushed to an immutable sorted file (an SSTable); compaction later merges those files in the background so all rows of one partition end up next to each other on disk.
- A read for one partition and a clustering range goes to the owning replicas, merges the memtable with the few SSTables that hold that partition, and returns the rows already in clustering order, so no sort step is needed.
- A query that does not include the partition key has no way to pick a node, so it would have to scan every node. Instead you create a second table keyed the other way and write every row to both.
Why it fits time-ordered data
- Writes are sequential appends to the commit log and the memtable, so a node sustains tens of thousands per second on ordinary disks.
- A partition's rows are stored together in clustering order, so "latest 50 messages" is one contiguous read from one place.
Worked example
Chat message history
- 50M DAU × 40 messages/day = 2B messages/day ≈ 23K writes/s, ~70K/s at peak. Each row ~200 B.
- Table messages_by_conversation: partition key conversation_id, clustering key seq DESC, columns sender_id, body, sent_at.
- Load the last 50 messages: one partition, one range, ~10 KB read from one or two SSTables. Latency a few ms, because the rows are already contiguous and ordered.
- Partition size: a busy group chat with 1M messages is 200 MB, past the ~100 MB comfort limit where reads and compaction slow down, so bucket the partition key as (conversation_id, month). A client paging back crosses buckets only at month boundaries.
- Second query, "my messages across chats", gets its own table messages_by_user keyed by (user_id, sent_at); both tables are written in one logged batch (the store records the batch first, so if one write fails it retries until both have applied) and cannot drift apart.
- Storage: 400 GB/day × 3 replicas = 1.2 TB/day; 90 days hot ≈ 108 TB, so about 60 nodes with 2 TB each before tiering old buckets to object storage.
Variants and when each wins
Cassandra / ScyllaDB (leaderless, tunable consistency)
- Every node accepts writes and you choose how many replicas must acknowledge each one. It wins for multi-region write availability; concurrent updates to one cell resolve by last-writer-wins timestamps.
HBase / Bigtable (single-writer regions, strong per-row consistency)
- Each key range is served by exactly one node at a time, so a read after a write on the same row always sees the write. It wins when you need that per-row guarantee; failover takes seconds while a range moves to another node.
DynamoDB (managed, same partition and sort key model)
- The same key model with no cluster to run and per-request pricing. It wins for small teams; items are capped at 400 KB and each partition throttles at a fixed throughput, so a hot partition hits a hard limit.
Postgres with partitioning
- The table is split by time and indexed on (conversation_id, seq) inside a database you already run. It wins up to a few TB; beyond one node you shard by hand.
Pick Cassandra or Scylla for append-heavy, range-read data at tens of TB; Bigtable or HBase when you need strong per-row reads; Postgres until you actually outgrow it.
Failure modes
Unbounded partition
- One partition grows to gigabytes, reads of it time out, and compaction on that node stalls. It happens because the partition key has no bound on how many rows it collects, such as one group chat that lives forever. Fix it by adding a time bucket to the key, (conversation_id, month), and paging across buckets when a client reads back in time.
Tombstone build-up
- A range read gets slower the more rows you delete, and eventually fails with a tombstone limit error. It happens because a delete writes a marker row that every read has to scan past until compaction removes it. Fix it by using TTLs instead of explicit deletes where possible, deleting whole partitions rather than rows, and tuning gc_grace_seconds, the delay before deleted data is purged, so markers go away sooner.
Query without the partition key
- A new feature needs "all messages by user" and the only table is keyed by conversation. It happens because the table was modelled for one query and this is a different one. Fix it by adding a second table keyed by user and writing both in a logged batch, which guarantees both writes eventually apply; never turn on ALLOW FILTERING in production, because it scans every node.
Write skew across replicas
- Two clients update the same cell and one update silently vanishes. It happens because the store resolves concurrent writes by last-writer-wins on timestamps, and the two writers' clocks disagreed. Fix it by designing rows as immutable appends with a sequence number, or by using lightweight transactions (a conditional write that costs an extra round of coordination between replicas) for the rare true update.
Compaction falling behind
- Read latency climbs and the disk fills with many small SSTables. It happens because rows are being written faster than compaction can merge the files. Fix it with more nodes or faster disks, size-tiered compaction for write-heavy tables, and throttling of bulk loads.
Interviewer follow-ups
Answer out loud before you open each one.
How do you choose the partition key and clustering key?
Partition key is whatever the hot query filters on exactly, so one query hits one partition. Clustering key is the order the query reads in, so the rows come back sorted with no sort step. Then check the partition stays under about 100 MB and a few hundred thousand rows; if not, add a time bucket to the partition key.
Why no joins?
Rows for one partition live on specific nodes; joining to another table means fetching from other nodes for every row, which destroys the single-scan property. Instead you denormalise: write the joined fields into the row, or keep a second table keyed for the other query and write both.
What does a delete actually do?
It appends a tombstone marker; the data stays until compaction after a grace period so that a replica that was down still learns about the delete. Reading through many tombstones is slow, which is why TTLs and partition-level deletes are preferred over deleting rows one by one.
How does a read work when replicas disagree?
The coordinator reads from the number of replicas the consistency level demands, compares timestamps, returns the newest value, and sends a read repair to the stale replica in the background. With quorum reads and writes you always see the latest acknowledged write.
Connections
- In study paths
- Splitting the data
- Built on
- LSM tree
- Pairs with
- Partition the data · Tune reads and writes with quorums · Order events within a stream · Expire data automatically · Write-optimised vs read-optimised storage
- Pulls against
- Default to a relational database · Commit across two stores
- Used in
- Chat App (WhatsApp / Messenger) · News Feed (Twitter / X timeline)
The same parts on other clouds
The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.
| AWS | Used here as | Google Cloud | Microsoft Azure | Open source and others |
|---|---|---|---|---|
| Amazon Keyspaces | Owning replicas | Bigtable | Managed Instance for Apache Cassandra | Apache Cassandra, ScyllaDB |
| Amazon EC2 Auto Scaling | Chat service | Compute Engine managed instance groups | Virtual Machine Scale Sets | Kubernetes Deployment with a HorizontalPodAutoscaler, Nomad |