Introducing a new way to review your PRs.

Read the announcement

helixdb / helix-db

HelixDB is an OLTP graph database with native vector and full-text search built in Rust on Object Storage.

View on GitHub

27 documents · Updated Nov 23, 2026

docs / storage-encoding-and-migrations

Storage encoding and migrations

The storage encoding boundary defines how graph data, index state, and migration state become durable key/value rows. It keeps old rows readable long enough to move them into the current format, then publishes readiness only after the new rows pass structural and cross-record checks. The main reader and writer lifecycle is described in database-engine, while graph writes and search indexes are covered by graph mutations and transactions and index lifecycle and search. This page focuses on the on-disk contracts those systems protect.

Storage encoding boundary

The storage format has two related parts: a typed key space that gives each row a stable scope and kind, and typed values that describe the data stored under that key. Current index values use a small versioned frame, while graph property rows retain their established archive format. The legacy codecs remain available only to recognize and migrate older rows. References: crates/…/mod.rs• L1-10, crates/…/mod.rs• L1-15, crates/…/mod.rs• L1-5, crates/…/codec.rs• L3-8

Physical key namespaces

Every physical key declares whether it belongs to global database control data, legacy unscoped data, or one tenant. A tenant key starts with the 0xFD tenant envelope and a big-endian 128-bit tenant ID. A global key starts with the fixed 0xFE sentinel and then a global kind. Legacy unscoped keys have no tenant envelope. This explicit marker prevents a tenant ID from being mistaken for a graph or index prefix. References: crates/…/scope.rs• L12-16, crates/…/scope.rs• L67-119, crates/…/managed_index.rs• L1-7, crates/…/global.rs• L18-32

The logical key then identifies the row family. Graph keys use a one-byte prefix followed by fixed-width big-endian IDs where needed. Managed index keys use logical prefix 0x06, a record kind, and the identity, generation, entity, partition, or other fields needed by that row family. Parsers require the known prefix, the expected shape, and no trailing bytes. References: crates/…/data.rs• L25-80, crates/…/data.rs• L136-165, crates/…/managed_index.rs• L125-190, crates/…/managed_index.rs• L328-405

Persisted value framing

Current lifecycle and control values begin with a one-byte framing version and a one-byte value kind. Numeric fields use big-endian bytes. Strings and byte fields carry bounded lengths. Closed enums use explicit tags. Decoding checks the version, value kind, field limits, valid enum tags, and complete consumption of the input before it returns a value. These checks stop a row from being accepted under the wrong record family or from silently ignoring new bytes. References: crates/…/codec.rs• L39-56, crates/…/codec.rs• L98-135, crates/…/codec.rs• L144-235

Graph property rows use the established rkyv archive boundary. Empty properties use an empty row, while non-empty rows preserve the archived property list. Index partition identities use a type-preserving archive of one property value rather than display text. This keeps index identity stable across numeric types and nested values. References: crates/…/row.rs• L28-79, crates/…/property_value.rs• L11-50

Canonical index values

Secondary index values use canonical equality or range forms. Equality rows carry a digest beside the canonical bytes, and range rows carry an ordered representation with its direction. The decoder checks that the digest and canonical value agree. Non-unique equality membership in the current format uses a portable Roaring bitmap; its decoder rejects malformed or trailing data. References: crates/…/codec.rs• L855-903, crates/…/equality.rs• L20-51

Vector metadata has two compatibility boundaries. The current metadata archive keeps its deployed field order, while the legacy decoder reads the older archive and projects out retired controls. Current index records hold the stronger generation and score semantics next to this metadata so old physical bytes do not decide current behavior by themselves. References: crates/…/metadata.rs• L1-7, crates/…/metadata.rs• L16-90, crates/…/metadata.rs• L52-100

Version boundaries

The storage version is a global typed value that records the managed-index format understood by the writer. The current value is 0x0004; a writer can migrate supported older V2 data, rejects a version newer than its own, and rejects a version below the supported migration floor. The value is only one part of the boundary: allocator watermarks and cleanup readiness must also agree with it. References: crates/…/metadata.rs• L9-30, crates/…/bootstrap.rs• L84-108, crates/…/repository.rs• L147-205

Readiness markers

Readiness markers record durable completion of major format steps. They are not treated as independent switches. The accepted order is graph rewrite ready, index migration ready, then full storage schema complete. A missing marker means work is incomplete. A malformed marker or a marker that skips an earlier step makes startup fail instead of treating uncertain state as resumable work. References: crates/…/mod.rs• L1298-1354

The complete marker is published only after graph and index readiness exist and no legacy dynamic index definitions remain. The final write is checked again in a serializable transaction. This makes the marker a statement about the data state rather than a best-effort record of an attempted migration. References: crates/…/mod.rs• L1357-1415

Legacy compatibility

The legacy catalog stores dynamic index identities in metadata key names and definitions or tombstones as JSON values. Migration code decodes those rows, validates that the key identity agrees with the value identity, converts them to current validated definitions, and then lets the normal index lifecycle create or converge current generations. References: crates/…/index_catalog.rs• L1-17, crates/…/index_catalog.rs• L72-127, crates/…/mod.rs• L1549-1575

Legacy tenant keys use a markerless [tenant ID][logical key] envelope. The current form adds the 0xFD marker before that tenant ID. Operation values receive an extra compatibility pass because their stored cursors can point at old physical keys; other values can move without changing their bytes when their value format is already current. References: crates/…/tenant_envelope.rs• L8-38, crates/…/envelope.rs• L291-333

Startup migration path

Writer startup performs the migrations that must be complete before normal writes can use the current format. It first checks the durable bootstrap tuple, moves tenant keys, converges managed-index storage, and reconciles lifecycle operations before returning a writer. Runtime-dependent index work then finishes before the database handle is ready for normal use. References: crates/…/pipeline.rs• L10-29, crates/…/bootstrap.rs• L23-36

The startup sequence is ordered so that each later step can rely on the prior boundary:

flowchart TD
    A[Open storage] --> B[Validate global version and watermarks]
    B --> C[Move legacy tenant keys]
    C --> D[Converge graph rows and indexes]
    D --> E[Materialize legacy vector properties]
    E --> F[Reconcile legacy definitions]
    F --> G[Remove obsolete physical rows]
    G --> H[Publish readiness markers]
    H --> I[Reconcile lifecycle operations]
    I --> J[Start normal writer]

Bootstrap validation

Bootstrap accepts either a fully absent current tuple or a complete, valid tuple. On a new store it writes the current storage version, logical index watermark, vector physical watermark, and current cleanup readiness in one transaction. On an existing store it selects migration or cleanup from the stored version and readiness markers. Partial current state stops startup. References: crates/…/bootstrap.rs• L40-108, crates/…/bootstrap.rs• L111-215

Tenant key rewrite

The tenant rewrite runs before other writer bootstrap because every later scan must see one physical namespace. It copies each recognized legacy row to its marked destination, verifies the destination has the same value, deletes the source only after verification, and publishes the tenant-envelope marker only after a final scan finds no legacy rows. Repeating any phase after a restart is safe. A conflicting destination fails without deleting the source. References: crates/…/envelope.rs• L44-53, crates/…/envelope.rs• L56-189

Graph and index rewrite

The graph-format rewrite runs as a blocking durable job. It reserves ID allocators above existing data, processes source keyspaces in bounded transactions, and stores a resume key after each committed batch. Once the rewrite completes, startup materializes legacy vector properties and queues old catalog definitions for current lifecycle convergence. References: crates/…/mod.rs• L758-844, crates/…/mod.rs• L927-1089, crates/…/mod.rs• L1417-1475

Legacy equality rows use a verify-before-publish pattern. Active generations are rebuilt from authoritative graph properties into current bitmaps. Building generations restart their current lifecycle operation after stale work rows are cleared. Only after both directions of membership verification pass does the writer publish the current storage version, remove obsolete non-unique equality rows, and publish cleanup readiness. References: crates/…/equality_bitmap.rs• L158-195, crates/…/equality_bitmap.rs• L271-319, crates/…/equality_bitmap.rs• L321-432

Definition convergence

Definition convergence turns each valid legacy definition into a current index identity and reconciles it through the normal lifecycle. Tombstones become current cleanup work. The legacy catalog is retired only after all definitions have been activated or otherwise handled. Vector physical cleanup then runs as a blocking job, and the index readiness marker is published in the same final transaction that removes the old catalog metadata. References: crates/…/mod.rs• L1505-1527, crates/…/mod.rs• L1549-1615, crates/…/mod.rs• L1617-1679

Runtime-dependent completion

Some migrations need the complete runtime and its lifecycle drivers. After the writer exists, the system migrates legacy definitions and backfills routing directories for already-active legacy vectors. The full schema marker is published only when the required graph and index markers exist and the legacy catalog is empty. References: crates/…/pipeline.rs• L25-29, crates/…/mod.rs• L1692-1700, crates/…/mod.rs• L1357-1415

Background migration path

Background migration handles cleanup that does not block the writer after the safe current representation is active. The job state lives in the scoped metadata namespace, so a restart can find the same work and continue from its last committed physical key. The default worker runs in the background; disabled mode leaves work available for explicit stepping. References: crates/…/mod.rs• L3-8, crates/…/migrations.rs• L5-12, crates/…/worker.rs• L41-59

Durable job progress

Each migration job has an ID, an execution mode, and a state. A running or failed state records the source stage, the last committed resume key, and the processed-row count. A failure is durable and can return to running from that checkpoint. Completion is durable and prevents the same job from running again. References: crates/…/mod.rs• L820-844, crates/…/mod.rs• L927-1089, crates/…/mod.rs• L1120-1146

Worker scheduling

The worker processes one bounded migration step at a time. Useful work uses the active interval. No runnable work ends the worker. Errors use an increasing retry delay capped at 60 seconds, and shutdown interrupts the wait before the worker joins. Batch limits apply to both source rows and source bytes. References: crates/…/worker.rs• L13-39, crates/…/worker.rs• L74-115, crates/…/migrations.rs• L14-24, crates/…/migrations.rs• L30-84

Cleanup ownership

The background path is used for legacy graph-row cleanup after the current graph rewrite is durable. It does not make old data safe by deleting it first. The blocking rewrite publishes its readiness and enqueues cleanup only after the current rows exist. The worker can then remove old rows in resumable transactions without changing the current representation. References: crates/…/mod.rs• L1445-1502, crates/…/mod.rs• L2930-3119

Safe migration guarantees

The migration design treats a committed prefix as the unit of progress. Copy-before-delete protects key rewrites. Verify-before-publish protects rebuilt indexes. Durable resume keys protect long scans. Readiness markers protect version transitions. Together these rules allow a crash or transient error to leave either the prior safe state or a later state that the next startup can inspect and resume. References: crates/…/envelope.rs• L1-7, crates/…/equality_bitmap.rs• L1-6, crates/…/mod.rs• L1311-1354

Restart behavior

Startup migration jobs are idempotent. A restart sees completed jobs as complete, resumes failed or running work from durable state, and rechecks prerequisites before publishing each readiness marker. Background failures remain visible in the job state and are retried with backoff. References: crates/…/mod.rs• L1014-1088, crates/…/worker.rs• L86-103

Corruption handling

Decoding errors, unknown tags, wrong value kinds, malformed readiness markers, key/value identity mismatches, and conflicting rewrite destinations stop the affected path. The system does not convert these cases into an empty result or advance a marker. This preserves the chance for repair or an explicit database rebuild instead of hiding data loss. References: crates/…/error.rs• L1-61, crates/…/codec.rs• L117-135, crates/…/bootstrap.rs• L156-215, crates/…/envelope.rs• L74-101

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep key prefix and record-kind changes aligned with every scan prefix, parser, and migration stage that identifies the row.
  • Keep value framing changes aligned with supported-version lists, typed value-kind checks, and any compatibility decoder that reads the old bytes.
  • Keep a readiness marker change aligned with its prerequisite marker order and with the final transaction that publishes completion.
  • Keep tenant-envelope changes aligned with operation cursors and every persisted cursor used by resumable scans.
  • Keep legacy index-definition changes aligned with identity conversion and the validation that compares legacy key identity with current record identity.
  • Keep cleanup changes aligned with the rule that current rows are verified before legacy rows or readiness markers are removed.

Important failure modes

TriggerConsequenceGuard
Stored version is newer than the writerThe writer cannot safely interpret managed-index rowsReject the store with an unsupported-version error
Readiness markers skip an earlier step or contain a value other than 1Startup cannot know which migration state is safeRequire an ordered readiness prefix and exact marker values
Legacy tenant destination already contains a different valueCopying could overwrite current dataFail before source deletion and leave the source intact
A value has the wrong frame version, kind, tag, or trailing bytesA row could be interpreted as another row family or as partial dataDecode with bounded fields, closed tags, and full-input checks
Rebuilt equality membership differs from graph propertiesSearch results could omit or add entitiesVerify graph-to-bitmap and bitmap-to-graph before publication
A legacy catalog key and definition disagreeMigration could activate the wrong indexReject the catalog row before convergence
A migration batch fails after a prior commitWork could repeat or stop at an unknown pointPersist the last committed resume key and retry from it

Key files

FileWhy to read it
crates/…/scope.rsDefines tenant identity and the physical tenant envelope
crates/…/managed_index.rsDefines current managed-index key namespaces and parsing
crates/…/codec.rsDefines current value framing and decoder safety checks
crates/…/row.rsDefines graph property row and index-identity encoding
crates/…/index_catalog.rsDefines the legacy dynamic-index catalog boundary
crates/…/metadata.rsDefines storage version and global migration ownership values
crates/…/bootstrap.rsShows version validation and bootstrap planning
crates/…/pipeline.rsShows the writer-open migration order
crates/…/envelope.rsShows copy, verify, cleanup, and marker publication for tenant keys
crates/…/equality_bitmap.rsShows verify-before-publish index migration
crates/…/mod.rsDefines durable jobs, readiness boundaries, and migration orchestration
crates/…/worker.rsShows resumable background scheduling and retry behavior