Skip to content

Design

Experimental. The API is not stable and nothing here is wired into a database engine yet. What may change.

This page describes what the code in src/main/java/ai/humem/tiervec does today. It is written from the sources and their Javadoc. Where something is not built yet, it says so.

The contract: VecIndex

Every index implements VecIndex, over float[] vectors with a long id and one of three metrics (COSINE, DOT, L2).

  • add, delete and search may be called concurrently from many threads, and a search never throws because of a concurrent write.
  • Read-your-writes: after add(id, v) returns, a search that starts later in any thread can return id. After delete(id) returns, a search that starts later never returns id.
  • Ids are unique: adding an existing id replaces its vector.
  • Hit.score is a similarity and higher is better for every metric (cosine and dot as they are, L2 as the negative squared distance).
  • search(q, k, allow) takes an optional LongPredicate filter and may return fewer than k hits only when fewer than k live vectors qualify.

The parts

Class What it is
FlatIndex Exact or quantized scan over every live vector. No graph. Has serialize and load.
GraphIndex Mutable, concurrent HNSW-style graph. seal() turns it into a SealedGraph, fromSealed turns it back.
SealedGraph Immutable compact graph. The only mutation is a soft delete through a bitset. Has serialize and load (one segment file, see SEGMENT-FORMAT.md).
IvfIndex Standalone inverted-file index with k-means lists and a background retrain. Not a segment type of TieredIndex.
TieredIndex The tier: an exact tail in front of sealed graph segments. A tier, its tail included, can be written with checkpoint and read with open.
Kernels, VKernels, NKernels, Kern, VKern Distance kernels. Kernels picks vector or scalar code once per JVM or image, for the flat, IVF, and graph kernels. NKernels holds the Vector API kernels for a native image: the fp32 dot and squared L2, the int8 dot, the SQ8 dots, and block16; its 128-bit forms of the last four are also what an aarch64 JVM uses for them.

Flat index

Vectors live in chunks (the first chunks grow from 64 to 4096 slots, then 4096 per chunk), each with a float array, an id array, a live bitmap, and optional quantized codes. A slot is written once and never reused, so a reader never sees a half-written slot. Appends never copy old data.

The read path takes no lock. A writer fills a slot and then sets its live bit with release semantics, which publishes it. Delete clears the bit. Replace writes a new slot, sets it live, and clears the old bit, so a search can briefly see both copies and the result is de-duplicated by id. Writers take one of 64 stripe monitors by id hash. compact() takes an exclusive lock that pauses writers, never searches.

Quantization is chosen per index:

  • NONE: fp32 scan. The result equals brute force, with ties broken by the smaller id.
  • SQ8: symmetric int8 with one scale per vector. No training, so churn cannot make it stale.
  • BINARY: one sign bit per dimension against a per-dimension threshold, frozen after the first calibration set of live vectors. Until then searches are exact.

A quantized search scans the codes, keeps the best rerankFactor * k candidates, rescores them in fp32, and returns the best k. Returned scores are always exact fp32 scores.

Graph index

A multi-layer HNSW graph, with the alpha robust prune of DiskANN (Vamana) as a parameter (alpha = 1 is the HNSW neighbor heuristic). Nodes live in fixed-size chunks and never move. Neighbor lists are copy-on-write arrays published with release and acquire VarHandle stores, so readers take no lock and always see a complete list. Writers take a striped monitor of the owning node, one at a time.

  • add publishes the node's own links, then marks it live, then adds the reverse links, then updates the entry point. A node is returnable only when live, and reachable only through published links.
  • delete marks a node deleted at once. Deleted nodes are still traversed. consolidate() repairs the neighbor lists that hold a tombstone, waits (never blocking a reader) until searches that started earlier are done, and then recycles the slots.
  • A replace is a new node plus a tombstone of the old one.
  • Search is a beam search at layer 0 with a greedy descent above. Filtered-out and deleted nodes are traversed but not collected. With a filter, after a visit budget with fewer than k collected, the search falls back to an exact scan over the nodes that pass the filter.
  • filterScanBelow(fraction) (default 0, off) lets a filtered search skip the walk when the allowed share is small. The share is estimated from 256 id slots spread over the index (a fixed number of predicate calls, for filters without a count). When at most fraction of the sampled live slots pass, the search scans the allowed ids exactly, which costs one predicate call per live id and returns the exact top k. Above the threshold the search is unchanged. With the option off, a filtered search spends its whole visit budget before the scan when the allowed share is below about ef divided by the budget, and that wasted walk is most of the cost of a filtered search at a share near 0.01 (see issues 12 and 56).
  • A filter can say how many ids it allows (SizedIdFilter). When that count is at most ef (never below k), the search skips the walk and scans the allowed ids exactly. The count picks the plan only: test decides which ids may be returned, and a count of 0 scans too, so a wrong count never gives a wrong result. A negative count means unknown and changes nothing. The threshold of ef has not been set from a study (issue 12). liveCount() is the number that a host compares its count with.
  • With Params.sq8 the traversal uses int8 codes and the final candidates are rescored in fp32. Construction always uses fp32.

IVF index

K-means centroids, one append-only list per centroid, and a search probes the nearest nprobe lists. Until the live count reaches trainAt, every search is an exact scan. Training is lazy. needsRetrain() measures drift, list skew, and growth, and retrain() rebuilds the model off to the side while writers continue (their changes go to a log that is replayed), then swaps the new model in with one volatile write. Searches never wait for a retrain. Deletes are tombstones in the lists, and compact() rewrites a list from its live slots.

IVF has no serialization yet, and it is not used by TieredIndex.

The tier

TieredIndex is the composition. The vectors in the host's records are the source of truth. The tail, the segments, and the marks on their hidden rows are derived data that can be thrown away and rebuilt (rebuildFrom) from raw vectors, so the code never has to make the derived state durable. rebuildFrom swaps the rebuilt state in the way a merge does: a search that started before the swap keeps the segments of its view, with their marks.

Exact tail

add puts the vector into the active tail, a FlatIndex (fp32 or SQ8), and a raw copy next to it. The raw copy is what a seal and a rebuild read. The vector is visible to the next search in any thread before add returns. delete removes it from the tails and hides every segment copy at once. apply does both for a whole batch (see Batches).

Sealed HNSW segments

When the active tail reaches tailLimit vectors, or its first vector is older than tailAgeMs, and the index holds at least smallLimit vectors, one worker thread seals it:

  1. Freeze the tail under the write lock. A new empty tail takes over writes.
  2. Build a GraphIndex from the frozen contents and seal it, outside any lock, while searches and writes continue (searches scan both tails).
  3. Publish the segment with one volatile write of an immutable view that drops the frozen tail.

A vector is always in the frozen tail, in the segment, or in both for an instant, and the dedupe resolves the overlap, so it is never missing. Below smallLimit live vectors no graph is built at all and every search is an exact scan.

Atomic swap of the segment list

The segment list, the active tail, and the frozen tail live in one immutable view. A seal or a merge builds its result off to the side and publishes a new view with a single volatile write. A search reads the view once and finishes on it, so a search that started before a swap still sees everything it needs and the old segments stay valid for it. Their marks stay valid too: a mark is never removed, and a segment lives for as long as a search holds a view that names it, after which the garbage collector frees it. A search writes nothing that another search reads.

Batches

apply(batch) is the write path of a host transaction: the adds, then the deletes, under one hold of the write lock. The tail and the marks change in place, so a new view cannot make a batch visible at one instant. A batch counter does: it is odd while a batch is applied and even otherwise, and only the writer that holds the lock changes it. A search reads it before it scans and waits while it is odd. After the scan it reads it again, behind an acquire fence. When the value changed, a batch was applied during the scan, the answer may mix the state before the batch with the state after it, and the search runs again on the current view. A search that returns therefore saw no batch in progress from the start of its scan to its end: all of every earlier batch and nothing of any later one. A single add or delete does not touch the counter, because each is atomic by itself.

A search that is in the middle of a scan when a batch opens finishes the scan on the view it holds, which is still valid memory, and discards the answer. A search that starts inside the batch waits for it. A search that starts after the batch sees all of it, because the closing write of the counter comes after every change of the batch.

The query path pays one volatile read before the scan and one fence and one volatile read after it, plus a second scan when a batch overlapped the first. A search that meets a batch waits for the length of the batch (a short spin, then parks of 20 microseconds). A stream of batches that follow each other faster than one scan takes can starve a search, so the rate of apply calls is the bound.

Tombstones: a mark on each hidden row

Versions come from one counter under the write lock. A replace or a delete marks every visible copy of the id in the segments with the version at which it hid it. A segment allocates its marks (one long per row) when the first row is hidden, and keeps the list of its hidden rows. A mark is set once and never removed. A copy in the frozen tail, from which a seal is building a segment, is remembered by the tail and marked when the seal publishes that segment, and a merge copies the marks that were set on its inputs while it ran onto its result. Searches pass over marked rows inside the beam, so a deleted or replaced vector never appears and nothing waits for a merge. A segment with no hidden row is searched with the plain filter. Because a mark belongs to a row and not to an id, an id that the host reuses after a delete is a new row, which is visible while the old copies stay hidden. checkpoint writes the hidden rows of every segment into the tombstone file, and open marks them again from those bits (SEGMENT-FORMAT.md), so a reopened tier hides the same rows.

A search scans the tails exactly, searches every segment, drops hidden ids, applies the filter in both, and merges by score. Duplicate ids (a replace in flight) are resolved newest source first: active tail, frozen tail, then segments. Freshness.SEALED_ONLY skips the tails. It may lag (a vector added or replaced since the last seal is not in it) but never returns a deleted id, a stale vector, or an id that was never added.

A search can take a SearchContext, one method expired() for a deadline, a cancel flag, or both. The search asks it when it starts, before each tail and each segment, before more than 1024 candidates have been looked at since the last poll (tail scan, rerank, greedy descent, beam, exact scan of a segment), and after each pause while it waits for a batch. When the answer is true the search throws SearchCancelledException and leaves its view like any other search, so a stopped search holds nothing back. A search without a context pays a test for null in those loops.

Merge

When the segment count exceeds maxSegments, or one segment is deadFraction hidden, the worker rebuilds the smallest segments (or the dead one) into one segment from their valid vectors only, throttled to mergeRatePerSec inserts per second so the cores that serve queries are not starved, copies the marks that were set meanwhile onto the new segment, and swaps it in. A search that started before the swap goes on with the old segments and their marks. The optional size-ratio trigger (mergeTriggerRatio) is described with the knobs.

A merge builds one graph from the valid vectors of its inputs. With mergeMode INSERT (off by default), a merge that takes the largest segment and at least one other copies the graph of the largest, deletes its hidden rows from the copy, inserts the valid vectors of the others, and seals it. It rebuilds instead when the hidden share of the largest segment is above deadFraction. The time and the peak memory of the two modes have not been compared, and INSERT becomes the default only if a study meets bounds written before it (#16).

Bounds and threads

One worker thread runs one task at a time (a seal or a merge). There is a cap on segments. No allocation proportional to n happens on the query path. Back pressure: when the active tail holds hardTailFactor * max(tailLimit, smallLimit) vectors, add waits for the worker, bounded to 10 s. awaitCapacity(deadline) is the same wait as a pre-check for apply: it waits for room in the tail and returns false at the deadline. apply never waits for the worker and does not look at the limit, so one batch can take the tail above it. add and delete take one write lock for a short time, and apply takes it once for the whole batch. Searches take no lock.

The paragraph above is the standalone tier. A tier made with a HostEnv (new TieredIndex(dim, metric, config, env), and the open overloads that take one) creates no thread, pool, or timer:

What How it runs with a host environment
Seals and merges One task at a time on env.executor(), submitted after a write when something is due. A seal by age and a retry need env.schedule, or a call of maintain() from the host's timer.
Parallel part of a build buildThreads ranges, claimed by the calling thread and by helper tasks, so an executor with one thread is enough.
Permits Every seal, merge, and rebuild asks env.acquireBuild(kind, estimatedPeakBytes) first and closes the permit afterwards. A refused background build waits 50 ms, then twice as long each time, up to 5 s. sealNow, mergeNow, and rebuildFrom throw BuildRefusedException.
Memory budget A background merge waits while bytesHeld() plus its estimate is above env.memoryBudgetBytes(). The seal threshold becomes the smaller of tailLimit and about an eighth of the budget in tail rows, at least 64, and the hard tail limit follows. A call of mergeNow asks for a permit only.
close Waits up to 30 s for a build that runs. A task that has not started does nothing.
Health FAILED while the executor refuses the tasks.

The estimate is arithmetic: for each row, twice the vector (with its SQ8 code) plus 12 times M bytes of links, and 200 bytes. It has not been compared with a measured peak (#13). The budget cannot shrink what the vectors themselves need, and it does not hold add back.

Kernels and the scalar twin

Kernels holds the flat-tier distance kernels: float dot and L2, int8 dot, and Hamming. Each has a Vector API version in VKernels and a scalar twin. The choice is made once, when Kernels is initialized: scalar when -Dai.humem.tiervec.scalar=true, scalar when VKernels cannot load (for example without the jdk.incubator.vector module), vector otherwise. Kernels.VECTOR says which one is in use. IvfIndex uses the same kernels.

On a JVM whose preferred float species has fewer than 8 lanes (128 bits: NEON on aarch64), VKernels has no fast form of the int8 dot, the SQ8 dots, and block16: its int8 dot is built on 256-bit species, which the JVM cannot map to NEON (138 times slower than the scalar twin in CI, #52), and the other three need 8 float lanes. There Kernels uses the 128-bit forms of NKernels on aarch64 (Kernels.JVM_NARROW) and the scalar twins on any other host (Kernels.JVM_QUANT_SCALAR), and Kernels.quantReason says which and why. If NKernels cannot initialize on an aarch64 JVM, the scalar twins are used, never an exception. The fp32 dot and squared L2 stay in VKernels on every JVM, hamming stays scalar, and a JVM with 8 or more float lanes is unchanged. Kern.dotQ8 follows the same rule, so one run uses one SQ8 dot in the flat and in the graph tier. AVX-512 and SVE are not used.

Inside a GraalVM native image (Kernels.IN_IMAGE) the choice is different. VKernels is more than 10 times slower than scalar code there (a masked tail and a slice-based sum send the whole method through a slow path), so an image uses NKernels for every kernel that has a Vector API form (the fp32 dot and squared L2, the int8 dot, the SQ8 dots, and block16) when the module is present in the image, and the scalar twins otherwise. NKernels is the textbook form that an image compiles to SIMD code: a static final species, two accumulators, fma, reduceLanes(ADD), a scalar tail, no masks. The species is chosen from os.arch in a static initializer, 128 bits on aarch64 and 256 bits everywhere else (a CPU with AVX-512 runs the 256-bit species), and the image builder must run that initializer, which META-INF/native-image/ai.humem/tiervec/native-image.properties asks for with --initialize-at-build-time=ai.humem.tiervec.NKernels. The initializer catches a missing module and leaves the species null, so an image built without the module still builds and Kernels selects the scalar twins, with the cause in Kernels.scalarReason. The int8, SQ8, and block16 kernels have two forms each in NKernels, one per species, chosen by NKernels.WIDE: the wide form for the 256-bit float species, and the narrow form for the 128-bit one, where the Vector API has no 32-bit byte species, so the byte vector is loaded at 64 or 128 bits and split into parts. Each form is the one that was fastest in an image on both Oracle GraalVM 25 and GraalVM CE 25.0.2, on x86-64 and arm64 Linux (#48). Hamming has no Vector API form and stays scalar. Kernels.JVM_VECTOR and Kernels.NATIVE_VECTOR say which holder is in use. The fixed-order sum of the JVM kernels is not used in an image: a sum in a fixed order is fast only on Oracle GraalVM, so NKernels takes reduceLanes, and its result can differ from the JVM by rounding. To build the image, give the builder --add-modules jdk.incubator.vector -H:+VectorAPISupport (GraalVM 25; the properties file does not set them, so a scalar image stays possible).

Kern holds the graph kernels: float dot, squared L2, and the dot of a float query with SQ8 codes (in an image, NKernels.dotF8). Its Vector API versions are in VKern, and its scalar twins are the scalar twins of Kernels. On a JVM VKern sums the lanes in the fixed order of VKernels.hsum, as the flat kernels do, so a graph kernel returns the same bits for the same input in the interpreter, in C1, and in C2, and a segment that is read back is served with the score bits of the graph it was written from (#104). reduceLanes(ADD) was used there before and left the order to the JIT tier. NKernels still uses reduceLanes(ADD), so on an aarch64 JVM the SQ8 distance that steers the graph walk (NKernels.dotF8N, dotF8x4N) can differ in the last bit between two JIT tiers. The score of a returned hit is not affected: with SQ8 it is recomputed in fp32 by VKern. Only a tie of two walk distances within one ulp can change which candidates a search visits. Kern follows Kernels.VECTOR, and Kernels initializes both holder classes inside its try, so one run never mixes the two modes and a failure to load either holder selects the scalar twins for every index. GraphIndex, SealedGraph, and TieredIndex need no incubator class in that case.

A scalar result and a vector result of the same float kernel can differ by rounding. KernDiffTest states the tolerance (1e-5 times the sum of the absolute terms) and compares exactly representable data bit for bit; for NKernels it uses 1e-4 times the sum of the absolute terms plus a floor of 1e-6, on lengths around the lane width and at 100, 128, and 770, for every form of every NKernels kernel, so the 128-bit forms of aarch64 are tested on an x86 host too. NoIncubatorTest starts a JVM without the module and builds and searches a tier in it. The graph, segment, and tier tests also run with the scalar twins, in the surefire execution scalar-twin.

Tier configuration

TieredIndex.Config is a record. Config.defaults() gives the defaults below, and with... methods return a changed copy. The constructor rejects tailLimit < 1, smallLimit < 0, maxSegments < 1, mergeFan < 2, hardTailFactor < 2, buildThreads < 1, a negative or infinite mergeTriggerRatio, and a BINARY tail quantizer.

Knob Default What it does
tailLimit 2000 Seal when the active tail has this many vectors.
tailAgeMs 60000 Also seal when the oldest tail vector is older than this. 0 means never by age.
smallLimit 1000 Below this many live vectors no graph is ever built and every search is an exact scan.
maxSegments 8 Merge when the segment count exceeds this.
mergeFan 4 How many of the smallest segments one merge takes.
deadFraction 0.3 Compact (merge) a segment when this share of it is hidden.
mergeRatePerSec 50000 Insert rate cap of the merge builder. 0 means unlimited.
hardTailFactor 4 Back pressure: writers wait when the active tail holds this factor times max(tailLimit, smallLimit) vectors.
tailQuant NONE Tail kernel: NONE scans fp32, SQ8 scans int8 codes and reranks the best 5 times k candidates in fp32.
graph GraphIndex.Params.defaults() Segment graph parameters: M 16, efConstruction 200, efSearch 100 (the search beam), alpha 1.0, sq8 false, seed 42.
buildThreads max(1, min(4, cpus - 1)) Threads of one seal or merge build.
splitBeam false When true and more than one segment is searched, segment s gets the beam max(k, ceil(ef * rows(s) / rows of all searched segments)) instead of the full ef.
mergeTriggerRatio 0.0 Size-ratio merge trigger, off at 0. See below.
vectorStorage HEAP Where a segment that open reads keeps its fp32 vectors. HEAP copies them into the heap. MAPPED leaves them in the mapped segment file: less heap, slower fp32 distances as measured (SEGMENT-FORMAT.md).

With mergeTriggerRatio X above 0, the segment with the most live rows is the big one. When the live rows of all the other segments together exceed X times the live rows of the big one, all segments are merged into one. Otherwise, if maxSegments is at least 2 and the count exceeds it, the count rule picks among the small segments only, so the big one is rewritten only by the ratio rule. With maxSegments 1 the count rule includes the big one and a merge happens at every seal. A merge into the big segment rewrites about (1 + X) / X vectors per inserted vector, on top of the small-segment merges.

Other controls on the tier: filterScanBelow(fraction) (default 0, off) is the scan-first threshold of filtered searches, applied per segment (the tails are always scanned exactly), sealNow(), mergeNow() and awaitIdle(timeoutMs) run or wait for the worker, search(q, k, allow, ef) sets the beam per call, and stats() reports seals, merges, segment sizes, floor entries, write amplification, and a log of finished tasks.

Configuration as a map

Config.toMap() writes every setting as text, and Config.fromMap reads it back to an equal configuration. The keys are the component names of the table above, with the graph parameters one by one: graph.M, graph.efConstruction, graph.efSearch, graph.alpha, graph.sq8, and graph.seed, plus mergeMode. A key that a map does not have takes the default of the library that reads it, so a host stores the whole result of toMap(). The dimension and the metric belong to the index definition and are not keys.

Moment Method Unknown key Value that cannot be used
An index is created Config.fromMap(map) refused, the message names it refused, the message names the key
A stored definition is loaded Config.fromStoredMap(map, warning) reported to warning and ignored, so a downgrade opens refused
An open index changes reconfigure(changes) refused refused

reconfigure changes graph.efSearch and splitBeam, the search-time knobs. It refuses every other key whose value would change, and makes all changes of a call or none.

Config.fingerprint(dim, metric) returns a canonical text and its SHA-256 over what decides the data of the graphs and their files: the dimension, the metric, the id width, graph.M, graph.efConstruction, graph.alpha, graph.sq8, and the layer 0 layout. The search-time knobs, the tail settings (the tail is not persisted), the seal and merge policy, vectorStorage, and graph.seed are not in it. The manifest records the same settings except alpha, and open compares those, so a host that wants a change of alpha to discard the persisted state compares fingerprints itself.

Statistics

metrics() returns a Map<String, Long>. bindMetrics(sink) pushes the same numbers to a MetricSink: the sink gets each increment of a counter from then on, and each gauge once, to read when it wants. The library depends on no metrics library, and a sink that throws is ignored.

Name Kind What it counts or reads
tiervec.seals, tiervec.merges counter finished seals and merges
tiervec.failures counter background tasks that failed
tiervec.backpressureWaits counter waits of writers for room in the tail
tiervec.sealedVectors, tiervec.mergedVectors counter rows that seals and merges put into graphs
tiervec.bigMerges, tiervec.bigMergedVectors counter the part of the merges that rewrote the largest segment
tiervec.deferredBuilds counter background builds that waited for a permit or for the memory budget (a refused call that builds on its own thread counts too)
tiervec.adds gauge calls of add, batches included
tiervec.live, tiervec.tailSize, tiervec.segments, tiervec.bytesHeld, tiervec.floorEntries gauge live vectors, vectors in the tails, segments, resident bytes, hidden rows in the segments (the name is from the floor map that the marks replaced)
tiervec.sealMillis, tiervec.mergeMillis, tiervec.maxSealMillis, tiervec.maxMergeMillis gauge time in seals and merges, and the longest one
tiervec.mergeWriteAmpThousandths, tiervec.totalWriteAmpThousandths gauge rows built per add, in thousandths: by merges, and by seals and merges
tiervec.health gauge 0 OK, 1 DEGRADED, 2 FAILED

health() is DEGRADED after a seal or a merge has failed three times in a row, until one of that kind works, and FAILED when the worker has ended while the tier is open.