HNSW vector index explained, with measured trade-offs

How an HNSW vector index finds nearest neighbours in a layered graph, what M and ef control, and measured recall and latency at 1.18M vectors.

An HNSW vector index is a layered graph that finds the stored vectors nearest to a query without comparing against every one. Search starts in a sparse top layer, hops greedily toward the query, then drops into denser layers to refine. It gives up a little recall for a large drop in work, and three parameters set that trade.

What problem does HNSW solve?

A semantic cache turns every question into an embedding, a vector of a few hundred numbers. To reuse an answer it must find the stored question whose vector is nearest to the new one. The exact way is to measure the distance to every stored vector and keep the smallest. That is correct, and its cost grows in step with the number of vectors, on every single lookup.

HNSW stands for hierarchical navigable small world. It was introduced by Malkov and Yashunin in Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs. The word approximate matters: the algorithm finds items similar to a query without comparing the query with every item one by one, and in exchange it sometimes misses the true nearest neighbour. How often it finds it is called recall.

How does HNSW search work, step by step?

The index is a stack of graphs. Every vector is a node in the bottom layer. Each node is also promoted to higher layers at random, with a probability that falls off exponentially, so each layer up holds far fewer nodes. In the upper layers the nodes are sparse and the links between them span long distances. In the lower layers the nodes are dense and the links are short.

  1. Start at the entry point in the top layer.
  2. Look at the current node's neighbours and move to the one closest to the query. Repeat until no neighbour is closer.
  3. Drop to the next layer down, at the same node, and repeat the greedy walk. Each layer has more nodes, so each walk refines the position.
  4. In the bottom layer, stop walking a single path. Keep a list of the closest candidates found so far, as wide as the beam width, and keep expanding the nearest unexplored candidate.
  5. Stop when the list is full and the nearest unexplored candidate is further away than the worst result already held.
  6. Return the top k results from the list.
flowchart TD
  Q["query vector"] --> E["entry point, top layer"]
  E -->|"greedy hops over long links"| L1["closest node in the top layer"]
  L1 -->|"drop a layer"| L2["greedy hops over shorter links"]
  L2 -->|"drop to the bottom layer"| L3["beam search: keep the closest candidates"]
  L3 --> R["top k nearest neighbours"]
Figure 1. How an HNSW search descends the layers Long links in the sparse upper layers get the search into the right region. The dense bottom layer finds the nearest neighbours inside it.

The upper layers work like a motorway network and the bottom layer like local streets. The paper shows that separating links by their length this way gives search cost that grows with the logarithm of the collection size instead of in step with it.

What do M, ef_construction and the beam width control?

ParameterWhat it isRaise it and you get
MEdges kept per nodeBetter recall, more memory, slower build
ef_constructionNeighbours considered when inserting a nodeA better graph, slower build
Beam width, also called ef or ef_searchCandidates kept during a searchBetter recall, slower queries, no rebuild needed

Vendors describe them the same way. Qdrant's indexing documentation defines m as the number of edges per node, where a larger value means a more accurate search and more space, and ef_construct as the number of neighbours considered during the build, where a larger value means more accuracy and more build time. Its example configuration uses m 16 and ef_construct 100. The pgvector README sets the search-time knob, hnsw.ef_search, to 40 by default.

M and ef_construction are fixed when the graph is built. The beam width is chosen per query. That makes the beam the first thing to tune: you can trade latency for recall without rebuilding anything.

Measured: recall and latency by beam width

Numbers make the trade concrete. This is the Crowkis index on glove-100-angular, the GloVe set with 100 dimensions from the ann-benchmarks project, stored as 32-bit floats, built with M=32 in the upper layers, 64 in the bottom layer, and ef_construction=400. Each cell is recall at 10 and median search latency.

HNSW recall at 10 and p50 search latency on glove-100-angular
CorpusBeam 64Beam 256Beam 512BuildMemory
100K92.25% / 436 µs99.14% / 1403 µs99.87% / 2661 µs98 s159 MB
500K87.97% / 555 µs96.94% / 1884 µs98.94% / 3455 µs610 s469 MB
1.18M86.49% / 526 µs95.58% / 1792 µs98.08% / 3309 µs1345 s1067 MB

The figures were taken on a development laptop, an Apple Silicon machine with 16 GB of RAM and 8 cores, while other builds ran on it. Treat them as a floor. They measure the index called in process, and they are not server throughput.

Three things stand out.

  • Recall falls as the corpus grows, at a fixed beam. Beam 64 finds the true neighbours 92.25% of the time at 100K and 86.49% at 1.18M. A graph that was dense enough for a small set is too sparse for a large one.
  • A wider beam buys the recall back and takes time. At 1.18M, beam 256 reaches 95.58% and takes 1792 µs, more than three times the beam 64 latency.
  • No setting wins both. No beam width clears recall at or above 95% and p50 under 1 ms at the same time, at 1.18M. Build time at that size is 22.4 minutes.

An earlier investigation into the recall drop found where the misses come from. When the search found the true neighbour, it ranked it first. When it missed, the true neighbour was never among the candidates at all. So the loss was in reaching the right part of the graph, and the fix is more edges or a wider beam. Re-ranking the candidates more exactly would not have helped. A million vectors on a laptop has more of the vector-search numbers.

Why is filtered search the hard part?

Real lookups are rarely over the whole index. A cache searches inside one tenant; a document store searches inside one document set. There are two ways to combine a filter with a graph walk, and both have a cost.

Filtering after the search is simple and can return nothing. pgvector says so plainly: with approximate indexes, filtering is applied after the index is scanned, and it offers iterative index scans to look further when too few rows survive. Crowkis had the same defect. A small document set beside a large cache returned the correct answer for 0 of 200 test queries, because the nearest vectors overall all belonged to the cache.

Filtering during the walk fixes that and needs a limit. A sparse scope rarely fills the result list, so the stop condition in step 5 never fires and the search walks the whole graph. Crowkis now filters inside the traversal with a budget of 16,384 node visits, which brought the same test to 200 of 200. The ceiling is stated in its docs: an answer sitting behind more than roughly sixteen thousand nearer out-of-scope vectors can still be missed. Qdrant takes a third route and adds extra edges to the graph based on indexed payload values. Self-hosted RAG with CDOC shows where the scoped search is used.

A second repaired defect shows how much hangs on the entry point. Re-indexing the key that happened to be the entry point cut the graph off from it, and only 4 of 200 queries returned the correct answer until that was fixed.

Where does the time go in a semantic cache?

In a cache the index is not the slow part. Crowkis measured vector search at 526 µs at 1.18M vectors, and the end-to-end semantic read, a cold CGET, at about 250 ops/sec on the same laptop. The difference is the time to embed the query. The lookup is embedding-bound and not search-bound, so tuning the index further will not speed it up.

Two other numbers are easy to confuse with it. A plain GET runs at 103,000 ops/sec at 512 connections, which is a key read that never touches the index. And an exact repeat of a question skips the embedder through a small cache of recent embeddings: in process, on one warm thread, that took an exact-repeat CGET from 19.5 ms to 0.16 ms. Neither is the cost of a new, reworded question. Where the milliseconds go breaks the path down.

How does Crowkis use HNSW?

The index is written for Crowkis and lives inside its storage engine, next to the LSM tree. It supports insert, search, delete and persist, and it is reloaded from disk at start. The default embedder produces 384-dimension vectors. Three commands let you look at it from any RESP3 client:

text
CVECCOUNT
CSIM "How do refunds work?" "What is the refund process?"
CGET "What is the refund process?" THRESHOLD 0.9 TENANT demo WITHCONFIDENCE

CVECCOUNT returns the live number of entries in the index. CSIM returns the similarity between two queries through the live embedder. CGET runs the search and then the checks on the result, and THRESHOLD sets the similarity bar for that one call. A near neighbour is only a candidate: a cache hit is not a correct answer covers what happens after the index returns.

On quantization: storing each dimension as an 8-bit integer instead of a 32-bit float takes one byte per dimension instead of four. Crowkis has built its index both ways. In the investigation above the 8-bit version was not the cause of the recall loss, since the missed neighbours were absent from the candidates and not merely ranked wrong.

When is HNSW the wrong choice?

  • Small collections. With a few thousand vectors, comparing against all of them is exact and quick. An approximate index adds build time and a recall question you did not have.
  • Collections larger than memory. HNSW keeps the graph in RAM. The table above shows 1067 MB at 1.18M vectors of 100 dimensions, and wider embeddings need more. Disk-based indexes exist for that case.
  • Scale beyond what has been verified. For Crowkis the largest verified scale is 1.18M vectors at dimension 100. A 5M run was attempted and failed at 12% recall, an 8M run ran out of memory on 16 GB, and dimension 1536, the shape many commercial embedding models produce, has never been benchmarked. If your corpus is your product, use a vector database; semantic cache vs vector database compares the two jobs.
  • Workloads that need exact answers. If a missed neighbour is unacceptable, an approximate index is the wrong tool at any setting.

Common questions

Is HNSW exact?

No. It is an approximate nearest neighbour index, and its quality is reported as recall: the share of true nearest neighbours it finds, measured against a brute-force search. Recall depends on the graph parameters, the beam width and the size of the collection.

What are good values for M and ef?

Start from your library's defaults and measure recall on your own vectors. Raise the beam width first, because it needs no rebuild. Raise M and ef_construction when a wider beam no longer gets you to the recall you need, and accept the extra memory and build time.

Why does recall drop as the index grows?

The same number of edges per node has to cover more nodes, so the search is more likely to end in a region that does not contain the true neighbour. More edges or a wider beam restore recall. The measured table above shows the effect from 100K to 1.18M at a fixed beam.

How many vectors can the Crowkis index hold?

The largest scale verified on a public dataset is 1.18M vectors at dimension 100. Nothing above that has worked yet, and figures for larger sizes are projections. The product page is Crowkis.

Book your demo today.

A 30 minute call. Bring your stack and your questions.

Pick a time

Or write to us.