Key technologies
Cassandra
A write-optimised, leaderless wide-column store that is very good at exactly the queries you designed it for and bad at every other one. The data model is the whole interview.
Cassandra shows up in interviews whenever the numbers get big: “a billion messages a day”, “every sensor reading forever”, “a timeline for 500 million users”. It is a good answer to those — if you can model the data for it. Candidates who say “Cassandra, because it scales” and then sketch relational tables are the ones who get the follow-up they can’t answer.
The one sentence to carry in: in Cassandra you design tables for queries, not for entities.
What Cassandra actually is
- Leaderless: every node can take any read or write. There is no primary and no failover.
- Partitioned by consistent hashing: each row’s partition key is hashed onto a token ring, and virtual nodes spread ownership. The partitioning lesson has the ring.
- Replicated
Ntimes (the replication factor, usually 3), with per-query tunable consistency. - Log-structured storage: writes are appends, never in-place updates.
ScyllaDB is a C++ reimplementation with the same data model and protocol; everything here applies to it.
The write path, and why writes are cheap
A write goes to the commit log (sequential append, for durability) and the memtable (a sorted in-memory structure), and is acknowledged. That’s it — no read, no seek, no lock. When the memtable fills, it is flushed to disk as an immutable, sorted SSTable. Background compaction merges SSTables, discarding overwritten values and expired data.
This is an LSM tree (log-structured merge tree), the same design as RocksDB, LevelDB, HBase and Bigtable. It trades read cost for write cost: a read may have to check the memtable and several SSTables (Bloom filters skip most of them) and merge the results by timestamp.
Because nothing is updated in place, an update and an insert are the same operation — an upsert with a newer timestamp. A read returns the value with the latest write timestamp, cell by cell. That is why Cassandra doesn’t care whether a row existed, and why clock skew between clients can make an older write win.
The data model: partition key and clustering columns
CREATE TABLE messages_by_conversation (
conversation_id uuid,
sent_at timeuuid,
sender_id uuid,
body text,
PRIMARY KEY ((conversation_id), sent_at)
) WITH CLUSTERING ORDER BY (sent_at DESC);
- The partition key (
conversation_id) decides which nodes hold the data. All rows with the same partition key live together. - The clustering column (
sent_at) orders rows within the partition on disk.
So “the latest 50 messages in conversation X” is one partition, read sequentially from the start. That is the fastest query Cassandra can serve. “All messages sent by user Y” is a query this table cannot answer without scanning every node.
Query-first modelling
The relational habit is one table per entity and joins at read time. Cassandra has no joins, so you write one table per query and duplicate data into each.
| Query | Table | Partition key | Clustering |
|---|---|---|---|
| Messages in a conversation, newest first | messages_by_conversation |
conversation_id |
sent_at DESC |
| A user’s conversations, most recent first | conversations_by_user |
user_id |
last_message_at DESC |
| Messages a user sent (moderation) | messages_by_sender |
sender_id, day |
sent_at |
A new message is written to every table it belongs in. Writes are cheap, so this is fine; keeping
the copies consistent is your job (logged BATCH statements across tables give all-or-nothing
eventually, not isolation).
Secondary indexes and ALLOW FILTERING are not the escape hatch they look like. A secondary
index on a high-cardinality column queries every node; ALLOW FILTERING scans partitions and
discards rows. Both work in a demo with 1,000 rows and fall over in production. If a query matters,
it gets its own table.
Partition sizing: the mistake that hurts later
A partition lives on one set of replicas and is read as a unit. Rules of thumb: keep partitions under ~100 MB and ~100,000 rows.
The classic failure is an unbounded partition: sensor_readings keyed by sensor_id alone grows
forever, and one partition becomes gigabytes that are slow to read, slow to compact and painful to
repair. The fix is a time bucket in the partition key:
PRIMARY KEY ((sensor_id, day), recorded_at)
Now each partition is one sensor-day. A query for a week reads seven partitions. A hot partition key — one celebrity conversation — has the same symptoms, and the same remedy as in the partitioning lesson: split the key.
Tunable consistency
Each query picks a consistency level. With replication factor 3:
| Level | Replicas that must respond | Use |
|---|---|---|
ONE |
1 | Lowest latency; may read stale data |
QUORUM |
2 of 3 | QUORUM writes + QUORUM reads overlap, so reads see the latest acknowledged write |
LOCAL_QUORUM |
2 of 3 in the local data centre | The usual multi-region choice |
ALL |
3 | One replica down fails the request — rarely worth it |
R + W > N is the overlap rule from the replication lesson.
Replicas converge through hinted handoff (a coordinator holds writes for a briefly-down replica),
read repair, and scheduled anti-entropy repair — which you must actually run, or deleted
data can come back.
Deletes are writes: tombstones
A delete writes a tombstone, a marker that shadows older values. It must survive until every
replica has seen it, so tombstones are kept for gc_grace_seconds (10 days by default) and only
purged by compaction after that.
Two consequences:
- Queue-like tables are an anti-pattern. Insert jobs, delete them when done, and every read scans
past thousands of tombstones. Reads slow down and eventually fail with
TombstoneOverwhelmingException. - Repair must run within
gc_grace_seconds. A replica that missed the delete and outlives the tombstone resurrects the data — “zombie” rows.
Lightweight transactions
INSERT ... IF NOT EXISTS and UPDATE ... IF balance = 10 give compare-and-set using Paxos. They are
correct and roughly 4× the round trips of a normal write. Use them for rare uniqueness checks
(claiming a username), not on a hot path.
Cassandra versus the alternatives
| Reach for it when | Avoid when | |
|---|---|---|
| Cassandra / Scylla | Massive write throughput, time series, known queries, multi-region active-active | Ad-hoc queries, joins, transactions, frequently changing access patterns |
| DynamoDB | Same model, fully managed on AWS | You want to self-host or avoid per-request pricing at scale |
| Postgres | Relations, transactions, flexible queries | Sustained writes beyond one primary |
| HBase / Bigtable | Range-partitioned wide columns, Hadoop/GCP ecosystems | You want leaderless multi-region writes |
| ClickHouse | Analytical scans and aggregations over huge event data | Point lookups and frequent updates |
When to propose Cassandra in an interview
When writes are enormous, access patterns are few and known, and you can tolerate eventual consistency or pay for quorums. Messaging, activity feeds, IoT readings, audit logs, user event history.
“Messages go in Cassandra, in a table partitioned by conversation id and clustered by a timeuuid
descending, so loading a chat is one partition read. A second table keyed by user id gives the inbox.
For big group chats I’d bucket the partition by month to keep partitions bounded. Writes and reads at
LOCAL_QUORUM with replication factor 3 per region — a user sees their own message on refresh, and
we survive a node loss without errors.”
Sizing it on the whiteboard
Say 2 billion messages a day, 300 bytes each:
- Write rate: 2B / 86,400 ≈ 23,000 writes/s average; plan for 3× at peak ≈ 70,000/s.
- Per node: a few thousand to tens of thousands of writes/s per node, depending on hardware — call it 10,000 to be safe. With RF 3, every write lands on 3 nodes: 70,000 × 3 / 10,000 ≈ 21 nodes for write throughput alone.
- Storage: 2B × 300 B = 600 GB/day, ×3 replicas = 1.8 TB/day — ~650 TB/year. Keep node density to a few TB each for manageable repairs and you are in the hundreds of nodes for a year of history. That is the moment to talk about TTLs and tiered storage.
The follow-ups, and the one-line answers
- “Why are writes fast?” Append to commit log and memtable; no read, no in-place update.
- “How do you query by a different field?” A second table keyed by it, written alongside.
- “What happens with an unbounded partition?” It grows until reads and compaction suffer; add a time bucket to the partition key.
- “Is it consistent?” Tunable:
QUORUMreads and writes overlap;ONEis fast and may be stale. - “Why not use it as a queue?” Deletes create tombstones, and reads scan past them.
- “How do you enforce a unique username?” A lightweight transaction,
IF NOT EXISTS, on a table keyed by username — and only there.