Courses › System Design › Key Technologies

Vector Databases

Embeddings, ANN indexes (HNSW, IVF, PQ), filtering, hybrid search and RAG.

A vector database stores embeddings — lists of numbers produced by a machine learning model that capture meaning — and finds the vectors most similar to a query vector. It powers semantic search, recommendations, image similarity and retrieval-augmented generation (RAG) for LLM applications. Common choices are pgvector (PostgreSQL), Pinecone, Weaviate, Milvus, Qdrant, and vector search built into Elasticsearch, OpenSearch and Redis.

The core idea

[Documents] -> [Chunk + embedding model] -> (Vector DB: vector + metadata)
[User query] -> [Embed query, same model] -> (Vector DB: vector + metadata) -> [Top-k nearest chunks] -> [Rerank / LLM prompt]
  • Each item (a document chunk, product or image) is turned into a vector of a few hundred to a few thousand dimensions.
  • Similar meaning → nearby vectors, measured by cosine similarity, dot product or Euclidean distance.
  • A query is embedded with the same model, and the database returns the k nearest neighbors.

Approximate nearest neighbor (ANN) indexes

Comparing the query against every vector (exact search) is too slow beyond a few million items. Vector databases use approximate indexes that trade a little recall for huge speedups:

Tuning parameters (ef_search in HNSW, nprobe in IVF) let you trade latency against recall per query.

Practical concerns

  • Metadata filtering — real queries are "similar documents *that this user can see*, in English, from the last year." Filtering after the ANN search can return too few results; good systems filter during the search.
  • Hybrid search — combine vector similarity with keyword (BM25) relevance; exact terms like product codes and names are matched poorly by embeddings alone. See Search Systems.
  • Re-ranking — fetch the top 100 with ANN, then re-score them with a more accurate (and slower) cross-encoder model.
  • Re-embedding — changing the embedding model means re-embedding everything; version your indexes.
  • Scale — 100 million 768-dimensional float vectors is ~300 GB before index overhead, so shard by ID and use quantization.

Choosing one

  • pgvector — you already run PostgreSQL and have up to tens of millions of vectors; you get transactions and joins with the rest of your data.
  • Elasticsearch / OpenSearch — you need keyword and vector hybrid search in one place.
  • Dedicated (Pinecone, Milvus, Qdrant, Weaviate) — hundreds of millions to billions of vectors, high QPS, or a managed service.

[!INTERVIEW] Support chatbot with RAG "Help-center articles are split into ~500-token chunks, embedded, and stored in a vector index with metadata for product and language. For each question, we embed it, run a hybrid search filtered by the customer's product, re-rank the top 50, and put the best 5 chunks in the LLM prompt with citations. A pipeline re-embeds an article whenever it changes." See LLM Inference.