Intermediate · Fundamentals
Vector Database
Visual diagram · Math · Worked example · 3 difficulty levels.
TL;DR. Vector databases store and search data as mathematical vectors (embeddings), enabling fast similarity search for AI applications like semantic search and RAG.
Intuitive Explanation
Regular databases search by exact matches — like looking up a name in a phone book. Vector databases search by similarity — like asking 'find me songs that sound like this one.' They organize data by meaning, not by keywords.
Technical Definition
A vector database is a specialized data management system optimized for storing, indexing, and querying high-dimensional vector embeddings using approximate nearest neighbor (ANN) algorithms. It provides sub-linear search over millions-to-billions of vectors using indexes like HNSW, IVF, or PQ.
How it works
Vector databases are a critical infrastructure component for modern AI applications. **Why Vector Databases:** Traditional databases use exact matching (SQL WHERE clauses). But AI works with embeddings where you need to find the closest vectors — approximate nearest neighbor (ANN) search. **Core Algorithms:** - **HNSW (Hierarchical Navigable Small World):** Graph-based index. Best recall/speed tradeoff. O(log n) search. - **IVF (Inverted File Index):** Clusters vectors, searches only relevant clusters. Good for very large datasets. - **PQ (Product Quantization):** Compresses vectors for memory efficiency at the cost of some accuracy. - **Flat/Brute Force:** Exact search, O(n). Only viable for small datasets. **Key Features:** - **Metadata Filtering:** Combine vector similarity with attribute filters (date, category, source) - **Hybrid Search:** Combine dense vectors with sparse (BM25) retrieval - **Multi-tenancy:** Isolate data between users/organizations - **Real-time Updates:** Add/delete vectors without rebuilding the entire index **Major Players:** - **Pinecone:** Fully managed, serverless - **Weaviate:** Open-source, supports multimodal - **Qdrant:** Open-source, Rust-based, high performance - **Milvus:** Open-source, battle-tested at scale - **pgvector:** PostgreSQL extension (good enough for many use cases) - **Chroma:** Lightweight, developer-friendly, great for prototyping
Mathematical Notation
similarity(a, b) = (a · b) / (‖a‖ · ‖b‖) (cosine similarity)Cosine similarity measures the angle between two vectors, ignoring magnitude. A value of 1 means identical direction (semantically identical), 0 means orthogonal (unrelated), and -1 means opposite. Other distance metrics include Euclidean distance and dot product.
Visual Explanation (flowchart)
Raw Data → Embedding Model → Vectors [0.12, -0.45, ...] → Vector Database (Index) → Query Vector → ANN Search → Top-K Similar Results
Real-World Use Cases
- Notion: Powers Notion AI's ability to search and answer questions about workspace content
- Spotify: Uses vector similarity for music and podcast recommendations
- Shopify: Product search uses embeddings to find semantically similar items
- GitHub: Copilot uses vector search to find relevant code snippets
Related Concepts
- Large Language Model (LLM) — A massive neural network trained on vast text corpora to understand and generate human language with remarkable fluency.
- Embedding — A dense vector representation that captures semantic meaning, mapping discrete items like words into continuous mathematical space.
- Retrieval-Augmented Generation (RAG) — A technique that enhances LLM responses by retrieving relevant documents from an external knowledge base before generating an answer.