Skip to content

Refactor compaction for concurrent index - #487

Open
razdoburdin wants to merge 12 commits into
mainfrom
dev/razdoburdin/compaction
Open

razdoburdin wants to merge 12 commits into
mainfrom
dev/razdoburdin/compaction

Conversation

@razdoburdin

@razdoburdin razdoburdin commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

The goal of this PR is reduction of compaction time for concurrent::MutableVamanaIndex.
Two approaches are applied:

  1. adds a new method repair_incomming_connections to concurrent::MutableVamanaIndex and related orchestrators. The method takes a pointer to a response structure with internal ids and do consolidation (reparent of incoming connections) if the vector is soft-deleted (no-op for all other vectors). delete_entries now takes an optional pointer to a response structure, if the pointer isn't null it writes internal ids of the newly soft-deleted slots and compaction epoch.
  2. Compact() method now swaps Empty slot with the last element in index, so number of copies is min(n_empty, n_non_empty).

C-API changes:

  1. svs_task_create_points_deletion returns the svs_task_h follow_up, allocate memory for it and write all required data, so follow_up task structure has all required information to finalize deletion.
  2. svs_task_complete takes the svs_task_h task and execute it. If the execution is successful, task is freed by this operation. If an error occurs, task is kept valid for resubmission on manual drop by svs_task_drop.
  3. svs_task_drop frees the tasks's memory without execution or rollback.

So the 2-stage deletion pipeline looks like:
task = svs_task_create_points_deletion;

submit to background execution:
svs_task_complete(task);

@rfsaliev rfsaliev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Some changes requested

Comment thread bindings/c/include/svs/c/svs_c.h Resolved
Comment thread bindings/c/include/svs/c/svs_c.h Resolved
Comment thread bindings/c/include/svs/c/svs_c.h Outdated
/// @param task The task handle to free
/// @remarks A task keeps its index alive: the index is released only when the index
/// handle and all of its tasks are freed.
SVS_API void svs_task_free(svs_task_h task);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What will happen if user created a task, but not executed it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

it's ok from correctness perspective. The task is deleted, but it's job remains undone.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hm, it does not look like reliable approach. Instead it looks like user can drop an uncompleted task at a middle of processing.
Considering use "commit-cancel" approach:

  • Commit-and-release: svs_task_complete() or svs_task_commit() (rather than svs_execute_task()) - complete/execute-till-finish the task and 'free' it.
  • Cancel-and-release: svs_task_cancel() - can be used to mark a vector back to active (valid). Failed if external id is already reused for another vector - to be documented.
  • Drop-and-release svs_task_drop() - just 'free' the task. Should be properly documented for delete_point task explaining what will happen then.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

svs_execute_task -> svs_task_complete
svs_task_free -> svs_task_drop

Comment thread bindings/c/src/svs_c.cpp Resolved
Comment thread bindings/c/src/task.hpp Resolved
@eshenayo

eshenayo commented Dec 1, 2026

Copy link
Copy Markdown

Results from testing this PR (6d9a425) in valkey-search. valkey-search uses the C API with SVS_SYNC_KIND_FINE_GRAIN. It runs consolidation and compaction in the background, on its own thread pool, once deletes reach 25% of the live vectors (eshenayo/valkey-search#77).

Setup:

  • Cohere 768-d embeddings, COSINE, 100K vectors loaded.
  • 64 clients overwrite random keys (each write is a delete plus an add) while 8 search, for 10 minutes.
  • GRAPH_MAX_DEGREE 32, build window 200, EF_RUNTIME 50, 96-core host.
  • Two runs per cell; ranges are min-max.

Correctness: as a drop-in (no changes on the valkey-search side), the valkey-search unit suite (16/16), integration suite (1082 passed) and TSAN unit suite are clean.

1. Drop-in: cheaper compaction

Worst search under overwrites Before (SVS 265a8f8) With #487
FP32, single-threaded maintenance 22.5-23.2 s 16.3-17.4 s
SQ8, single-threaded maintenance 22.0-25.9 s 16.2-17.0 s
FP32, maintenance on up to 96 threads 1.71-2.25 s 1.22-1.42 s
SQ8, maintenance on up to 96 threads 1.48-1.90 s 1.09-1.44 s

Write throughput, recall@10 and memory are unchanged within run-to-run ranges.

2. Two-stage delete

How we used it:

  • Each delete calls svs_task_create_points_deletion, and the tasks are queued.
  • Up to N background threads run them with svs_task_execute.
  • Before compacting, we stop running tasks, and afterwards we free the queued ones, because the compaction makes them no-ops.

Maintenance runs on up to 96 threads; "off" is the drop-in. All runs are from the same day.

off N = 1 N = 4 N = 8
FP32 worst search 1.26-1.43 s 1.29-1.32 s 0.83-0.90 s 0.39-0.52 s
FP32 median compaction 908-984 ms 812-993 ms 460-526 ms 254-260 ms
FP32 median consolidate before it 3.5-3.7 s 3.3-3.9 s 2.0-2.1 s 0.42-0.46 s
SQ8 worst search 1.06-1.28 s 0.94-1.09 s 0.73-0.74 s 0.34-0.41 s
SQ8 median compaction 716-755 ms 722-758 ms 468-505 ms 254-260 ms
  • One thread isn't enough: at 2,000-4,000 deletes/s, one thread can't keep up with the repairs, so most deletes are still unrepaired when the next compaction starts. Eight threads keep up, and compaction then has little left to consolidate under its exclusive lock.
  • Cost: each svs_task_execute took 1.5-2.7 ms of wall time under this load, lock waits included.

3. Compaction can be starved by the C API wrapper's lock

  • The lock: ConcurrentIndexVamana::compact takes DynamicIndex::write_lock() exclusively (bindings/c/src/index_vamana.hpp). search, add_points, delete_points, consolidate and repair take read_lock() on the same std::shared_mutex. On glibc that lock favors readers, so under steady shared traffic the exclusive request can wait indefinitely.
  • What we saw: with 4 or 8 threads running repair tasks, and before we paused them for compaction, compaction never finished in a 10-minute run (4 threads), or waited about 590 s until the writers stopped (8 threads). Stack: std::shared_mutex::lock from DynamicIndex::write_lock (index.hpp:91) from ConcurrentIndexVamana::compact.
  • After pausing our repair threads: one run in 16 still stopped compacting for minutes (deleted slots grew to 467K, memory from 114 to 517 MiB). We didn't capture a stack for it, but it fits search and write traffic alone starving the same lock.
  • Suggestion: the index's own compact_gatekeeper_ already stops new shared holders while a compaction waits. Either rely on it for the FINE_GRAIN path, or give the wrapper a writer-preferring lock (for example, a gate the wrapper's shared callers pass through, like compact_gatekeeper_), so a pending compaction can't be starved.

4. API notes

  • Return value: svs_task_execute returns only success or failure. A count of the slots it repaired, 0 when an epoch change skipped the work, would let callers measure how far repairs lag behind.
  • Task lifetime: a task keeps its index alive until freed. That's fine for us; we free queued tasks before replacing or freeing a handle, for example when an SQ8 conversion swaps the index.
  • One id per call: fine for us, since valkey-search deletes one key at a time.

@eshenayo

eshenayo commented Dec 1, 2026

Copy link
Copy Markdown

Follow-up: 245d1f8 fixes the compaction starvation we reported above.

These are local runs on a 96-vCPU lab server, so they confirm the fix but don't predict cloud numbers: the lab host has more memory bandwidth than a cloud instance. The reference measurement will be on an AWS 16-vCPU instance with nearly all threads given to compaction, comparing peak latency for HNSW and LeanVec. We'll post those results here when they're done.

Setup: same as above. Cohere 768-d, COSINE, 100K vectors, 64 clients overwriting while 8 search, 10 minutes, two runs per cell (ranges are min-max). Maintenance threads are scaled to the index size: at 100K, compaction uses up to 12 threads and consolidation up to 6. "Drains" are the background threads running svs_task_execute. In earlier runs we paused them while compacting, to work around the starvation; these runs test with and without that pause.

Repairs off 4 drains, no pause 8 drains, no pause 8 drains, paused
FP32 worst search 1.39-1.48 s 0.58-0.76 s 0.34-0.68 s 0.40-0.51 s
FP32 median compaction 947-973 ms 327-344 ms 182-290 ms 203-268 ms
SQ8 worst search 0.90-1.37 s 0.61-0.67 s 0.32-0.35 s 0.34-0.35 s
SQ8 median compaction 710-726 ms 385-483 ms 245-250 ms 213-243 ms
  • No starvation in 16 runs: compaction kept completing in every run (46-105 passes per run), deleted slots stayed bounded, memory stayed flat, and no search failed. With 6d9a425, the same 4-drain setup never completed a compaction in 10 minutes.
  • No workaround needed: with and without pausing the drains, results overlap, so valkey-search doesn't need its own change for this.
  • More repair threads help: 8 repair threads keep up with the deletes better than 4, so compaction has less left to consolidate.

@eshenayo

eshenayo commented Dec 2, 2026

Copy link
Copy Markdown

AWS reference run with #487 (245d1f8): peak latency under overwrite churn, HNSW against SVS.

LeanVec isn't included. LVQ / LeanVec need the prebuilt SVS package, and no package with #487 exists yet. These columns are HNSW, SVS FP32 and SVS SQ8, all built from source with #487. We'll add LeanVec when a package with #487 is available.

Setup:

  • Server r8i.4xlarge (16 vCPU), client r8i.8xlarge, same AZ and cluster placement group (0.3 ms round trip), Amazon Linux 2023, Valkey 9.2.0.
  • valkey-search with background consolidation and compaction (SVS_VAMANA: consolidate deleted slots in the background and keep FT.INFO from waiting eshenayo/valkey-search#77).
  • Nearly all threads to compaction: svs-maintenance-threads 16 (compaction uses all 16, consolidation 8), reader and writer threads 16, io-threads 2.
  • Cohere 1M, 768-d, COSINE.
  • Matched graphs: HNSW M 16 / EF_CONSTRUCTION 200, SVS GRAPH_MAX_DEGREE 32 / CONSTRUCTION_WINDOW_SIZE 200, EF_RUNTIME 50 for both.
  • 64 clients overwrite random keys (delete plus add) while 8 search, for 20 minutes. Maintenance runs once deleted slots reach 25% of the live vectors.
  • One run per configuration.
HNSW SVS FP32 SVS SQ8
Search QPS at rest 10,957 10,296 11,855
Recall@10 at rest 0.953 0.953 0.919
Closed loop under churn
Search QPS 5,603 6,195 7,774
Writes/s 851 1,399 1,614
Worst search 0.12 s 4.92 s 5.51 s
Search p99 / p99.9 12 / 16 ms 6 / 25 ms 5 / 17 ms
Compaction, median / max - 4.0 / 4.9 s 4.5 / 5.5 s
Consolidation, median (concurrent) - 19.8 s 16.0 s
Passes in 20 min - 6 7
Open loop under churn
Search rate 5,478/s 5,148/s 5,927/s
Searches over 1 s 0% 20.3% 18.2%
Search p99 / max 0.13 / 0.24 s 6.2 / 8.8 s 13.4 / 14.6 s
  • Peak latency is the compaction. SVS searches are fast between passes (lower p99 than HNSW), but each compaction blocks the index for 4-5.5 s on 16 vCPU. HNSW's worst search is 0.12 s. With a pass every 3-3.5 minutes, the index is blocked 2.0% (FP32) to 2.6% (SQ8) of the time.
  • Open loop exaggerates the stall here. The open-loop rate was half the at-rest throughput, but under churn that's 76-98% of what each engine served closed loop, so the queue that builds during each stall takes a long time to drain. The closed-loop worst search is the cleaner peak-latency number. A rerun at a lower rate would separate the stall from the queueing.
  • Writes are faster on SVS (1,399-1,614/s against 851/s for HNSW), and its search throughput under churn is higher.

Local runs (96-vCPU lab server, above) showed that running the per-delete repairs from svs_task_create_points_deletion on background threads cut the compaction several-fold. That's a valkey-search prototype, not used in this AWS run.

@eshenayo

eshenayo commented Dec 2, 2026

Copy link
Copy Markdown

AWS follow-up: the two-stage delete's per-delete consolidation, run in the background, on the same setup as the AWS run above (r8i.4xlarge server, 16 vCPU, Cohere 1M, 20 minutes of overwrites, svs-maintenance-threads 16, built with this PR at 245d1f8).

"Repairs on" means:

  • each delete uses svs_task_create_points_deletion;
  • up to 8 background threads run the follow-up tasks;
  • before compacting, valkey-search frees the queued ones.

This is a valkey-search prototype, not yet in a PR. "Off" is plain delete_points, as in the run above. One run per configuration. The open-loop rate this time is about half the closed-loop throughput under churn, so it shows the stalls without the queueing that inflated the first open-loop run.

FP32, off FP32, on SQ8, off SQ8, on
Closed loop: worst search 4.84 s 1.08 s 5.27 s 0.94 s
Compaction, median / max 4.0 / 4.8 s 0.92 / 1.06 s 4.5 / 5.3 s 0.85 / 0.93 s
Consolidation before it, median 19.7 s 0.07 s 16.0 s 0.07 s
Search QPS / writes per second 6,296 / 1,403 7,164 / 1,247 7,971 / 1,612 8,718 / 1,417
Open loop (3,100/s FP32, 3,900/s SQ8): searches over 1 s 3.87% 0.06% 9.17% 0.04%
Open loop: search p99 / max 3.24 / 4.75 s 0.16 / 1.25 s 4.50 / 5.91 s 0.42 / 1.21 s
  • The background consolidation keeps up. About 250K tasks completed per pass, which is every delete before the trigger. So the full consolidation before each compaction has almost nothing left (0.07 s instead of 16-20 s), and compaction no longer redoes it under its exclusive lock.
  • Each compaction pause drops 4.5-5x, to about 1 s at 1M on 16 vCPU. HNSW's worst search on this setup is 0.12 s.
  • Cost: writes are 11-12% slower. Recall@10 moved by up to 0.02 in either direction between runs, within what we see between identical runs.

LeanVec is still to come; it needs the prebuilt package with this PR.

@razdoburdin
razdoburdin requested a review from rfsaliev December 3, 2026 09:10

@rfsaliev rfsaliev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGFM

Comment thread bindings/c/include/svs/c/svs_c.h Resolved
Co-authored-by: Rafik Saliev <rafik.f.saliev@intel.com>
@eshenayo

eshenayo commented Dec 3, 2026

Copy link
Copy Markdown

Follow-up from the Valkey Search community call on 2026-12-03 (notes): an exclusive compaction window should be about as short as a read or write (tens of milliseconds), with longer work time-sliced across windows and the per-pass setup cost known. This PR brings a compaction at 1M down to about 1 s on 16 vCPU with background repairs, so the remaining step is slicing it. Filed as #505: a C API call that compacts a bounded batch per call.

This branch has not been deployed

No deployments
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.

3 participants