Skip to content

Build unpartitioned vector indexes in memory, in bulk - #1325

Open
xav-db wants to merge 10 commits into
mainfrom
bulk-vector-index-build
Open

xav-db wants to merge 10 commits into
mainfrom
bulk-vector-index-build

Conversation

@xav-db

@xav-db xav-db commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

This adds an in-memory "bulk" path for building an unpartitioned vector index. On real S3 it builds a 1M x 128-dim index in 143 s against 1,775 s for the serial build on main, with the same recall.

It is off by default. One stored byte is new (below), and an older image refuses a store that holds it, so this release only teaches every image to read it. Turn it on with HELIX_VECTOR_BULK_BUILD_MEMORY_BYTES=auto (or a byte count) once every node that can become writer runs an image with this change.

How it works

serial build (today)                    bulk build
  for each source row                     pass 1  read the label's rows into memory, run by run
    read the stored graph                 pass 2  link the whole HNSW graph on a thread pool
    insert one node                       pass 3  write every node once, rechecking each row
    commit
  • Pass 1 takes the label's members from one snapshot, then reads them in runs of 65,536 ids or 64 MiB, each under its own short snapshot. No transaction is held across runs.
  • Pass 3 rereads each source row and stores a node only if the row still holds the vector it was linked for. A row that changed or went away during the build is unlinked from the graph, and its queued operation indexes it as usual. So the guarantees for writes during a build are the serial scan's.
  • Memory is reserved before a row is read, from the label's exact size. A build that would not fit builds serially; one that only has to wait for a running build parks and costs the vector lane nothing.

When it falls back to the serial build

bulk attempt
  not enabled, or does not fit in the memory budget   -> serial, before reading a row
  killed or fenced mid-attempt                         -> next process deletes the attempt's rows, tries again
  three attempts started                               -> serial
  a failure only the bulk path can produce (a panic)   -> rows deleted, serial at once, logged as a defect
  too many rows changed while it ran                   -> rows deleted, serial at once

Stored data

One change, already approved: build stage byte 0x07 in the index operation record, carrying the existing source-scan payload plus one u8 (bulk attempts started). Nothing else stored changes: no new key, value field, tag, merge operand, WAL content or storage version. A bulk-built graph uses today's row encodings; only the edges differ from the ones a serial build would choose.

With the feature off, no 0x07 is ever written. Turned off mid-build, the build deletes what the attempt stored and returns to the serial Scan stage, which an older image reads.

Numbers

Real server on real S3, release, aarch64, binary built from the head of this branch. Single runs unless it says otherwise. The serial baselines are main's.

Case Bulk Serial on main
1M x 128-dim build 143.0 s, recall@10 0.9625 1,774.6 s, 0.965
250k x 1536-dim build, three runs 69.3 / 67.3 / 72.6 s, 0.966 784.3 s, 0.9676
1M x 128, 100,000 rows rewritten during the build 500.9 s, 0.9646, longest lifecycle step 0.49 s 1,817.0 s, 0.9667
250k x 1536, a ninth of the rows changed 443.2 s in bulk, 0.9609, longest step 0.50 s
250k x 1536, a quarter of the rows changed gives the attempt up after 13.3 s, then serial: 859.8 s in all, every row scanned
  • Rows changed during a build. Repairing links costs about 4 ms a changed row at 128 dimensions and 13 to 21 ms at 1,536. A step stops repairing after 250 ms, and an attempt is given up once the rows still to pass would bring the repairs past an eighth of the graph. That is just under the share where bulk stops being faster than serial at 1,536 dimensions.
  • Read pass under 37,600 rows/s of ingest (990k x 1536): retained conflict metadata stayed flat at 0.5 MiB and the ingest rate did not change. Measured one round earlier; the read pass is unchanged since.
  • Memory limits (one round earlier, admission unchanged since): two builds in a 13 GiB cgroup, and one in 11 GiB with 1M rows ingested during the write pass, finished with no OOM kill. At 6.7 GiB the build goes serial before reading.
  • kill -9 in the write pass: finished in bulk on the second attempt in 250.2 s with all 1,000,000 rows present.
  • Forced panic in the write pass: error logged, serial scan one second later, all 250,000 rows present.
  • Older image (one round earlier): refuses a store at 0x07; with bulk turned off the build returns to Scan and the older image opens the store and finishes it.

Also in this PR

  • Validate a vector build's applied state 4,096 rows a step is its own commit. It applies to every vector build, serial and partitioned included, and is not switched by the bulk setting.
  • Resume a vector cleanup scan after a deleted row: the bulk path deletes an abandoned attempt's rows in bounded steps and resumes after the last row it deleted. Existing cleanup behaviour is unchanged.
  • Status gains a vector_bulk_build object (attempt, limit, phase, progress, wait reason). No new stage names; the Go, Python, TypeScript and Rust SDKs decode it.

Not covered

  • Partitioned vector builds stay serial.
  • Discarding or aborting a partly written bulk attempt deletes row by row and is slow on a large index.
  • Not run locally: the x86_64 kernels, the amd64 image, and the macOS and Windows jobs.
  • Two findings that also exist on main and are not fixed here: a fenced writer keeps answering readyz with 200 while every request fails, and at 1M rows about 0.5% of rows are not returned by a search with their own vector under either build path.

Testing

  • The equivalent of every CI job that builds or tests Rust, on the final head. One step is red there: commands::auth::tests::the_callback_port_is_free_again_after_the_callback in the CLI suite fails with "Address already in use" in 3 of 10 suite runs and passes alone. This branch does not touch crates/cli.
  • Contract tests for each rule: admission, parking, the per-run snapshots, link repair and its bounds, each fall-back, status never ahead of storage.
  • Hand mutants of the new rules, all caught.
  • Two independent reviews; every finding is fixed in this branch.

A cleanup scan can now start just past the token of a row it issued, in
that row's lane, instead of at the first lane's prefix. A resumed scan
seeks past the rows already deleted instead of reading their tombstones.
Only a scan from the start proves a namespace empty.
The lifecycle driver's scans still start at the first lane.
`VectorBulkBuildTuning` holds the construction threads, the fewest
vectors a bulk build is started for, and the memory bulk builds may
hold together (`VectorBulkBuildMemory`): off, which is the default,
automatic, or an explicit byte count. The automatic budget is what the
process's memory ceiling leaves beside the larger of its configured
memory and what the rest of the process holds, with an eighth of the
ceiling kept free.

`DbConfig::configured_memory_bytes` is what the configuration lets the
process hold (caches, vector stores, the build planning cache and the
write buffer), `DbConfig::write_buffer_bytes` the write buffer alone,
and `config::resident_anonymous_bytes` the anonymous memory the process
holds now.
`LabelSource::over` reads the rows of ids the caller took from the
label's bitmap itself, by one scan where they are dense in their key
range and by point reads otherwise, as a build step's source does, under
`ReadBounds` instead of one step's limits. `LabelSource::open` is now
that reader over the ids one step takes. `upper_id`, `cursor_id` and
`source_key` give a reader the build's bound, the id a cursor stands at
and the key of an id's row.
`BulkVectors` holds the vectors of a source in entity order and
`BulkGraph::construct` links them on a given number of threads, with the
caller's layer for each node and the degree limits, beam widths and
neighbor selection a serial insert uses, counting the vectors it has
linked and stopping when cancelled.

`BulkGraph::unlink` removes a node whose entity changed after it was
read. Every neighbor that lost its link to the node is linked again from
the neighbors and their own neighbors, with the selection the serial
delete uses, so a graph many nodes were unlinked from stays as densely
linked as a serial build under the same deletes. The graph stays
symmetric: a node a full row refuses is not linked, and a neighbor keeps
the links it has. Every link change is recorded (`LinkChange`), so rows
already stored can be staged again. An unlink runs on the calling thread
and costs 4.3 ms at 128 dimensions and 20.9 ms at 1,536, at the default
degree.

`VectorIndex::stage_bulk_node`, `stage_bulk_link_change` and
`stage_bulk_metadata` stage a graph's rows through the typed writers a
serial insert uses: the rows have today's encodings and differ from a
serial build's only in which edges they hold.

Nothing uses the builder yet.
`IndexOperationDriver::parked_until` names an operation whose next step
could only wait for something the process will signal. The step that
parks an operation releases its claim with its own commit, so the
family's lane goes to other operations at once, and the dispatcher
passes over the operation until the driver stops reporting it or the
returned time passes. The default never parks.
A validation step read as many applied-state rows as the entity ceiling
lets a scan step decode, 512 by default, and proved the partition of
every one of them with a metadata read. A build of a million entities
took 2,000 such steps and 90 s on S3: little beside a serial scan of
half an hour, and 40% of a build whose scan stores a graph linked in
memory, which the next commit adds.

A step now reads up to 4,096 rows, which it only deletes, and proves a
partition once for the rows that follow one another in it: every read
of a step sees one snapshot, so the next row of the same partition
proves nothing more. The entity ceiling still bounds the partitions a
step proves, and the transaction ceilings what it deletes, so a step
that fences writers stays short. An unpartitioned build validates in a
few hundred steps; a partitioned one whose every row names another
partition validates as before.
A bulk build does the work of the serial `Scan` stage in three passes:
it reads the rows of the label's members into memory, links the whole
graph in parallel, and writes every row once from lifecycle steps that
reread the source, so an entity changed since it was read is left to
its queued operation. It reads the source the serial scan reads, the
label's bitmap up to the build's bound. The read pass takes the members
once and reads their rows in runs, each under a snapshot of its own, so
no transaction is open for the length of the read; a run that storage
fails for a moment is read again in place.

Stored state. A bulk attempt is stored as vector build stage 0x07: the
serial scan's progress and one `u8`, the bulk attempts started for the
operation since it was last at the serial stage. After three attempts
that did not store their graph the build scans serially. Nothing else
stored changes: rows are written by the writers a serial insert uses.
A release from before this one cannot decode the stage and refuses to
open the database as a writer while a build is at it, so bulk builds
are off unless `HELIX_VECTOR_BULK_BUILD_MEMORY_BYTES` turns them on
(`auto` or a byte count; unset or `0` is off), and with them off no
bulk stage is stored. A build that blocks, or that runs with bulk
builds off, first deletes what it stored and returns to the serial
stage, so a blocked record is one every release reads.

Failures. A bulk attempt that fails where a serial build may not (a
construction or staging thread that does not start or panics, a write
step that panics, a torn graph, an upload of the count that fails for
good) never blocks the build and is not tried again: what it stored is
deleted and the serial scan builds the index at once.

Source changes. A row changed after the read pass is taken out of the
graph before the graph is written, on the lifecycle step, which costs
4.3 ms a row at 128 dimensions and 20.9 ms at 1,536. A step spends at
most a quarter of a second on such rows. A build whose unwritten nodes
would hold, at the share changed so far, more changed rows than an
eighth of the graph gives its attempt up and scans serially at once:
bulk is the slower way past a changed share of 14% at 1,536 dimensions.

Memory. A build reserves its memory before it reads a row, for the
exact number of the label's members, and scans serially when no budget
would hold it. The budget is measured against what the rest of the
process holds while no build runs; what builds leave resident is taken
to be theirs, so a build that fitted before another ran fits after it,
and what is resident beyond everything the builds reserved is the
rest's. A build that cannot reserve the whole write buffer writes only
while the memtables hold less than two flush windows.

Waiting. A build that waits for its turn, for the memory of running
builds, for its own read and link passes or for a flush parks its
operation and takes no step until it is woken.

Status. `vector_build_path` reports how a build constructs its graph
and `vector_bulk_build` the bulk attempt, its phase and its progress. A
writer reports the bulk stage for as long as a restart would find it:
until the step, or the abort, that left it is uploaded.
The server reports `vector_build_path` and `vector_bulk_build` in the
status of a vector build. No SDK models them; each one's decoding test
now covers a status that carries them.
The setting that turns them on and why it is off by default, the status
fields, what an attempt is and when the build falls back to a serial
one, what discarding and aborting a partly written build cost, and the
rules for upgrading and for rolling back to an image from before them
(v0.0.18 and earlier). Regenerates the docs bundle.
@mintlify

mintlify Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated
helix 🟢 Ready View Preview Nov 23, 2026, 10:05 PM

💡 Tip: Enable Automations to automatically generate PRs for you.

Comment thread crates/db/src/lib.rs
Comment on lines +2388 to +2390
let durable = match self.storage() {
HelixStorage::Writer(writer)
if sightings.is_some() || index_lifecycle::is_aborting_vector_build(&operation) =>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Rollback looks safe too soon

get_index_operation reads sightings before opening its snapshot. After a restart with bulk builds disabled, the call can read None, pause, then resume after the worker has deleted the abandoned graph and applied Scan, but before that change uploads. The saved None skips the durable read, so the response stops reporting the bulk stage while storage still contains 0x07.

An operator following the rollback instructions can then start an older image that refuses the store and takes the writer role from the running server. Check sightings again after reading the applied record, while keeping the protection against forgetting newer sightings.

Comment on lines +2608 to +2614
let min_vectors = self.tuning.min_vectors().max(1);
if rows < min_vectors || (!*probed && source.holds_fewer_vectors_than(min_vectors).await?) {
return Ok(Begun::Next(
self.serial(operation_id, VectorSerialBuildReason::TooFewVectors),
));
}
*probed = true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Probing holds the vector lane

begin runs holds_fewer_vectors_than before checking the memory budget or acquiring the preparation turn. With the default threshold, this can read and decode 4,096 vector rows, or inspect up to 32,768 members, inside one lifecycle step—even when the build cannot fit.

The probe spans multiple 64 MiB runs without stopping at the step's total input-byte limit. Large source rows therefore make other vector operations wait through a long scan before this build falls back to serial. Reject builds that cannot fit before probing, and give the probe a total byte or step bound.

Comment on lines +800 to +803
impl Drop for Turn {
fn drop(&mut self) {
self.parking.wake_all();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 A free turn stays unused

Turn::drop wakes waiting builds before _permit is dropped. On another runtime thread, a woken build can call try_acquire_owned while the permit is still held and park again. Releasing the permit then sends no further wake through Parking, leaving the build idle until another event or the 30-second deadline.

Release the permit explicitly before calling wake_all.

Three contracts failed on CI's runners and passed on a fast machine.

Two asserted how many nodes a step unlinks, under the driver's real
bound of 250 ms of unlinks a step: a slow, busy runner reaches the time
before the count. The contracts' drivers now unlink without a bound in
time, so a step unlinks by its counts on every machine, and only the
contract of the time bound sets one.

One retried a step whose lost commit had started the build's read and
link passes, and expected the retry to collect them. Offered while they
still ran, the retry parked and stored nothing. The retry now waits, as
the worker does, until the passes have woken the build.

The wait for a parked build no longer fails after the 30 s park limit,
which the passes of a source of a few thousand rows can exceed on a
starved machine: it and the two other waits for background tasks share
one liveness bound of ten minutes.

Tests only.

This branch was successfully deployed

1 active (outdated) deployment
staging - docs — 515aecf0 Deployed Nov 23, 2026 by mintlify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant