SSTables and Sparse Indexes

What an SSTable is

SSTable = Sorted Strings Table. Two properties define it:

Keys are sortedin file order
Each key appearsexactly once

It is not a traditional data structure. It is a flat, sequential file on disk: key-value pairs written one after another in sorted order, grouped into blocks.

An SSTable file: key-value records written back to back in sorted key order, each key exactly once, with no slack between them. The writer seals a block once the running size reaches the target (4 KB here), after the record that crossed it, so blocks vary in size. Block 1 holds handbag, handcraft and handiwork, 4,213 bytes, starting at byte 0. Block 2 holds handsome, handy and harbor, 4,600 bytes, starting at byte 4,213. Block 3 holds only hazard, one oversized 6,500-byte record, starting at byte 8,813. More blocks follow.
An SSTable is sorted records packed back to back, sealed into blocks of varying size

Deep dive: why a flat file works — immutability

The tempting answer is "because it's sorted." That's wrong. A new key can land anywhere in a sorted array, so inserting into one is O(n) — everything after the insertion point shifts. What actually avoids that cost is that the file is never mutated after creation:

  • Random-order inserts are absorbed by the memtable, a mutable in-memory structure (red-black tree, skip list) with O(log n) insert at any position.
  • The memtable is flushed once, already sorted, as a single sequential write — not N individual inserts.
  • Compaction merges old SSTables into a new one by merge-sort — a sequential scan, never a random-access insert into an existing file.

Logical offset = byte N of the file's own 0-to-size number line; physical = whichever disk block actually holds that byte. The inode/extent map translates one into the other.

Deep dive: who resolves logical → physical, and where does that mapping live?

The storage engine never touches physical addresses. It only ever issues "read N bytes at logical offset X of this file." Everything below that call belongs to the filesystem:

Four horizontal layers. RocksDB issues pread of 16384 bytes at offset 40960 of one file, which is purely logical: a file and a byte offset. The engine sees nothing below that line. The VFS and page cache split the request into four 4 KB pages of the file, pages 10 to 13, and serve any already-cached page from RAM. The filesystem translates each logical page to a physical block number through the file’s extent map, cached in the inode. The block layer issues the real I/O, one request when the blocks are contiguous.
One pread, four layers — and the engine only ever sees the top one

Two things make this cheap in practice, independent of whatever block size the engine picked:

  • The extent map itself is cached. The first read of any part of a file pulls its logical→physical metadata into RAM (inode cache). Later reads don't re-fetch it — only true cache misses on data pages hit the device.
  • Sequential writes land in contiguous extents. An SSTable is written once, as a single continuous flush. Filesystems optimize allocation for exactly that pattern, handing out one contiguous run of blocks. So a 16 KB engine block spanning four OS pages is very likely physically contiguous too, and the block layer collapses it into one physical I/O.

The engine's block-size choice (4 KB, 16 KB, whatever) only changes how many bytes are requested in one logical read call. It has zero bearing on how those bytes get located on disk — that machinery is identical at any block size and is owned entirely by the filesystem.

That machinery is treated as a black box for the rest of the guide, because the engine genuinely cannot see into it. Appendix A opens it: what an extent map actually is, where on disk it sits, and why it costs almost nothing for a file written the way an SSTable is written.

Sparse indexes: avoiding a full in-memory index

Because the file is sorted, you don't need an offset for every key. Store the offset of only the first key of each block. That is a sparse index.

✗ Full index (hash map) — every key

"handbag"   → byte 0
"handcraft" → byte 120
"handiwork" → byte 240
...100M entries in RAM

✓ Sparse index — one per ~4KB block

"handbag"  → byte 0      (block 1)
"handsome" → byte 4096   (block 2)
"hazard"   → byte 8192   (block 3)
"handcraft"/"handiwork": no entry at all

To find "handiwork": the sparse index says it falls between "handbag" and "handsome", so jump to byte 0 and scan forward through ~4 KB until found (or proven absent).

Record sizeRecords per 4 KB blockIndex size reduction
Small (~80 bytes)~51~51× (100M keys → ~2M index entries)
Medium (~2 KB)~2~2×
≥ 4 KB1None — one index entry per record

The saving comes from how many keys share a block, not from the technique itself. It is most valuable for many small records and close to worthless for large ones. Either way the cost is a bounded sequential scan of at most one block — fast regardless of block count.

Block size is a tunable knob, not a fixed 4 KB constant. ~4 KB is a good default only when records are much smaller than that (tens to a few hundred bytes — short keys, counters, log lines). For larger records, scale the block up to preserve the ratio: with ~2 KB records, a 64 KB block fits ~32 records → ~32× reduction instead of ~2×. The cost is a bigger sequential scan per lookup — still cheap compared to the random seek a full index would have saved you from. Real systems pick block size relative to expected record size to keep this ratio healthy.

Deep dive: what counts as a "block"?

A block is a variable-size chunk of bytes targeting ~4 KB. Pairs are packed sequentially; once the accumulated size crosses the target, the current block is sealed and a new one begins. The boundary is always placed after a complete key-value pair, never in the middle of one — a key and its value always land in the same block.

How is the seal point decided? A simple greedy loop. No lookahead, no bin-packing:

16 KB target; pairs of 2.0, 4.5, 7.2, 5.0 KB

running = 0
+ 2.0 KB  → running = 2.0 KB    (< 16KB, keep going)
+ 4.5 KB  → running = 6.5 KB    (< 16KB, keep going)
+ 7.2 KB  → running = 13.7 KB   (< 16KB, keep going)
+ 5.0 KB  → running = 18.7 KB   (≥ 16KB → seal block here)

The pair that crosses the threshold stays in — never split, never held back — so this block ends up 18.7 KB actual, not 16 KB. Target sizes are triggers for when to seal, not fixed slot sizes; real blocks vary and typically land at or above target (sometimes far above, if one value is huge). Because blocks aren't fixed-size, the file format must record where each one actually ends — a length prefix, or the sparse index's next entry marking the following block's start — so a reader never assumes a block is exactly the target size.

Lookup walkthrough — finding "handiwork"

  1. Check the sparse index (in RAM): "handiwork" falls between "handbag" and "handsome".
  2. Seek to the byte offset of the "handbag" block.
  3. Scan forward within that block until "handiwork" is found, or confirmed absent.
  4. The block is small enough that the scan is negligible next to the seek that got you there.

Block compression

Each block is compressed independently before being written. Saves disk space, reduces I/O bandwidth (fewer bytes transferred per read), and costs CPU on both compress and decompress. Because compression is per-block rather than per-file, a reader can decompress exactly the one block it needs.

The Invariants

  • Guarantees: Sorted access (so range scans work), a RAM footprint proportional to block count rather than key count, and — because the file is immutable — that any reader holding the file sees a stable, self-consistent snapshot of it.
  • Does not guarantee: That a key found here is the current value — a newer SSTable or the memtable may hold a later version. It also does not guarantee one block = one disk I/O, or any particular index-size reduction independent of record size.