Vector Databases Deep Dive
How vector databases work under the hood: the HNSW index, approximate nearest-neighbour search, cosine vs Euclidean distance, product quantization and metadata filtering — for Java developers.
On this page
You can use a vector database effectively without understanding its internals, but knowing how approximate nearest-neighbour search works explains its trade-offs — why it is fast, why it is approximate, and how to tune it. This tutorial goes under the hood of the stores you met in Spring AI embeddings.
Key Takeaways
- Vector search is approximate — it trades a little recall for an enormous speed-up.
- HNSW is the dominant index: a navigable graph you traverse toward the query.
- Match the distance metric to your embedding model — cosine for normalised vectors.
- Quantization trades a little accuracy for large capacity at scale.
The problem: exact search does not scale
Finding the most similar vectors to a query by comparing against every stored vector is exact but linear — a million vectors means a million comparisons per query. That is far too slow for interactive use. Vector databases solve this with approximate nearest-neighbour (ANN) search: accept a tiny chance of missing the true best match in exchange for searching in milliseconds.
HNSW: the dominant index
Hierarchical Navigable Small World builds a multi-layer graph of vectors:
Search starts at the top layer, where sparse long-range links let it jump quickly toward the query's region, then descends through layers with progressively denser local connections for accuracy. At each layer it greedily moves to the neighbour closest to the query. The result is search that scales logarithmically rather than linearly.
Tuning HNSW
Two parameters trade quality against speed and memory:
m— connections per node. Higher means better recall and more memory.ef(search) — how many candidates to consider during search. Higher means better recall and slower queries.
spring:
ai:
vectorstore:
pgvector:
index-type: HNSW
# Higher m and ef_construction improve recall at the cost of build
# time and memory. Defaults are sensible; tune only if recall is low.Distance metrics
The metric defines what "similar" means, and it must match your embedding model:
| Metric | Measures | Use when |
|---|---|---|
| Cosine | Angle between vectors | Normalised embeddings (most text models) |
| Dot product | Magnitude and direction | Some models, when magnitude carries meaning |
| Euclidean (L2) | Straight-line distance | Embeddings trained for it |
Product quantization and scale
At very large scale, storing millions of full-precision vectors strains memory. Product quantization compresses vectors by splitting each into sub-vectors and encoding each with a compact code from a learned codebook. This can shrink memory by an order of magnitude, at a small accuracy cost — the codes are approximations of the originals.
It is one of several capacity techniques (alongside disk-based indexes and sharding) that let vector databases scale beyond what in-memory full-precision storage allows. For most applications you will not need it; it becomes relevant at the hundreds-of-millions-of-vectors scale.
Metadata filtering and the index
Filtering by metadata ("only this tenant's documents") interacts with the ANN index in ways that affect performance. A very selective filter can force the database to search more of the graph to find enough matching results, slowing queries. Databases handle this differently — some filter during traversal, some after. At scale, test filtered query performance, not just unfiltered, and index the metadata fields you filter on heavily. See multi-tenant AI architectures.
What this means for you
Understanding the internals guides practical decisions:
- Recall vs speed is a dial (
m,ef) — raise it if retrieval misses relevant passages, lower it if queries are too slow. - The distance metric must match the embedding model — the most common silent misconfiguration.
- Scale techniques (quantization, sharding) matter only at large scale — do not adopt their complexity early.
- Filtered queries need their own performance testing.
Next
Frequently Asked Questions
How does a vector database find similar vectors so fast?
What is HNSW?
Cosine similarity or Euclidean distance — which should I use?
What is product quantization?
Related tutorials
- Fine-Tuning LLMs: LoRA & QLoRAUnderstand fine-tuning for engineers: LoRA and QLoRA, instruction tuning, RLHF and DPO, and the crucial decision of when to fine-tune versus when retrieval or prompting is the better tool.
- Embedding Models & Semantic SearchHow embedding models power semantic search: bi-encoders vs cross-encoders, re-ranking, hybrid search combining keywords and vectors, and choosing embeddings for retrieval quality.
- Transformer Architecture ExplainedThe transformer architecture explained for engineers, not researchers: self-attention, multi-head attention, positional encoding and why it explains context limits, token cost and hallucination.
- Prompt Engineering MasterclassAdvanced prompt engineering techniques: prompt chaining, meta-prompting, self-consistency, structured reasoning and prompt optimization — beyond the basics, for reliable production prompts.