Databases

Vector Databases

How vector databases store embeddings and power similarity search for AI applications like semantic search and RAG.

  • Vector DB
  • AI
  • Embeddings
Vector database illustration

What is a Vector Database?

A vector database stores data as high-dimensional numeric vectors (embeddings) and is optimized to answer one core question fast: “which vectors are most similar to this one?” This makes it the backbone of semantic search, recommendation systems, and Retrieval-Augmented Generation (RAG) for LLMs.

From Text to Vectors

An embedding model turns text (or images, audio) into a fixed-length list of numbers that captures its meaning — similar meanings land close together in that vector space.

embed_example.py
“cat on the mat”“feline on the rug”“stock markets fell”“rainy weather today”Vector space — nearby points = similar meaning

Similarity Search Query

query.py

Distance Metrics

Common similarity metrics
metricmeasurestypical use
Cosine similarityAngle between vectors (ignores magnitude)Text embeddings (most common default)
Euclidean (L2)Straight-line distanceImage embeddings, general clustering
Dot productMagnitude-sensitive similarityRecommendation scoring

Approximate Nearest Neighbor (ANN) Indexes

Scanning every vector for every query doesn’t scale. Vector databases use ANN indexes to find very likely nearest neighbors in a fraction of the time:

Popular ANN index types
indexideatrade-off
HNSWNavigable small-world graph of vectorsFast + accurate; more memory
IVFClusters vectors into buckets, searches nearby bucketsGood speed/memory balance
PQ (Product Quantization)Compresses vectors to save memoryLower precision, huge memory savings

Retrieval-Augmented Generation (RAG)

The most common real-world use case today: store document chunks as embeddings, retrieve the most relevant chunks for a user’s question, then feed them to an LLM as context — grounding the model’s answer in your own data instead of relying purely on what it memorized during training.

Pinecone, Weaviate, Milvus, Qdrant, and Chroma are purpose-built vector databases; PostgreSQL (via the pgvector extension), Elasticsearch, and MongoDB Atlas also added vector search on top of their existing engines — a useful option when you don’t want to run a separate database just for embeddings.