Vector Databases and Approximate Nearest Neighbor Search

Vector databases explained: embeddings, cosine similarity, ANN indexes like HNSW and IVF, filtering, recall vs latency and scaling vector search.

Intermediate 3 min read · Updated

A vector database stores embeddings, lists of numbers that represent the meaning of text, images or users, and finds the stored vectors most similar to a query vector. Exact search compares the query against every vector, which is too slow for millions of items, so vector databases use approximate nearest neighbour (ANN) indexes such as HNSW and IVF that trade a little accuracy (recall) for very large speed-ups.

vectorsnearestDocumentsEmbedding modelVector indexHNSWQueryEmbed queryANN searchtop-k + filtersResultsre-ranked
Semantic search with a vector database: documents and queries go through the same embedding model, and an approximate nearest-neighbour index finds the closest vectors.

Embeddings and similarity

An embedding model maps similar things to nearby points: "refund policy" and "how do I get my money back" end up close together even though they share no words. Similarity is measured with:

MeasureMeaningNotes
Cosine similarityAngle between vectorsThe usual choice for text embeddings
Dot productAngle and lengthEquals cosine when vectors are normalised; used by many retrieval models
Euclidean (L2) distanceStraight-line distanceCommon for image and other embeddings

Use the measure the embedding model was trained for. Vectors typically have a few hundred to a few thousand dimensions; a million 768-dimension vectors in 32-bit floats take about 3 GB before index overhead.

ANN index types

IndexHow it worksStrengthsWeaknesses
Flat (exact)Compare against every vectorPerfect recall, simpleLinear cost; fine only for small sets
HNSWA multi-layer graph where each vector links to near neighbours; search greedily walks the graph from the top layer downVery high recall at low latency; supports incremental insertsMemory-hungry; deletes and rebuilds are costly
IVFCluster vectors into many lists; search only the lists nearest the querySmaller memory, fast builds, good for very large setsRecall depends on how many lists you probe; needs training on sample data
Product quantisation (PQ)Compress vectors into short codes; often combined with IVFLarge memory savingsLower precision; re-rank with full vectors to recover
Disk-based graphs (e.g. DiskANN-style)Graph index stored on SSD with a small in-memory partBillions of vectors without all of them in RAMHigher latency than in-memory

Each index has knobs that trade recall for speed, such as HNSW's search breadth or IVF's number of probed lists. Tune them against a measured recall target on your own data, for example 95% recall@10 compared with exact search.

Filtering and hybrid queries

Real queries combine similarity with conditions: "similar products, in stock, under ₹2,000, in this category", or "chunks this user is allowed to read". How filters are applied matters:

  • Post-filtering (search, then drop non-matching results) can return too few results when the filter is selective.
  • Pre-filtering (restrict the candidate set first) is accurate but can be slow if the index can't skip efficiently.
  • Good vector databases apply filters during graph or list traversal, and partition indexes by high-level filters such as tenant.
  • For text, combine vector search with keyword search (hybrid search) to handle exact terms and IDs. See RAG.
  • Shard vectors across nodes and query all shards in parallel, merging their top-k results; replicate shards for read throughput and availability. See sharding.
  • Separate indexing from serving: build or rebuild indexes in the background and swap them in.
  • Freshness: newly inserted vectors may sit in a small unindexed buffer that's searched exactly until it's merged into the main index.
  • Re-embedding: changing the embedding model invalidates every vector, so plan a full rebuild with a dual-write or blue-green index switch.
  • Multi-tenancy: a namespace or partition per tenant keeps filters cheap and data isolated.

Do you need a dedicated vector database?

Not always. Many relational and search databases now support vector indexes (for example PostgreSQL with the pgvector extension, or Elasticsearch and OpenSearch). For thousands to a few million vectors that live alongside relational data, an extension keeps one system and transactional consistency. Dedicated vector databases earn their place at hundreds of millions of vectors, with heavy filtering, high query rates or tight latency targets.

Where it’s used

RAG and semantic search

Retrieve document chunks similar in meaning to a question, filtered by the user's permissions.

Recommendations

Two-tower models embed users and items; ANN search over item vectors generates candidates in milliseconds.

Deduplication and similarity

Find near-duplicate images, products or support tickets by embedding distance.

In an interview

  • Explain embeddings and pick the similarity measure the model expects.
  • Compare HNSW and IVF in terms of recall, latency, memory and updates.
  • Explain how filters interact with ANN search.
  • Plan for sharding, freshness and re-embedding when the model changes.

Further reading