Module 7 · AI infrastructureLesson 81 of 887 minAdvanced
Vector databases
A vector database stores embeddings and finds the nearest ones to a query fast, using approximate indexes instead of comparing against everything.
Search with every point checked, then turn on the index and compare the number of distance checks.
Once you have embeddings, you need to store them and answer "which stored vectors are closest to this one?" quickly. That's what a vector database does.
Why plain search doesn't scale
Comparing a query with every stored vector (brute force or flat search) is exact and fine for thousands of vectors. At 100 million vectors with 1,536 numbers each, it's billions of operations per query.
Approximate indexes
Vector databases trade a little accuracy for a lot of speed:
- IVF (inverted file): cluster vectors into groups. At query time, find the nearest group centres, then search only those groups. More groups searched means better recall, slower queries.
- HNSW (hierarchical navigable small world): a multi-layer graph where each vector links to its near neighbours. Search starts at the top and greedily hops closer. Very fast and accurate; uses more memory. The most popular choice.
- Quantization (PQ): compress vectors so more fit in memory, at some accuracy cost.
The key metric is recall: what fraction of the true nearest neighbours the index finds.
Features you'll need
- Metadata filtering: "nearest documents, but only from this customer, in English". Filtering well with ANN is surprisingly hard; check how a database handles it.
- Hybrid search: combine vector similarity with keyword search (BM25) for names, codes and exact terms that embeddings handle poorly.
- Updates and deletes, replication and sharding, like any database.
Options
Dedicated: Pinecone, Weaviate, Qdrant, Milvus. Extensions to databases you may already run: pgvector for PostgreSQL, vector search in Elasticsearch, OpenSearch, Redis and MongoDB. For many apps, pgvector next to your existing data is the simplest start.