Anatomy of a Memtable
A companion to 04-write-path.html, which treats "write to memtable" as a single step (Stage 2 — Memtable). This zooms into that step: what the memtable actually contains, how it's structured, and why that structure is the real reason Cassandra is called "wide-column." The consequence worth internalizing is that the memtable is a nested tree keyed by (partition, clustering key, column name) — and a flush freezes that nesting onto disk rather than flattening it, which is why one partition ends up spread across many files.
Where It Sits
A write only reaches the memtable after the coordinator hashes the partition key to find the owning node, and that node appends the write to its commit log.
The memtable is per-table, per-node, in-memory. There's one for messages, a separate one for every other table, all living in the JVM heap of that replica.
- Guarantees: Anything in the memtable is already in that node's commit log — the memtable is never the only copy of an acknowledged write on the replica.
- Does not guarantee: Any sharing across tables or nodes. Each table has its own memtable on each replica; nothing in one node's memtable is visible to another node.
What's Actually Inside It
The memtable is a sorted map of partitions. Each partition is a sorted map of rows (by clustering key). Each row is a set of cells — one per column, each carrying its own value and its own timestamp. Nothing is a fixed-width tuple, and a column that was never written simply has no cell — it costs nothing.
Schema-Defined, but Sparse
In CQL (what CREATE TABLE above gives you), you can't write to a column that doesn't exist in the schema — unlike the old Thrift/BigTable-era Cassandra, where a row could carry any arbitrary, undeclared column name at write time. You'd have to ALTER TABLE messages ADD reaction_1 text before writing to reaction_1.
But once a column is declared, populating it is still optional and free per row: you could declare 100 extra columns and a row that only ever writes author and content pays nothing for the other 98 — no padding, no wasted space, no fixed per-row cost. Different rows in the same partition can populate completely different subsets of the declared columns.
That per-row sparseness — not unlimited undeclared column names — is the wide-column property CQL Cassandra actually gives you today.
Three Orderings, Stacked
Two different orderings are stacked on top of each other here, and conflating them is the most common source of confusion:
| Level | Sorted by | Why |
|---|---|---|
| Partition → partition | token (hash of channel_id) | Even distribution across nodes. channel_id=42 and channel_id=43 land in unrelated places — you cannot range-scan across partition keys. |
| Row → row (within a partition) | clustering key (message_id), real order | Fast contiguous range scans within one partition — "last 100 messages in this channel" is a sequential read, not a scatter. |
| Cell → cell (within a row) | column name | Each column is independently addressable, independently timestamped. |
- Guarantees: Unwritten declared columns cost nothing per row, and every cell carries its own timestamp, so columns of the same row are reconciled independently.
- Does not guarantee: Writes to undeclared columns (CQL needs an
ALTER TABLEfirst), or any meaningful order between partitions — adjacentchannel_ids are not adjacent anywhere.
How It Fills Up, Write by Write
Three writes land in sequence on the same partition. The third is an UPDATE to a cell that already exists.
Because the memtable is a live, mutable structure, write 3 reconciles immediately — the content cell is simply replaced by whichever write carries the higher timestamp. There's no duplicate sitting around. The author cell is untouched.
- Guarantees: Within one memtable, each cell holds at most one version — the highest-timestamped write it has seen.
- Does not guarantee: That older versions are gone from the node. A version already flushed to an earlier SSTable is untouched by this in-memory replace.
Flush: The Structure Gets Frozen, Not Flattened
When the memtable fills up, its entire nested shape — partitions, rows, cells, timestamps — is written out in one sequential pass to a new SSTable. The nesting doesn't change; it's now just immutable and on disk.
The catch: every flush produces a separate SSTable, so the same partition can end up with cells scattered across several files.
Reading channel 42 now means merging its cells across all three files, keeping the highest timestamp per cell and discarding the rest. That merge cost is exactly what compaction exists to reduce, by periodically rewriting overlapping SSTables into fewer, consolidated ones (see Part 6 — Compaction).
- Guarantees: The flushed file keeps the exact nesting and ordering of the memtable — partitions by token, rows by clustering key, cells with their timestamps — written in one sequential pass.
- Does not guarantee: That a partition lives in one file. Until compaction consolidates them, every flush that touched partition 42 is another file a read must merge.
Read Path: "Last 100 Messages in Channel 42"
This is the payoff for the whole structure — the query never scans anything it doesn't need. It walks straight down the same nesting the write path built.
hash(42) → token 7F3A…→ this replica owns it, no other node is contacted.- On this node: skip every other partition — jump straight to partition
42in the memtable and in each SSTable's partition index. - Within partition
42, rows are already sorted bymessage_id— take the last 100 directly, no re-sorting needed. - For each row, merge its cells across the memtable + SSTable-001/002/003, keeping the highest timestamp per cell.
- Return 100 reconciled rows to the coordinator → client.
- Guarantees: A clustering-range query within one partition is a contiguous traversal of already-sorted rows — no sort, no scan of other partitions.
- Does not guarantee: A single source per row. Each returned row is a merge over the memtable plus every SSTable holding part of partition 42, so read cost grows with fragmentation.
Is the Memtable a Tree?
Yes, at every level — but it's a nested tree of sorted maps, not one single tree over the whole dataset.
| Top level (partition → partition) | A concurrent, sorted, tree-like map keyed by token. Cassandra's actual implementation uses a skip list here — a probabilistically-balanced structure that gives you O(log n) sorted lookups/inserts under heavy concurrent writes, without the lock contention a classic balanced BST would need. |
|---|---|
| Inside each partition (row → row) | A separate sorted tree keyed by clustering key, scoped to just that partition. This is what makes "last 100 messages in this channel" a cheap traversal of one small tree instead of a scan of everything. |
| Inside each row (cell → cell) | A small sorted structure keyed by column name. |
So "self-balancing tree" is the right instinct — it's just three of them, nested, where the outer one is keyed by token and each of its entries opens into its own inner tree keyed by clustering key.
That nesting is the wide-column model: the tree's key isn't just "row id," it's
(partition, clustering key, column name), all the way down.
Multiple Roots, Not One Continuous Tree
More precisely: this is one root at the top, plus a separate independent root per partition — not one giant merged tree.
The outer skip list has a single entry point that holds all partitions currently in the memtable, sorted by token. But each entry in that skip list doesn't inline its rows — it holds a pointer to a wholly separate tree, built just for that partition's rows, with its own root:
So: 1 root for the outer structure, plus N independent roots — one per partition currently held in the memtable. They aren't nodes of one giant merged tree; they're separate tree objects the outer structure happens to reference, one hop down.
- Guarantees: A write to one partition rebuilds only that partition's row-tree; readers and writers of every other partition never contend with it.
- Does not guarantee: Contention-free writes within a single hot partition — all its writers race to replace the same root.