Vector Database Architecture

How AI Databases Store and Search High-Dimensional Embeddings at Scale

A vector database stores high-dimensional embeddings — floating-point arrays produced by transformer models, CLIP, or sentence-transformers — and answers one question faster than brute force: which stored vectors are closest to a query vector? At 10 million vectors in 1536 dimensions, comparing every pair is 15 billion operations. An approximate nearest-neighbor (ANN) index reduces that to tens of thousands with 95–99% recall. Every production RAG system, semantic search engine, and AI agent back-end runs on this tradeoff.

In 2026, Pinecone, Weaviate, Qdrant, and Milvus collectively serve hundreds of billions of queries per month. Their core differentiation lives in the ANN index they use — almost universally some variant of HNSW (Hierarchical Navigable Small World).

The RAG Retrieval Pipeline

How a query flows from a user's question to retrieved context chunks

Query Text "How does RAFT work?" Embedding Model ↓ 1536-d Vector Database ANN Search (HNSW/IVF) Top-K Chunks Chunk 1: RAFT leader Chunk 2: AppendEntries Chunk 3: Election... k=3 typically LLM Answer w/ context

Key Numbers

1536
Dimensions for OpenAI text-embedding-3-small
3072
Dimensions for text-embedding-3-large
~95%
Typical HNSW recall at ef=128 (vs brute force)
O(log N)
HNSW search complexity vs O(N) brute force
10–40%
Memory overhead of HNSW compared to raw vectors
P99 <10ms
Qdrant's target search latency at 1M vectors
0.96
Cosine similarity threshold commonly used for "relevant"
HNSW
Algorithm powering virtually all 2026 production vector DBs

HNSW: Hierarchical Navigable Small World

HNSW builds a multi-layered graph where upper layers are shortcuts and the bottom layer is a dense "small world" graph. Search starts at the top and greedily descends. Watch the traversal below — the query (yellow) finds nearest neighbors by following edges.

32
3
Click Search to find nearest neighbors in the HNSW graph

How Embeddings Work

An embedding model maps text (or images, audio) to a dense vector in ℝᵈ. Models like text-embedding-3-small, sentence-transformers/all-MiniLM-L6-v2, and OpenAI's CLIP produce vectors where semantic similarity ≈ cosine similarity:

cosine_sim(A, B) = (A · B) / (‖A‖ · ‖B‖)

Two sentences with similar meaning cluster together in the high-dimensional space even if they share no words. "How do I reset my password" and "How to recover account access" land near each other. "SQL injection attack" lands far away.

Cosine vs L2 vs Dot Product

MetricFormulaBest forNote
Cosine(A·B)/(‖A‖‖B‖)Normalized embeddingsRange [-1, 1], ignores magnitude
L2 / Euclidean‖A − B‖When magnitude mattersAffects by vector scale
Dot ProductA · BUnnormalized, raw logitsNot bounded, sensitive to scale

ANN Index Algorithms Compared

HNSW — The Production Standard

HNSW (Malkov & Y Reshetov, 2016) builds a navigable small world graph across L layers. Layer 0 has all N vectors; each upper layer ℓ contains ~N·e^(−ℓ) vertices selected randomly. Long-range edges in upper layers skip vast distances; short-range edges in layer 0 provide precision. Search is greedy: start at entry point, examine ef candidates, move toward closest neighbor, repeat until convergence on layer 0.

IVF: Inverted File Index

IVF partitions vectors into k clusters (k-means on a subset). Each cluster has an inverted list of member vectors. Search restricts comparison to vectors in the nearest w clusters (probe depth). IVF alone is slower than HNSW but combines well with product quantization for memory-constrained deployments.

PQ: Product Quantization

Product quantization splits each d-dimensional vector into m subvectors of d/m dimensions, then runs k-means (typically k=256) on each subvector space independently. Each subvector is replaced by its centroid ID — a single byte. A 1536-d float32 vector (6 KB) becomes 1536/8 = 192 bytes: 32× compression. Search uses an asymmetric distance computation (ADC) to reconstruct approximate distances from quantized codes.

HNSW + PQ = Production Stack

Most production deployments combine HNSW (for topology search) with PQ (for storage compression) and a re-ranking pass (exact distance on top-100 candidates). This is exactly what Pinecone Serverless, Qdrant, and Weaviate do under the hood.

Vector Similarity Explorer

Generate random vectors and measure their distances under different metrics

32
3 query vectors (red), 30 stored vectors (blue), 5 nearest shown (green)

Storage Architecture: How the DB Is Built

Segments and Sharding

At write time, vectors arrive in batches and are written to a mutable segment (like ClickHouse's MergeTree). When a segment fills, it is sealed and its vectors are indexed with HNSW. Sealed segments are immutable — no more inserts or updates. This design allows concurrent reads during indexing, just like LSM-tree databases.

ComponentRoleAnalogy
Write bufferAccumulate vectors before indexingMemTable in RocksDB
HNSW indexANN search over sealed segmentB-Tree in PostgreSQL
Metadata storeIDs, payloads, filter fieldsKey-value lookup index
Segment mergeCompact small sealed segmentsCompaction in LSM

Filtering and Hybrid Search

Most production workloads require filtered search: "find similar vectors where category='docs' and date>'2026-01-01'". Naively applying filters post-search destroys recall. Modern vector DBs handle this via:

  • Pre-filtering: Build a bitset of qualifying IDs before ANN search, intersect with candidates
  • Payload index: Maintain a B-tree or inverted index on payload fields for fast filtering
  • Hybrid search: Combine dense vector search with sparse BM25/keyword search using Reciprocal Rank Fusion (RRF)

Pinecone vs Weaviate vs Qdrant vs Milvus

DBIndexQuantizationFilteringBest for
PineconeHNSW + IVFPQ, binaryPayload indexManaged, serverless, production
WeaviateHNSW + compositePQ (1.22+)BM25 hybridSemantic + keyword hybrid
QdrantHNSW (custom)Scalar + binary PQPayload index + rangedHigh performance, self-hosted
MilvusHNSW, IVF, DiskANNPQ, binaryField indexHighest scale, cloud-native
pgvectorIVF-Flat, HNSWNone (native)PostgreSQL WHERESimpler stacks, existing PG

RAG System Design Patterns

Chunking Strategies

How you split documents dramatically affects retrieval quality. Common approaches:

  • Fixed-size (512 tokens): Simple, consistent, but can split sentences and lose context
  • Recursive character split: Split on newlines, then sentences, preserving semantic units
  • Semantic/chunking by embedding: Cluster sentences by embedding similarity, split at cluster boundaries
  • Document-structure aware: Split on headers, paragraphs — preserve titles as metadata

Parent Document Retrieval

Store both small chunks (for precision retrieval) and their parent document (for full context). On retrieval, fetch the small chunk's nearest neighbors, then return the parent document for richer LLM context. This addresses the "missing surrounding context" problem.

Re-ranking

HNSW returns candidates in ~10ms but at 95% recall. A cross-encoder re-ranker (like cross-encoder/ms-marco-MiniLML-6-v2) scores each candidate against the query with full attention — O(N·d) instead of O(log N). Use it as a second-stage filter: HNSW → top-100 → cross-encoder → top-10 → LLM.

HNSW Build vs Search Tradeoff

Explore how M and efConstruction affect index size, build time, and search quality

16
128
Higher M → larger index, better recall. Higher efConstruction → slower build, better recall.

Quantization: Reducing Memory Footprint

Scalar Quantization (SQ8)

Each 32-bit float is linearly mapped to an 8-bit integer: int8 = round(float / scale). The scale factor (one per vector, or per block of 128–256 vectors) is stored separately. SQ8 achieves ~4× compression with <1% accuracy loss for cosine similarity.

Product Quantization (PQ)

Split the d-dimensional vector into m subvectors of d/m dimensions. Run k-means on each subvector space with k=256 centroids. Replace each subvector by its nearest centroid ID (1 byte). The centroid vectors (256 × d/m × 4 bytes each) are stored in a codebook. For d=768, m=8: each vector = 8 bytes + codebook ~8 KB per 256 vectors.

Binary Quantization (BQ)

Threshold each float at 0: bit=1 if positive else 0. Hamming distance (bit XOR + count) replaces cosine similarity. Achieves 32× compression. Works surprisingly well for nearest neighbor ranking on normalized embeddings.

Choosing Quantization

MethodCompressionAccuracy lossSpeedUse case
SQ8<1%FastMemory-constrained production
PQ8–64×2–5%FastestLarge-scale retrieval (>10M vectors)
BQ32×5–15%FastestCandidate pre-filtering
None (FP32)BaselineBaselineGolden benchmark