Background

1. Vector Database

5 min read

A specialized database designed to store, index, and retrieve high-dimensional vectors (embeddings) efficiently. It is the backbone of semantic memory for modern AI systems.

Vector Database


Table of Contents


What is a Vector Database?

Traditional databases store structured data and query it using exact matches (e.g., WHERE name = 'Alice'). Vector databases are fundamentally different — they store data as high-dimensional numerical vectors and retrieve results based on semantic similarity rather than exact matches.

This makes them perfect for use cases where meaning matters more than keywords:

Traditional DB Vector DB
Exact keyword match Semantic similarity search
Structured (rows & columns) Unstructured (text, images, audio)
SQL queries Nearest-neighbour queries
Fast for exact lookups Fast for similarity lookups

How It Works

Raw Data (Text / Image / Audio)
        │
        ▼
  Embedding Model
  (e.g., OpenAI, Cohere)
        │
        ▼
  High-Dimensional Vector
  e.g., [0.21, 0.83, -0.11, ..., 0.35]  (768 or 1536 dimensions)
        │
        ▼
  Stored in Vector Database
  with metadata { id, source, timestamp, ... }
        │
        ▼
  Similarity Search (ANN query)
        │
        ▼
  Top-K Most Relevant Results

Step-by-step:

  1. Data Ingestion — Raw data (text, image, audio, video) is fed into the pipeline.
  2. Embedding Generation — An embedding model converts the data into a numerical vector.
  3. Storage — The vector and its metadata are stored in the vector database.
  4. Query — At query time, the user's query is also embedded and compared against stored vectors.
  5. Retrieval — The database returns the top-K most similar vectors using an ANN (Approximate Nearest Neighbour) algorithm.

Vector Representation & Similarity

In a high-dimensional vector space, semantic meaning maps to geometric proximity:

        Vector Space (simplified 2D)
        
   [AI]  ●─────● [Machine Learning]
          \   /
           \ /
            ● [Deep Learning]
            
            
                        ● [Pizza Recipe]

Items with similar meaning cluster together. This allows queries like:

  • "Find all documents similar to this one"
  • "What products are most similar to what this user liked?"

Similarity metrics used:

  • Cosine Similarity — measures the angle between vectors (most common for text)
  • Euclidean Distance — measures straight-line distance
  • Dot Product — efficient for normalized vectors

Indexing Algorithms

Brute-force comparison across millions of vectors is too slow. Vector databases use Approximate Nearest Neighbour (ANN) indexing:

Algorithm Description Best For
HNSW (Hierarchical Navigable Small World) Graph-based, high recall, fast query General purpose, production
IVF (Inverted File Index) Clusters vectors, searches relevant clusters Large datasets
Flat Brute-force exact search Small datasets, max accuracy
PQ (Product Quantization) Compresses vectors to save memory Memory-constrained systems

Database Type Highlights
Pinecone Managed cloud Easy setup, auto-scaling, production-ready
Weaviate Open-source / Cloud GraphQL API, built-in ML models
Qdrant Open-source / Cloud High performance, Rust-based
Chroma Open-source Lightweight, great for local dev & RAG
Milvus Open-source Highly scalable, cloud-native
pgvector PostgreSQL extension Add vector search to existing Postgres DB
Redis VSS In-memory Ultra-fast, existing Redis infrastructure

When to Use a Vector Database

Semantic search — Search by meaning, not just keywords
RAG (Retrieval Augmented Generation) — Ground LLMs in your private data
Recommendation systems — "People who liked X also liked Y"
Duplicate detection — Find near-duplicate documents or images
Anomaly detection — Identify data points far from known clusters
Chatbots with memory — Store and retrieve past conversation context


Code Example

A minimal example using Chroma (Python):

import chromadb
from chromadb.utils import embedding_functions

# Initialize client
client = chromadb.Client()

# Use OpenAI embeddings
openai_ef = embedding_functions.OpenAIEmbeddingFunction(
    api_key="YOUR_API_KEY",
    model_name="text-embedding-3-small"
)

# Create a collection
collection = client.create_collection(
    name="ai_docs",
    embedding_function=openai_ef
)

# Add documents (embeddings are auto-generated)
collection.add(
    documents=[
        "Vector databases store embeddings for semantic search.",
        "RAG combines retrieval with language model generation.",
        "Embeddings are numerical representations of meaning."
    ],
    ids=["doc1", "doc2", "doc3"]
)

# Query by semantic similarity
results = collection.query(
    query_texts=["How do I search by meaning?"],
    n_results=2
)

print(results["documents"])
# → Returns the 2 most semantically similar documents

Key Takeaway

Vector Databases store data as vectors (embeddings) in high-dimensional space, enabling machines to understand meaning and find the most relevant results — powering semantic search, RAG, recommendations, and modern AI memory systems.


Back to Overview | Next: Embedding Model →