AlgoInsight

Q&A

What are vector databases and embeddings?

By AlgoInsight Team2026-06-18algorithmsdatabases

Overview

Traditional databases search by exact match or keyword overlap; vector databases search by meaning. By converting content to embeddings and storing them in a vector DB, you can find the most semantically relevant documents for a query - even if they don't share keywords - by measuring geometric distance in embedding space.

graph LR
    A["Text: 'How do I reset my password?'"] --> B["Embedding model"]
    B --> C["[0.23, -0.87, 0.42, ...]<br/>768-dim vector"]
    C --> D["Vector DB: ANN search"]
    D --> E["Top-k nearest vectors"]
    E --> F["Similar documents returned"]
    style D fill:#1e1b4b,color:#fff

What Are Embeddings?

Embedding models (trained transformers) encode text into dense vectors such that:

Creating Embeddings (Python)

from openai import OpenAI
client = OpenAI()

response = client.embeddings.create(
    input="How does retrieval-augmented generation work?",
    model="text-embedding-3-small"
)
vector = response.data[0].embedding   # list of 1536 floats

Or with a local model:

from sentence_transformers import SentenceTransformer
model = SentenceTransformer("BAAI/bge-large-en-v1.5")
vector = model.encode("How does RAG work?")   # numpy array

Similarity Measures

Metric Formula Use
Cosine similarity `(A·B)/( A
Dot product A·B Fast; valid when vectors are normalized (= cosine then)
Euclidean distance √Σ(aᵢ−bᵢ)² Magnitude matters; less used for text

For text embeddings, cosine similarity (or dot product after L2 normalization) is standard.

How Vector Databases Work

Searching billions of vectors by brute-force exact nearest neighbour is O(N×d). Vector databases use Approximate Nearest Neighbour (ANN) indexing algorithms to make this fast:

Key ANN Algorithms

Popular Vector Databases

Database Type Notes
Pinecone Managed SaaS Easy setup, scalable, popular for RAG
Weaviate Open-source / cloud Multi-modal, built-in ML
Qdrant Open-source / cloud Fast, rich filtering
Chroma Open-source (local) Easy to use, great for prototyping
pgvector PostgreSQL extension Keep everything in Postgres
FAISS Library (Meta) In-memory, GPU-accelerated, not a server
Milvus Open-source Production-grade, Kubernetes-native

Metadata Filtering

Production search often combines vector similarity with structured filters:

# Retrieve semantically similar results but only from a specific category:
results = vectorstore.similarity_search(
    query="password reset",
    k=5,
    filter={"category": "support", "language": "en"}
)

This hybrid approach (dense retrieval + metadata filter) is more precise than pure semantic search.

Embedding Dimensions and Trade-offs

Larger dimension → more expressive (captures more nuance) but slower to search and store. Common choices: 384 (fast/small), 768 (balanced), 1536–3072 (OpenAI large models). For production, benchmark your specific task to find the accuracy/cost sweet spot.

Why This Matters in Interviews

Vector databases are a core component in every RAG and AI search system. Cover: embeddings capture semantic meaning as dense vectors, cosine similarity measures closeness, ANN algorithms (especially HNSW) make search fast at scale, and the choice between managed (Pinecone) vs self-hosted (Qdrant, Chroma) vs SQL extension (pgvector). Explaining the index-query pipeline and metadata filtering shows production awareness.

Common Pitfalls

References

More answers