HNSW RECALL

The index got worse and the latency graph got better

L216 MINAI SYSTEMS
PREDICT

Ojaline's product index is an HNSW graph searched at ef_search 5. At launch, recall@5 against brute force is 1.00. Three nightly syncs each delete and re-insert 20% of the products, so the index ends up holding exactly the same 60 vectors. What do you measure now?

An HNSW index (Malkov and Yashunin, arXiv 1603.09320) is a stack of proximity graphs. Every vector sits on layer 0; a random, thinning subset also sits on the layers above, as in a skip list. A node links to M near neighbours on each layer when it is inserted, and keeps at most M links on the upper layers and 2M on layer 0, where the paper’s Mmax0 = 2·M. A query descends greedily from the top, then runs a beam search on layer 0 that keeps the ef_search best candidates and stops when no candidate can improve them.

The graph is the index. A delete takes a node out of other nodes’ routes. The sim deletes the way FreshDiskANN’s “Delete Policy A” does: remove the node and every edge into and out of it, add nothing back. The re-insert links the new node well, and the neighbours that lost a link keep the loss.

▶ SIMULATION
OJALINE PRODUCT INDEX · LAYER 0
grey: layer 0 only · gold: also on an upper layer · ringed: entry point · red: unreachable
WHAT THE DASHBOARD SEES
ef_search
5
catalogue syncs applied
0
avg links per node, layer 0
—
products no search can reach
—
last measured recall@5
not measured
distance computations / query
—
measurements
—
✓ measured recall@5 ≥ 0.9✓ every live product reachable from the entry point
SCHEDULE
01[INDEX]INDEX: build HNSW over 60 products (M=4, ef_construction=16)
02[MONITOR]MONITOR: recall@5 on 16 sample queries vs brute force
03[SYNC]SYNC: catalogue sync 1: delete and re-insert 20% of products
04[SYNC]SYNC: catalogue sync 2: delete and re-insert 20% of products
05[SYNC]SYNC: catalogue sync 3: delete and re-insert 20% of products
06[MONITOR]MONITOR: recall@5 on 16 sample queries vs brute force
Start of three syncs scenario. 6 steps.
# hnswlib-style index, ef_search tuned once at launch
index.set_ef(5)
for product in changed_listings:      # every night
    index.delete(product.id)            # node and its edges gone, no repair
    index.add(product.id, product.vec)
# latency dashboard: flat. Nobody measures recall.

Watch the dashboard column. In THREE SYNCS the links per node fall from 6.35 to 4.75 and nothing turns red until the monitor measures: 0.85, at fewer distance computations than the fresh index. RAISE EF_SEARCH gets 0.99 back for 47.3 computations per query. REBUILD gets 1.00 at the old cost. In FOUR SYNCS 8 products have no path to them at all, ef_search 20 still measures 0.79, and only the rebuild brings it back.

What is simplified: 60 products in 2-D instead of millions in hundreds of dimensions, M = 4 instead of pgvector’s default of 16, nearest-M neighbour selection without the paper’s heuristic, and one delete policy. Libraries differ. hnswlib’s mark_deleted marks a node so that it “will be omitted from search results”; pgvector leaves dead tuples in the index until VACUUM, and its FAQ warns of fewer results “due to dead tuples”. The measurement that catches all of them is the same.

BREAK IT NOT YET

The sync job runs three catalogue syncs. The monitor measures recall@5 twice at ef_search 5. Order the steps so that both measurements read ≥ 0.9, yet the index you end with has recall below 0.9.

GOAL: both measurements ≥ 0.9, final recall < 0.9

TUNE IT NOT YET

After three syncs, recall@5 at ef_search 5 is below 0.9. Find the smallest ef_search that brings the final measurement back to ≥ 0.9, and note what it costs in distance computations.

GOAL: final recall ≥ 0.9 at the smallest such ef_search

FIX IT NOT YET

Write tune(index, queries, k, efs, target). index.live() returns the live products as { id, x, y }; index.search(q, k, ef) returns up to k ids from the HNSW graph for a query { x, y }. Recall for one query is the share of its true k nearest live products (Euclidean distance, by brute force over index.live()) that the search returned; average it over all queries. efs is sorted ascending. Return the smallest ef in efs whose average recall is ≥ target, or the string 'rebuild' if none is. The grader runs it on a fresh graph, a graph after three syncs, one after four, and with a shorter efs list.

The vector indexes chapter runs the same measurement on pgvector with real embeddings, sweeps hnsw.ef_search, and covers the README’s advice for slow vacuums on HNSW: REINDEX INDEX CONCURRENTLY, then VACUUM. In a RAG pipeline this is the second place retrieval can fail without an error; the first is chunking, which decides what the index can return at all.

Mental model:

An HNSW index is a graph you route through, and deletes take roads out of it. Fewer roads make searches shorter and worse at the same time, so latency cannot tell you about quality. Measure recall against brute force on a sample after every heavy round of deletes. Raise ef_search when the beam is too narrow; rebuild when the graph has lost the paths.

IN PRODUCTION

Vector search in pgvector, hnswlib, FAISS, OpenSearch and the hosted vector databases: product search, RAG retrieval and recommendation over data that is updated every day. Symptoms: searches that miss items you can find by id, fewer results than the LIMIT, recall that drifts down over weeks while p99 latency holds or improves. See the vector indexes chapter for the pgvector knobs.

IN INTERVIEWS

“Our vector search has got worse over time but nothing changed. What do you check?” Say recall first: a brute-force ground truth on sampled queries, compared with what the index returns. Then explain the graph: deletes remove routes, the beam stalls in local minima, some nodes become unreachable. Give the two fixes and their costs: raise ef_search (latency, no rebuild) or rebuild the index (build time and memory), and say you would schedule the rebuild after heavy churn and alert on recall, not only on latency.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: KV CACHE BATCHING →