Reference notes.
Vector databases are specialised systems for storing and querying high-dimensional vectors (embeddings). They enable semantic search by finding vectors similar to a query vector.
Core Concepts
Vector Search
Given a query vector, find the K most similar vectors in the database.
Similarity metrics:
- Cosine similarity — Angle between vectors (most common for text)
- Euclidean distance — Straight-line distance
- Dot product — Magnitude-aware similarity
Exact vs Approximate Search
Exact (brute force):
- Compare query to every vector
- complexity
- Perfect accuracy
- Only viable for small datasets
Approximate Nearest Neighbour (ANN):
- Trade accuracy for speed
- (HNSW) or (IVF) typical
- 95–99% recall is usually acceptable
- Essential for production scale
ANN Algorithms
HNSW (Hierarchical Navigable Small World)
Graph-based algorithm. Builds a multi-layer graph of connections.
- Fast queries
- Good recall
- Higher memory usage
- Most popular in-memory choice
DiskANN / Vamana
SSD-backed graph index storing the full graph and vectors on NVMe disk while keeping a compact quantised representation in RAM.
- Scales to billions of vectors at a fraction of HNSW’s RAM cost
- Low latency on modern NVMe SSDs
IVF (Inverted File Index)
Clusters vectors, searches only relevant clusters.
- Lower memory than HNSW
- Tuneable speed/recall trade-off
- Works well with quantisation
Quantisation (SQ, BQ, PQ)
Compresses vectors to dramatically reduce RAM usage and accelerate distance computation (often paired with HNSW or IVF, with optional full-precision rescoring of top candidates):
- Scalar Quantisation (SQ) — Compresses FP32 coordinates to INT8 (~4× memory reduction, minimal recall loss)
- Binary Quantisation (BQ) — Reduces each dimension to 1 bit (~32× reduction; very fast via Hamming/popcount, works well on high-dimensional embeddings with oversampling + rescoring)
- Product Quantisation (PQ) — Splits vectors into subvectors encoded as cluster centroids (high compression, commonly combined with IVF as IVF-PQ)
Flat
No indexing, brute force search.
- Perfect recall
- Use for small datasets or ground truth
Vector Databases
Purpose-built
| Database | Highlights |
|---|---|
| Pinecone | Fully managed, serverless option, easy to use |
| Weaviate | Open-source, gRPC/REST (v4) and GraphQL APIs, hybrid search |
| Qdrant | Open-source, Rust-based, filtering, scalar/binary quantisation |
| Milvus | Open-source, highly scalable, DiskANN support |
| Chroma | Lightweight, embedded, great for prototyping |
| LanceDB | Embedded, columnar, serverless |
Object-Storage Native / Serverless
Store vector indices directly on cloud object storage (S3/GCS) with SSD/RAM caching for cold-tier economics and massive multi-tenancy:
- Turbopuffer — Object-storage-native vector and full-text search
- AWS S3 Vectors — Native vector buckets and similarity queries directly inside Amazon S3
Extensions to Existing Databases
| Database | Extension |
|---|---|
| PostgreSQL | pgvector, VectorChord (vchord) |
| Redis | Redis Stack |
| Elasticsearch | Dense vector field |
| MongoDB | Atlas Vector Search |
| SQLite | sqlite-vec |
When to Use What
Pinecone/Weaviate/Qdrant — Production workloads, need scale
Turbopuffer / AWS S3 Vectors — Cost-sensitive or heavily multi-tenant workloads suited to object-storage tiers
Chroma/LanceDB — Prototyping, embedded use cases
pgvector / VectorChord — Already using PostgreSQL, moderate to large scale
Redis — Need caching alongside vectors
Key Features
Filtering (Metadata)
Query with both vector similarity AND attribute filters:
Find vectors similar to query
WHERE category = "electronics"
AND price < 100
Pre-filtering vs post-filtering affects performance and recall.
Hybrid Search
Combine vector search with keyword search (BM25). Usually merged with Reciprocal Rank Fusion (RRF).
Multi-tenancy
Isolate data between users/organisations:
- Namespace-based isolation
- Metadata filtering
- Separate collections
Sparse Vectors
Some databases support sparse vectors for keyword matching alongside dense vectors.
Architecture Considerations
Embedding Dimensions
Higher dimensions = more storage and slower queries.
- 384d: Lightweight, good for simple use cases
- 768-1024d: Common middle ground
- 1536-3072d: High quality, more resources
Index Build Time
HNSW index building is expensive. Plan for:
- Initial bulk load time
- Incremental updates
- Index rebuild strategies
Memory vs Disk
- HNSW typically memory-resident
- IVF can use disk with memory-mapped files
- Quantisation reduces memory requirements
Sharding
For large-scale deployments:
- Partition by ID range or hash
- Partition by metadata
- Managed services handle this automatically
Operations
Indexing Pipeline
Documents → Chunking → Embedding → Vector DB
↓
Metadata extraction
Update Strategies
- Full reindex — Simple but slow
- Incremental — Add/delete individual vectors
- Batch upsert — Efficient bulk updates
Backup & Recovery
- Point-in-time snapshots
- Continuous replication (managed services)
- Export/import functionality
Evaluation
Recall@K
What fraction of true nearest neighbours are returned in top K results?
Query Latency
p50, p95, p99 latencies under load.
Throughput
Queries per second at acceptable latency.
Build Time
Time to index N vectors.
Memory Usage
RAM required for index + vectors.
Benchmarks
- ANN Benchmarks — Algorithm comparison
- Vector DB Benchmark — Database comparison
Integration
Frameworks
- RAG implementations (LangChain, LlamaIndex)
- Direct SDK usage
Example (pgvector)
CREATE EXTENSION vector;
CREATE TABLE documents (
id SERIAL PRIMARY KEY,
content TEXT,
embedding vector(1536)
);
CREATE INDEX ON documents
USING hnsw (embedding vector_cosine_ops);
SELECT content
FROM documents
ORDER BY embedding <=> '[query_vector]'
LIMIT 10;Note: pgvector HNSW indices on vector (FP32) support up to 2,000 dimensions. For higher-dimensional embeddings (e.g. 3072d), use halfvec (FP16, up to 4,000 dimensions), bit (binary quantisation, up to 64,000 dimensions), or Matryoshka dimension truncation.
Example (Chroma)
import chromadb
client = chromadb.Client()
collection = client.create_collection("docs")
collection.add(
documents=["Document 1", "Document 2"],
ids=["id1", "id2"]
)
results = collection.query(
query_texts=["search query"],
n_results=5
)