Check your interview readinessStart Tech Assessment

Vector Databases

How vector databases power semantic search and RAG - embeddings, indexes (HNSW), similarity and when to use them.

10 min readUpdated Jul 2026By the TopCoding team

A vector database stores high-dimensional numerical vectors and retrieves the most similar ones to a query vector in milliseconds. It is the storage layer that makes semantic search, RAG, and recommendation systems practical at scale - the piece that connects embedding models to the LLM context window.

~10 ms
Typical ANN query latency at 1 M vectors using an HNSW index
5
Major vector database options compared in this guide
1536
Dimensions in OpenAI text-embedding-3-small - a typical production vector

What vector databases do

Traditional databases search for exact matches or range conditions on structured fields. A vector database searches for approximate similarity in a high-dimensional geometric space. Each document, image, or chunk of text is represented as a vector of floating-point numbers (an embedding), and the database indexes those vectors so it can answer "find the K vectors most similar to this query vector" in sub-linear time.

The naive approach - computing the distance from the query to every stored vector - is exact but O(n) per query. At 10 million documents this takes seconds, not milliseconds. Vector databases use approximate nearest-neighbour (ANN) algorithms that trade a small amount of recall for orders-of-magnitude faster search.

animalsvehiclesambiguous - sits between clustersfar apart = dissimilar meaningdogcatwolfkittenpuppycartruckbusvehiclesedanengineenginequery"hound"
2D projection of a high-dimensional embedding space. Similar concepts cluster together. The query 'hound' lands near the animals cluster; the three dashed lines show its nearest neighbours retrieved by ANN search.

Similarity metrics

"Similar" has a precise mathematical definition that depends on which metric you choose. The metric must be configured at index creation time and affects both index quality and query results.

MetricFormula (simplified)When to use
Cosine similaritydot(A, B) / (|A| * |B|) → range [-1, 1]Default for text embeddings. Measures angle, not magnitude - normalises for document length.
Dot product (inner product)sum(Aᵢ * Bᵢ) → unboundedFaster than cosine when vectors are pre-normalised (unit length). Used in many production systems.
Euclidean distance (L2)sqrt(sum((Aᵢ - Bᵢ)²)) → [0, ∞)Image embeddings and some structured embeddings. Sensitive to magnitude.
Cosine vs dot product
If you L2-normalise all your vectors before storing them, cosine similarity and dot product give identical rankings. Most embedding APIs (OpenAI, Cohere) return normalised vectors by default, so dot product is the faster choice in that case.

ANN search

Approximate nearest-neighbour (ANN) search returns the K most similar vectors without scanning the entire dataset. The key parameter is the tradeoff between recall (how often the true nearest neighbour is in the result) and latency (how long the search takes). Most production systems target 95-99% recall at less than 50 ms p99 latency.

Two families of ANN algorithms dominate production systems: graph-based (HNSW) and quantisation-plus-partition-based (IVF + PQ). They have fundamentally different memory and latency profiles.

HNSW and IVF

HNSW (Hierarchical Navigable Small World) builds a multi-layer graph where each layer is a subset of the full index. At query time the search starts at the top (coarse, few nodes), greedily navigates to the approximate region of interest, then descends to the bottom layer for precise neighbourhood search. Think of it as a geographic zoom: country level, then city, then street.

IVF (Inverted File Index) partitions the vector space into Voronoi cells (clusters), stores an inverted list of vectors per cell, and at query time only searches the most relevant cells. IVF is typically combined with product quantisation (PQ) to compress vectors and reduce memory footprint dramatically.

AlgorithmMemoryBuild timeQuery speedRecallBest for
HNSWHigh (full vectors in RAM)Slower (graph build)Very fastVery highDefault choice; low latency, high recall
IVF + PQLow (compressed vectors)Faster (cluster partitioning)FastGood (tunable)Billion-scale indexes where RAM is the constraint
ScaNN (Google)MediumMediumVery fastVery highLarge-scale GCP deployments

HNSW is the default in almost every managed vector database (Pinecone, Weaviate, Qdrant, Milvus). pgvector supports both HNSW and IVF-Flat. For most RAG applications you will never need to tune the algorithm - the defaults are well-calibrated.

pgvector vs dedicated vector DBs

This is the most common architectural decision teams face when adding vector search. pgvector is a Postgres extension that adds a vector column type and ANN index support. Dedicated vector databases are purpose-built systems with vector-first architecture.

pgvector
Use when:
You already run Postgres. Dataset is under ~5 M vectors. You want transactional consistency (update a document row and its vector atomically). Your team has strong Postgres skills. Avoid adding another infrastructure dependency.
Dedicated vector DB
Use when:
Dataset exceeds 5-10 M vectors. You need <10 ms p99 at high QPS. You want managed hybrid search (dense + BM25) out of the box. Multi-tenancy, access control, and namespace isolation are requirements.
pgvector HNSW is production-quality
pgvector 0.6+ added HNSW index support and matches the recall of dedicated databases at moderate scale. The operational simplicity of staying in Postgres is real - do not reach for a dedicated database prematurely.

Metadata filtering

Pure vector search returns the globally nearest vectors. In practice you almost always want to filter by metadata first: "find the most similar documentsowned by this user" or "find the most similar chunksfrom this product category".

There are two approaches, and choosing the wrong one degrades recall severely:

  • Pre-filter (filter then search) - apply the metadata filter first to reduce the candidate set, then run ANN search within it. Fast when the filter removes most documents. Recall collapses when the filter returns a small subset, because ANN was built on the full index.
  • Post-filter (search then filter) - run ANN to get top-1000, then apply the filter. Recall is high, but you may need a large over-fetch if the filter is selective, increasing latency.

Production vector databases (Weaviate, Qdrant, Pinecone) use a hybrid approach - a payload index for metadata combined with ACORN (Weaviate) or filtered graph traversal (Qdrant) - to maintain recall across all filter selectivities. Understand which strategy your chosen database uses before designing your schema.

Comparing your options

DatabaseHostingIndexHybrid searchFilteringNotes
pgvectorSelf-hosted (Postgres)HNSW, IVF-FlatManual (combine with pg_bm25 / ParadeDB)SQL WHERE clauseBest for teams already on Postgres; no extra infra
PineconeManaged SaaSProprietary (HNSW-based)Yes (built-in sparse + dense)Metadata filtersZero ops; serverless tier; strong enterprise SLAs
WeaviateSelf-hosted or managedHNSWYes (BM25 + vector hybrid, ACORN filtering)GraphQL + metadata indexOpen-source; rich module ecosystem; good multimodal support
QdrantSelf-hosted or managedHNSWYes (sparse vectors + dense)Payload index + filtered HNSWHigh-performance Rust core; excellent filtering recall
MilvusSelf-hosted or managed (Zilliz)HNSW, IVF+PQ, DiskANNYesScalar indexBattle-tested at billion-scale; most algorithm choices
Not sure which to choose?
Start with pgvector if you have Postgres. Move to Qdrant or Weaviate when you hit 10 M+ vectors or need managed hybrid search. TopCoding coaches engineers through these architecture decisions and the interview questions they generate. Book a free call to get personalised advice.

For how the vectors stored in these databases are produced, see Embeddings Explained. For how vector databases plug into a full retrieval pipeline, see RAG Explained.

Sources & further reading

  1. 1Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphsMalkov & Yashunin, 2018 - arXiv 1603.09320
  2. 2pgvector - Open-source vector similarity search for Postgrespgvector on GitHub
  3. 3Pinecone Learn - Vector Database GuidePinecone
  4. 4Weaviate Vector Database DocumentationWeaviate