What it is
A vector database stores vectors (with an id, the source text and metadata) and answers similarity queries: given a query vector, return the k stored vectors closest to it by cosine similarity, dot product or Euclidean distance. Under the hood it is an approximate nearest-neighbour (ANN) index plus the usual database concerns: persistence, filtering, replication, sharding and writes.
Why it exists
Semantic search and retrieval-augmented generation reduce text to vectors and look for close ones. Comparing a query with every stored vector is exact but linear in the data: 10 million 768-dimensional vectors cost billions of multiply-adds per query. ANN indexes make that sub-linear, at the price of occasionally missing a true neighbour. A vector database packages such indexes so applications do not have to.
How it works
HNSW. Hierarchical navigable small world graphs are the most widely used ANN index. Every vector is a node linked to up to M close neighbours. Higher layers contain exponentially fewer nodes and act as long-distance shortcuts. A search starts at an entry point in the top layer, greedily moves to whichever neighbour is closest to the query, drops a layer when it cannot improve, and at the bottom layer keeps a candidate list of size efSearch to collect the final top-k. Inserts walk the graph the same way to find neighbours to link.
Recall. Quality is measured as recall@k against exact search on a sample of real queries. Knobs: efSearch at query time (more candidates, better recall, more CPU), M and efConstruction at build time (more links, better recall, more memory and slower inserts). Recall depends on the data, so measure it; typical targets are in the 0.9–0.99 range.
Other index types. IVF clusters vectors and searches only the nearest clusters (nprobe). DiskANN-style indexes keep the graph on SSD for indexes bigger than RAM. Flat (exact) indexes are fine for small collections or heavily filtered subsets.
Quantisation. Store vectors in fewer bits: float16, int8 (a quarter of float32), binary (a thirty-second) or product quantisation. Memory and bandwidth drop sharply; recall drops a little, often recovered by re-scoring the top candidates with full-precision vectors.
Filtering. Applying a metadata filter after the ANN search (post-filtering) returns too few results when the filter is selective. Filtered search applies the condition during traversal, and planners switch to an exact scan of the matching subset when it is small. pgvector added iterative index scans (0.8) for the same reason.
Replicas and shards. ANN queries are CPU-bound and want the index in memory. Replicas hold full copies and multiply read throughput. Shards split the collection; a query fans out to every shard in parallel, each returns its local top-k, and a coordinator merges them, so latency follows the slowest shard.
When to use it
- Semantic search and RAG over more vectors than an in-process index handles comfortably.
- When you need filtering, multi-tenancy, replication and operational tooling around ANN search.
- Recommendations and deduplication by similarity (near-duplicate detection, “more like this”).
When not to
- Thousands to a few million vectors with modest traffic: an extension or vector type in the database you already run (pgvector in PostgreSQL, vector support in SQL Server 2025, Azure SQL, MongoDB, Redis or your search engine) avoids a new system and keeps data and permissions in one place.
- Exact matches or keyword queries: a B-tree or full-text index does it better.
- When results must be exact and complete (compliance search): ANN can miss items by design.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Higher efSearch | Better recall | More CPU per query, higher latency |
| Higher M / efConstruction | Better recall ceiling | More memory, slower inserts and builds |
| Quantisation | Several-fold less memory | Some recall, unless you re-score |
| Replicas | Read throughput, availability | Full index in RAM on every replica; writes applied everywhere |
| Shards | Indexes bigger than one machine | Fan-out on every query; tail latency of the slowest shard |
| Dedicated vector DB | Scale, features, performance | Another system to run and keep in sync |
Alternatives
- Keyword search (BM25) alone, or combined with vectors (hybrid search): strong for exact terms, codes and names that embeddings blur.
- In-process libraries (FAISS, hnswlib and similar): fastest for a single service with a static index; you handle persistence and scaling.
- Managed search services with vector fields: one system for keyword, vector and filtered queries.
Failure modes
- Silent recall loss: a parameter change, a new embedding model or data drift lowers recall; nothing errors, answers just get worse. Monitor recall on a fixed query set.
- Selective filters return nothing: post-filtering or a planner that guesses wrong.
- Memory pressure: the index outgrows RAM and pages to disk; latency jumps by orders of magnitude.
- Hot shard: one shard is slower (more data, noisy neighbour, compaction) and every fan-out query waits for it.
- Delete debt: deletes as tombstones bloat the graph and slow queries until compaction or a rebuild.
- Replica lag: a vector written a moment ago is visible on one replica and not another.
In production
Track P50/P95 query latency, queries per second per node, memory headroom and, crucially, recall on an evaluation set after every index or model change. Size capacity per node as cores ÷ CPU time per query at your efSearch, and memory as vectors × dimensions × bytes per dimension plus graph overhead. Put tenant and permission fields on every vector and filter inside the search. Plan index rebuilds (new model, new parameters) as blue/green migrations with an alias switch.
Common misconceptions
- "ANN is just a faster exact search." It returns approximately the nearest neighbours; recall below 1 is the deal.
- "Replicas let the index use less memory." Each replica is a full copy; shards or quantisation reduce memory.
- "Filters are free." Selective filters change which algorithm is efficient and can wreck recall if applied afterwards.
- "We need a vector database for RAG." Often a vector column in the database you already run is enough.
- "Cosine similarity score 0.8 means relevant." Scores are only comparable within one model and index; use rank and evaluation, not fixed thresholds across models.
Interview angle
Expect to explain ANN vs exact search, sketch HNSW, and discuss the recall/latency/memory triangle. Estimate memory (count × dimensions × 4 bytes) and decide between replicas, shards and quantisation from it. Explain filtered search for multi-tenancy and permissions, and how you would measure recall. Point out when an existing database with vector support is the simpler choice.
Model assumptions
The simulation is a simplified model, not a benchmark. A vector database node is a server with 4 query slots (cores) and room for 16 waiting queries. Query work is lognormal (cv 0.25): ~400 ms for an exact scan of the illustrative 10-million-vector index, ~8 ms with HNSW at efSearch 40, ~20 ms at efSearch 200. Recall figures are illustrative annotations; the engine does not compute recall. Queries arrive randomly (Poisson). Replicas sit behind a load balancer with health checks. The HNSW descent, metadata filtering and shard fan-out are scripted illustrations, not simulated. Numbers show behaviour and orders of magnitude only.