MVCC: LSM vs B-Tree, Side by Side
The appendix. This part puts the two families side by side on the one axis where they look most similar and are in fact most different — MVCC. The parts from In-memory databases to Query execution leave OLTP entirely: what changes when RAM is the primary store, and what a storage engine looks like when the query is a full-table scan rather than a point lookup.
SSTables and MVCC are different layers
The "each key appears once" rule of the SSTables and sparse indexes part is about the file format after compaction. It has nothing to do with MVCC. MVCC is a separate layer that may or may not sit on top of an SSTable engine.
Basic LSM engines (LevelDB, basic Cassandra)
No MVCC. "Latest wins" — reads always return the newest version. Multiple versions do exist temporarily across SSTables before compaction, but that's an artifact of append-only design, not intentional multi-versioning. Compaction collapses them. No xmin/xmax, no snapshots, no "which version is visible to my transaction."
RocksDB — lightweight MVCC via sequence numbers
Every write gets a monotonically increasing sequence number. A snapshot pins the current one; reads see only keys with seq ≤ the snapshot. Compaction respects pinned snapshots — it won't drop the seq=100 version while a reader holds a snapshot at seq=150.
Write key=age, value=25, seq=100
Write key=age, value=26, seq=200
Snapshot at seq=150: reads age=25
Snapshot at seq=250: reads age=26Why a single number is enough — no xip[] equivalent needed
PostgreSQL's visibility check needs three pieces (xmin, xmax, xip[]) because it must handle transactions that get an XID when they start but may commit — in any order — much later, possibly while others are in flight. That gap between "XID assigned" and "committed" is exactly what xip[] is for: it lists the specific in-progress XIDs to treat as invisible even though they fall inside the xmin–xmax range.
RocksDB has no such gap. Writes aren't concurrent uncommitted transactions competing over a shared range of numbers — they're serialized through a single write path (conceptually one writer, even though RocksDB internally batches concurrent callers via a leader/follower write-thread scheme). A sequence number is handed out only at the moment a write is fully applied to the memtable — so the instant a seq exists, it is already committed and visible. There is no state where seq=150 exists uncommitted while seq=160 is already committed. A plain
seq ≤ snapshotcomparison is therefore a complete visibility check.
| PostgreSQL | RocksDB | Basic LSM | |
|---|---|---|---|
| Multiple versions | Heap pages — multiple tuples per row | Multiple SSTable entries with different seq numbers | Temporarily across SSTables; no intentional versioning |
| Visibility | Transaction snapshot (xmin/xmax/xip[]) | Sequence-number comparison | Always latest |
| Purpose | Full ACID transaction isolation | Consistent point-in-time reads | N/A |
| Cleanup | VACUUM | Compaction (respects pinned snapshots) | Compaction |
The Invariants
- Guarantees: Both mechanisms give a read a stable point-in-time view without locking, and both defer cleanup of superseded versions until no snapshot can see them.
- Does not guarantee: Equivalence. RocksDB's sequence numbers give consistent reads, not full ACID transaction isolation — there is no notion of a transaction that assigns an identifier, does work, and then aborts.