CockroachDB, on disk

CockroachDB is a distributed SQL database, but each node keeps its share of the data in one ordinary local store: a log-structured merge tree managed by Pebble, a Go storage engine in the LevelDB and RocksDB family. Everything, the SQL rows, the schema, the Raft logs, the cluster's own metrics, is a sorted key with a value. Every example here is read from the sample store, written by CockroachDB v26.4.1.

One sorted keyspace

CockroachDB divides its keyspace into ranges of about 512 MB, each copied to several nodes by Raft. Within a node's store the keys of all its ranges sit in one sorted order. The first byte says what a key is:

First bytePrinted asWhat lives there
01/Local/…State kept beside each range: its Raft log, lease, applied state and descriptor, and the lock table. Never sent anywhere else.
02, 03/Meta1, /Meta2The range addressing records: where each range is, so any node can find any key.
04/System/…Node liveness, ID counters and the time series CockroachDB records about itself (/System/tsd).
88–fd/Table/…SQL data: the table ID, the index ID, then the indexed columns.
fe/Tenant/…The keyspace of a virtual cluster (a tenant), with its own tables inside.

The sample, a single node after a few minutes, has 3,311 distinct keys; most belong to CockroachDB itself, not to the three tables loaded into it (61 artists, 199 albums, 1,557 tracks).

SQL rows as keys

A row's key is /Table/<table ID>/<index ID>/<primary key columns>/<column family>. Every part uses an order-preserving encoding, so comparing the bytes compares the values: integers start with a byte that says their length (0 to 109 fit in one byte, 136 + the value), strings are escaped and end with 00 01, and there are encodings for floats, decimals, timestamps and every other key type. The artists table has ID 106; its primary index is index 1. Artist 1's key:

OffsetPartBytes
0table ID: 106f2
1index ID: 1 (the primary index)89
2id = 1, the primary key89
3column family 088
4a zero byte: the end of the roach key00
5MVCC wall time, nanoseconds since 1970, big-endian18 ed 94 85 59 39 36 30
13the version's length + 1: 900

The first four bytes are the roach key, /Table/106/1/1/0. Pebble stores engine keys: the roach key, a zero byte, then a version and the version's length plus one. For data the version is an MVCC timestamp: 8 bytes of wall time in nanoseconds since 1970, and 4 more for a logical counter when it is not 0. This one is 1796255126.839178800,0, 2026-12-02T23:45:26.8391788Z. CockroachDB's comparator sorts versions of the same roach key newest first, so a read at a timestamp seeks to the key and takes the first version at or before it.

MVCC: every version stays until garbage collection

An UPDATE does not change a value: it writes a new version at a newer timestamp. Artist 1 has 2 versions in the sample:

TimestampValueFile
1796255126.839178800,0name = Artist 1 & Friends, country = NZ000032.sst
1796255126.561985383,0name = Artist 1, country = UK000032.sst

A DELETE writes a version with an empty value, a tombstone. Old versions serve reads at past timestamps (AS OF SYSTEM TIME), backups and changefeeds; garbage collection removes them once they are older than the table's GC TTL (four hours by default). A transaction that has not committed yet writes an intent: a provisional version, plus a lock in the lock table under /Local/Lock that names its transaction.

Values

A value starts with a 4-byte checksum and a tag that says its type. A row's columns are stored as a tuple: for each non-NULL column, a header holding the difference from the previous column's ID and the encoding type, then the data. The newest version of artist 1, 29 bytes:

OffsetPartBytes
0checksum (CRC-32 of key and value)8f 24 ee 39
4tag: 10 = TUPLE0a
5column 2 (name): BYTES "Artist 1 & Friends"26 12 41 72 74 69 73 74 20 31 20 26 20 46 72 69 65 6e 64 73
25column 3 (country): BYTES "NZ"16 02 4e 5a

The id column is not in the value: it is in the key. Integers in values are zigzag varints, strings are a length and the bytes, decimals, timestamps, arrays and JSONB have their own compact forms. The viewer decodes them all; the sample's albums table has a column of most kinds.

Column families

A table can split its columns into families, each stored under its own key: an UPDATE that changes one family writes one key, not the whole row. albums has two, main and extra (its JSON, UUID and bytes columns). Album 1:

KeyColumns
/Table/107/1/1/0artist_id = 2, title = Album 1, year = 1961, price = 9.99, released = 2000-01-04, tags = {jazz}, rating = 5, explicit = f, added = 2024-01-02 03:00:00+00
/Table/107/1/1/1/1sku = 00000000-0000-4000-8000-000000000001

Family 0 is always written, even when all its columns are NULL, so a row exists exactly when its family-0 key does. A family other than 0 adds its length to the key (the /1 after the family ID) and is left out when all its columns are NULL. A secondary index is another set of keys under the same table, /Table/106/2/<country>/<id>/0 for the sample's index on artists.country, with the primary key appended so that every entry is unique.

The schema is in the keyspace too

Nothing outside the store says what table 106 is. system.namespace (table 30) maps each name to an ID, and system.descriptor (table 3) holds a protobuf descriptor for every database, schema and table: its columns with their IDs and types, its families and its indexes. The viewer reads the descriptors from the files and uses them to turn keys back into rows:

ColumnIDTypeFamily
id1INT8main
artist_id2INT8main
title3STRINGmain
year4INT2main
price5DECIMALmain
released6DATEmain
tags7STRING[]main
rating8FLOAT8main
explicit9BOOLmain
added10TIMESTAMPTZmain
info11JSONBextra
sku12UUIDextra
cover13BYTESextra

Pebble's tables

Pebble writes sorted tables (.sst) like LevelDB and RocksDB: data blocks, then index, filter and meta blocks, then a footer. It began with RocksDB's format, and since table format v5 stores blocks by column. CockroachDB v26.4.1 writes table format v8 (tiering metadata). A columnar block has a small header, then each column's bytes together: integers at the narrowest width that fits, often as differences from a base; byte strings as offsets and data; booleans as bitmaps.

For CockroachDB's keys the data block has these columns (the crdb1 key schema): roach key, MVCC wall time, MVCC logical, untyped version, trailer, prefix changed, value, value is external, is obsolete. Roach keys use prefix compression in bundles: a prefix shared by the whole block, one per bundle of 16 rows, and a suffix per row; the versions of one key leave the suffix empty. The first data block of 000032.sst holds 328 rows whose roach keys add up to 13,973 bytes; the column stores them, with its offsets, in 8,323. The timestamps are two integer columns, wall time and logical counter, each stored at the narrowest width that holds every row's value (as differences from a base when that is narrower); a block whose rows have no logical counters stores that column in a few bytes.

Each block is compressed on its own and followed by a 5-byte trailer, a compression byte and a checksum. The sample's blocks are compressed with MinLZ (CockroachDB's Fastest profile), a fast LZ77-style format, and checked with CRC-32C. The footer is 61 bytes: the checksum type, block handles for the metaindex and index, attributes (what kinds of keys the table has), a checksum of the footer, the format version, and the magic number f0 9f aa b3 f0 9f aa b3, two cockroach emoji in UTF-8.

Blob files: values apart from keys

Compaction rewrites data many times on its way down the tree. For large values that is wasted work, so Pebble can write values over a size threshold to separate blob files and keep only a short handle in the table: which blob file, which block, which value. CockroachDB sets the threshold by span: in the sample it is 32 bytes or 256 bytes, depending on the table (it is a property of each table). 928 of the sample's values are in 5 blob files. The MANIFEST records which blob files each table refers to, so a compaction can move the handles without copying the values.

The write-ahead log and the MANIFEST

Writes reach Pebble as batches: CockroachDB applies each committed Raft command as one. A batch is appended to the WAL, then put in the memtable; when the memtable fills, it is flushed to a level-0 table. Pebble's WAL uses LevelDB's record format, with record types that add the log number (Pebble reuses old log files, and the number tells old records from new) and the offset synced so far. The sample's 2 logs hold 23,125 operations, most of them CockroachDB's own time series and Raft state; for 118 keys the newest version is only in a log, not yet in any table, including the last SQL writes before the node stopped.

The MANIFEST is a log of version edits, in the same record format: tables added to and removed from each level, blob files added and removed, the next file number and the last sequence number. Replaying the sample's 5 edits gives 4 live tables, all on L6 after compaction, and 5 blob files. Pebble names the live MANIFEST with a marker: an empty file whose name holds the value, marker.manifest.000002.MANIFEST-000027.

Reading it back

The viewer does all of this in the browser: it replays the MANIFEST, opens each table and blob file, decodes every block and key, adds the WAL's writes, takes the newest version of each key, reads the descriptors, and rebuilds the rows. On the sample, every row it rebuilds matches what cockroach sql returns for the same tables.