← All lessons

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.

20 min readUpdated November 2026Free

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

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 is appended to the commit log on disk and inserted into the memtable in memory, then acknowledged. When the memtable fills it is flushed as an immutable sorted SSTable. Background compaction merges several SSTables into one, dropping overwritten and deleted values. writeMEMORYDISKmemtablesorted, in RAMcommit logappend-onlyflushSSTable 1SSTable 2SSTable 3compactionone bigger SSTableold versions droppedAcknowledged after the log append + memtable insert.No read, no seek, no in-place update.Reads check memtable + SSTables (Bloom filters skip most).
The LSM write path: cheap appends now, merging later.

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.

The point

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);

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).

Where candidates lose it

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:

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.

Say it like this

“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:

The follow-ups, and the one-line answers

  1. “Why are writes fast?” Append to commit log and memtable; no read, no in-place update.
  2. “How do you query by a different field?” A second table keyed by it, written alongside.
  3. “What happens with an unbounded partition?” It grows until reads and compaction suffer; add a time bucket to the partition key.
  4. “Is it consistent?” Tunable: QUORUM reads and writes overlap; ONE is fast and may be stale.
  5. “Why not use it as a queue?” Deletes create tombstones, and reads scan past them.
  6. “How do you enforce a unique username?” A lightweight transaction, IF NOT EXISTS, on a table keyed by username — and only there.