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.

A client writes channel_id 42, message_id 1001, author satyarth, content hello world. The coordinator hashes 42 to a token to find the owning replica. On that replica the write is first appended to the commit log, sequentially, and only then inserted into the memtable: in memory, sorted, one per table, in the JVM heap.
Hash to find the owner, append to the commit log, then land in the memtable

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.

The memtable for the messages table is a sorted map of partitions, ordered by token of channel_id: partition 42 with token 7F3A comes before partition 99 with token C112. Each partition is a sorted map of rows ordered by message_id: partition 42 holds rows 1001 and 1002, partition 99 holds row 4001. Each row is a set of cells, one per written column, each with its own value and timestamp. Row 1001 has author at T1 and content at T2 but no edited_at cell, because that column was never written, and it costs nothing.
Partitions by token, rows by message_id, one timestamped cell per written column

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:

LevelSorted byWhy
Partition → partitiontoken (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 orderFast 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 nameEach 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 TABLE first), or any meaningful order between partitions — adjacent channel_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.

Row 1001 in the memtable after each of three writes. After write 1, an insert, it has an author cell at T1 and no content cell yet. After write 2 it gains a content cell, hello world, at T2. Write 3 updates content to hello world! at T5; because T5 is greater than T2 the T2 cell is replaced in place and no duplicate is kept. The author cell at T1 is never touched.
In the live memtable an update replaces its cell — higher timestamp wins, no duplicate

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.

Three flushes produce three immutable SSTables: SSTable-001 at 10:02 holds partitions 42, 7 and 88; SSTable-002 at 10:14 holds 42 and 15; SSTable-003 at 10:31, the memtable from section 3, holds 42 and 99. Partition 42 appears in all three. Reading channel 42 means merging its cells from all three files and keeping the highest timestamp per cell.
Every flush is a new file, so partition 42 ends up in all three

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.

  1. hash(42) → token 7F3A… → this replica owns it, no other node is contacted.
  2. On this node: skip every other partition — jump straight to partition 42 in the memtable and in each SSTable's partition index.
  3. Within partition 42, rows are already sorted by message_id — take the last 100 directly, no re-sorting needed.
  4. For each row, merge its cells across the memtable + SSTable-001/002/003, keeping the highest timestamp per cell.
  5. 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:

The memtable’s outer structure is one skip list with a single head, holding every partition in token order: 42, 7, 88, 99, with sparser express levels above the base level. Each skip-list entry does not inline its rows; it points to a separate row-tree with its own root, keyed by message_id. So there is one root for the partition index plus one independent root per partition, not one merged tree.
One skip-list root for partitions, then a separate row-tree root per partition

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.