Keys and namespaces
The engine stores one flat, byte-ordered keyspace. It has no notion of tables, collections or graphs: every data model is a convention for building user keys and values on top of put, delete, write_batch and scan (source: src/storage/mod.rs, StorageEngine; src/storage/namespaced.rs, module doc). This page describes the internal key the engine adds, then the user-key layouts the upper layers use.
Internal key
Every stored version is keyed by an InternalKey:
encoded internal key
+--------------------+---------------------------+-----------+
| user_key (n bytes) | sequence (u64 big-endian) | op_type |
+--------------------+---------------------------+-----------+
8 bytes 1 byte: 0 = put, 1 = delete
InternalKey::encode concatenates the three parts and InternalKey::decode rejects anything shorter than 9 bytes. The struct derives Ord, so in memory versions sort by user key, then by sequence ascending, then by op type (source: src/storage/mod.rs, InternalKey, encode, decode).
The sequence comes from one engine-wide counter (next_sequence) assigned under the WAL lock, so sequence order is commit order. Higher means newer, and every read path resolves a user key to the highest sequence that is visible to it (source: src/storage/mod.rs, write_batch_local_opts, get, scan).
Encoded keys are compared bytewise, eight bytes at a time (key_cmp). Because the sequence follows the user key directly, a user key that is a byte prefix of another can interleave with it in encoded order. The scan merge detects this and falls back to an order-independent path (source: src/storage/mod.rs, key_cmp, scan).
Stored value
The value of each version is a bincode-serialized StorageValue { value: Option<Vec<u8>>, timestamp: u64 }: an option tag, a u64 length and the bytes when present, then the write timestamp. The timestamp drives MVCC snapshot reads (get(key, Some(ts))), while the sequence decides which version is newest. A delete writes op type 1 with value: None (source: src/storage/mod.rs, StorageValue, StorageValueRef).
The borrowed form StorageValueRef encodes to the same bytes as the owned form. Tested by storage_value_ref_encodes_like_owned.
Database namespaces
PostgreSQL-style databases are separated by a prefix that NamespacedStorage adds on every write and read and strips from scan results:
db:<database>\x1F<logical key>
The separator is the unit separator byte 0x1F. An empty database name and the protocol default "default" both normalize to postgres (source: src/storage/namespaced.rs, NS_SEP, NamespacedStorage::new, normalize_db).
A few keys live outside every namespace and are reached through NamespacedStorage::global:
| Prefix | Content | Written with |
|---|---|---|
__databases__:<db> |
Database registry (backs pg_database); postgres is implicit |
put_local (not replicated) |
__db_dropped__:<db> |
Tombstone that keeps a dropped database from reappearing on reconnect | put_local |
__db_owner__:<db> |
Owning role | put_local |
Registry writes use timestamp 0 (REGISTRY_TS) and are read through scans rather than point gets (source: src/storage/namespaced.rs, register_database, is_dropped, database_owner, REGISTRY_TS).
drop_database scans [db:<db>\x1F, db:<db>\x20) and deletes the keys in batches of 1,000 (DROP_DATABASE_BATCH_KEYS), each batch one write_batch (one Raft entry in cluster mode). It then bumps the catalog epoch, removes the registry and owner entries and writes the drop tombstone (source: src/storage/namespaced.rs, drop_database).
Writes under __catalog__: or __sequence__: advance the engine's catalog epoch, which invalidates memoized catalog state (source: src/storage/namespaced.rs, bumps_catalog_epoch, catalog_memo).
NamespacedStorage also carries the DDL undo log: while recording is on, each put or delete first saves the key's prior value once per savepoint level, so ROLLBACK can restore catalog keys and rewritten rows that DDL wrote directly to storage (source: src/storage/namespaced.rs, DdlUndoLog, record_priors).
Tested by isolates_same_logical_key_across_databases, scan_only_sees_own_namespace_and_strips_prefix, database_registry_create_list_drop, drop_database_purges_more_keys_than_one_batch, dropped_database_is_tombstoned_until_recreated, dropped_namespace_data_purged_across_flush_compaction_and_restart, point_get_and_scan_agree_after_drop_recreate_churn and default_and_empty_collapse_to_postgres (src/storage/namespaced.rs, tests).
Data directories written before namespacing existed hold keys without the prefix; the module doc says they are orphaned and must be reloaded (source: src/storage/namespaced.rs, module doc).
Relational rows
Inside a namespace, a SQL row is stored at <table>:<suffix>, with the encoded row as the value (source: src/query/engine.rs, pk_storage_key, seq_storage_key_for).
| Table shape | Suffix |
|---|---|
| Single-column integer primary key | The value shifted by 2^63 into u64, as 20 zero-padded decimal digits (encode_pk_int) |
| Text / varchar / char primary key | The string as is |
| UUID primary key | Its canonical text form |
| Boolean primary key |
0 or 1
|
| Date primary key | Day number + 10^10, 10 zero-padded digits |
| Timestamp primary key | Value + 10^17, 20 zero-padded digits |
| Composite primary key | For each member, <byte length>:<encoded value> concatenated (encode_key_tuple) |
| No primary key, or a member with no key encoding (float, decimal without marker, bytes, arrays, …) | Hidden row id: counter value + 2^63, 20 zero-padded digits |
(source: src/query/engine.rs, encode_pk_value, encode_pk_int, zero_padded_20, encode_key_tuple, composite_pk_storage_key, seq_storage_key_for)
The integer shift makes signed values sort correctly as text, and hidden row ids use the same encoding, so a table loaded in id order already sits at the keys ADD PRIMARY KEY (id) would give it. Hidden row ids come from the counter __rowseq__:<table>, reserved in blocks for multi-row inserts (source: src/query/engine.rs, seq_storage_key_for, reserve_row_sequences). A single-column NUMERIC, DECIMAL or float primary key is keyed by value only when the table carries the marker __catalog__:pknum:<table>, written at CREATE TABLE; older tables keep hidden row ids (source: src/query/engine.rs, numeric_pk_marker_key, numeric_pk_keyed).
Schema and counters live beside the rows in the same namespace, under prefixes such as __catalog__:, __sequence__: and __autoincrement__: (source: src/storage/namespaced.rs, module doc). The catalog chapters of this volume describe those records.
Other data models
| Model | Key layout | Value | Source |
|---|---|---|---|
| Property graph (Cypher) | Rows of the internal tables __graph_nodes__ (_id, _labels, _properties) and __graph_edges__, stored as relational rows |
Row encoding |
src/query/cypher/planner.rs, node_to_insert
|
| MongoDB documents |
mongodb:<db>:<collection>:<_id>; __collection_metadata__ as _id marks an existing collection |
bincode BsonDocument
|
src/protocol/mongodb/commands.rs, storage_key_with_db, load_documents_from_storage
|
| Redis, database 0 | __redis__:<key> |
Redis value record |
src/protocol/redis/commands.rs, redis_storage_key
|
| Redis, database N > 0 |
__redisdb__:<NN>:<key> (two digits) |
Redis value record |
src/protocol/redis/commands/persist.rs, value_storage_key
|
| Redis streams | __redisxs__:<NN>:<key> |
Stream record |
src/protocol/redis/commands/persist.rs, stream_storage_key
|
| Redis operation log | __redisop__:<gen 16 hex><seq 16 hex> |
Operation |
src/protocol/redis/commands/persist.rs, op_storage_key
|
| Memcached | __memcached__:<key> |
Entry encoding |
src/protocol/memcached/mod.rs, storage_key
|
| InfluxDB points | __influx__:pt:<db>\x1F<measurement>\x1F<tag set>\x1F<timestamp as decimal text> |
Version byte 1, then each field sorted by name: u32 BE name length, name, type tag (f, i, b, s) and big-endian value |
src/protocol/influxdb/mod.rs, point_key, point_entry
|
| InfluxDB databases / retention policies |
__influx__:db:<db>, __influx__:rp:<db> (JSON) |
src/protocol/influxdb/mod.rs, DB_PREFIX, rp_entry
|
|
| Prometheus |
__prom__:d:... (samples), __prom__:m:... (metadata) |
Tagged float or histogram |
src/protocol/prometheus/persist.rs, DATA_PREFIX, META_PREFIX
|
| Elasticsearch documents | __es__:doc:... |
src/vector/sources.rs, ES_DOC_PREFIX
|
|
| Vector collections | Catalog __vector__:c:<db>\x1F<name>; vectors __vector__:v:<db>\x1F<name>\x1F<source><id>
|
u32 LE dimension, f32 LE components, JSON payload |
src/vector/mod.rs, catalog_key, vector_key, encode_entry
|
| Cassandra schema |
__catalog__:cqlks:..., __catalog__:cqltable:...
|
src/protocol/cassandra/schema.rs |
|
| SCRAM credentials | __auth__:scram:... |
src/auth/scram.rs, KEY_PREFIX
|
The MongoDB, Redis, Memcached and InfluxDB handlers hold an Arc<StorageEngine> directly, not a NamespacedStorage, so their keys are not under a db: prefix (source: src/protocol/mongodb/commands.rs, storage field; src/protocol/redis/commands.rs, storage field; src/protocol/memcached/mod.rs; src/protocol/influxdb/mod.rs, with_storage). This matches the guide's statement that MongoDB documents are not SQL relations (How it works).
The InfluxDB handler migrates points stored in the older __ts__:meta:, __ts__:data: and __ts__:ds: layout (written by TimeSeriesStore) to the __influx__:pt: layout on load, then deletes the old keys in batches of 10,000 (source: src/protocol/influxdb/mod.rs, migrate_legacy_format). See Columnar projections, time series and Parquet.
Not verified: the per-model layouts in the table above are read from the code; this chapter found no test that pins their byte format, apart from the namespace tests listed earlier. The wire chapters cover behaviour at the protocol level.