Sub topic 9
What a vector database adds beyond an index
This chapter assumes you know how an approximate nearest neighbor (ANN) index such as HNSW or IVF works, what recall means, and how a database index speeds up queries. It also assumes basic Python and SQL. By the end, you should be able to say which parts of a vector search system come from the index and which come from the database around it.
An index is not a database
An index answers one question: given a query vector, which stored vectors are closest? Libraries such as FAISS do this well. You build the index in memory, add vectors, and call search.
That is enough for a notebook. It is not enough for an application. A real application needs answers to questions the index does not address:
- Where do the vectors live after the process restarts?
- How do I attach a title, a URL or a tenant ID to each vector?
- How do I delete one record, or change its text and embedding?
- What happens when two clients write at the same time?
- How do I restrict results to documents a user may see?
- What happens when the data no longer fits on one machine?
A vector database is an ANN index plus the machinery that answers these questions. The index is one component inside it.
The data model
Most vector databases share a similar model, even though the names differ.
| Concept | Meaning | Typical names |
|---|---|---|
| Collection | A set of records that share a vector size and a distance metric | collection, index, table, namespace |
| Record | One stored item | point, row, document, entry |
| ID | A unique key chosen by you or generated | id, primary key |
| Vector | The embedding, with a fixed dimension | vector, embedding |
| Payload | Metadata stored beside the vector | payload, metadata, attributes, columns |
Two rules follow from this model.
First, every vector in a collection has the same dimension. If your embedding model outputs 768 numbers, the collection is declared with 768 dimensions, and inserting a 1024-dimension vector fails.
Second, the metric is chosen when the collection or index is created. If the embedding model expects cosine similarity, you must configure cosine, or normalize vectors and use dot product. Changing the model later usually means building a new collection and re-embedding everything, because vectors from different models are not comparable.
Storing the original content
The embedding is a lossy summary of the source. You cannot rebuild the text from it. So the record usually keeps the text, or a pointer to it, in the payload. Search returns IDs and scores, and the application then needs the content to show or to pass to a language model.
Two common designs exist:
- Store the text and metadata in the vector database payload. This is simple and needs one lookup.
- Store only the ID and vector in the vector database, and keep the full content in another system such as an object store or relational database. This keeps the vector store small, but each query needs a second fetch.
A query is more than a vector
The index call takes a vector and a k. A database query usually takes more.
A typical request carries the query vector, k, an optional filter, the fields to return, and sometimes search parameters such as efSearch or nprobe. The planner decides how to combine the filter with the index. Filtering strategies are covered in the next chapter. For now, note that the planner exists because filtering interacts with the index and a naive approach can return too few results.
Durability and storage
A FAISS index lives in RAM. If the process dies, so does the index, unless you saved it with faiss.write_index. A database must survive crashes.
Most systems use a write-ahead log (WAL). Each write is appended to a durable log first. Then it is applied to the in-memory structures. After a crash, the system replays the log to restore the state. Periodically it writes a snapshot, so replay starts from the snapshot and not from the beginning.
The index itself may be stored in several ways:
- Fully in memory, with the disk used only for recovery. This gives the lowest latency and the highest cost per vector.
- Memory-mapped files, where the operating system keeps hot pages in RAM.
- Disk-based indexes, which keep compressed vectors in memory and full vectors on SSD. These suit datasets much larger than RAM.
Memory use is a real planning figure. Raw float32 vectors take 4 bytes per dimension. One million 768-dimension vectors need about 3 GB before any index overhead. An HNSW graph with 32 links per node adds roughly another 128 to 256 bytes per vector for links alone. Compression such as product quantization reduces the vector part.
Writes, updates and deletes
Indexes handle these operations unevenly, and the database hides the difference.
- Inserts. HNSW supports incremental inserts. Each new vector is linked into the graph. IVF can add vectors to the nearest cluster, but the cluster centers were trained on earlier data. If the data distribution drifts, recall drops until you retrain.
- Deletes. Removing a node from an HNSW graph can break paths through it. Many systems mark the record as deleted (a tombstone), skip it in results, and clean up later during compaction or a rebuild.
- Updates. An update is usually a delete plus an insert. Changing only the payload is cheaper, because the vector and graph stay the same.
Many systems write new data into a small, fast segment and merge segments in the background. A query searches all segments and merges the results. This gives fast writes, but each query touches more than one index.
Metadata and filtering support
The payload is not only for display. The database can index payload fields, so a filter such as category = 'a' does not scan every record. In a relational extension such as pgvector, you use ordinary B-tree indexes next to the vector index. In dedicated systems, you usually declare which fields to index.
This is a major reason to use a database. The next chapter explains why combining the two indexes is harder than it looks.
Scaling out
When one machine is not enough, systems split data in two ways.
- Sharding divides records across nodes. A query goes to every shard, each returns its top
k, and a coordinator merges these into the global topk. Each shard searches only its part, so latency can drop, but every query pays for network calls to all shards. - Replication copies each shard to several nodes. This improves availability and read throughput. It does not increase capacity.
Note that sharding changes recall slightly. Each shard has its own graph or clusters, and each is approximate. The merged result is usually close to a single large index, but you should measure it.
Access control and multi-tenancy
An application serving many customers must keep their data apart. Options include:
- One collection per tenant. Isolation is strong, but thousands of small collections cost memory and management effort.
- One shared collection with a
tenant_idpayload field and a mandatory filter. This scales well, but a missing filter leaks data across tenants. Enforce it in a server-side layer, not in client code. - Partitions or namespaces inside one collection, which many systems offer as a middle option.
The same idea in SQL
A relational extension shows the database features plainly. The pgvector example stores vectors in a table, and Postgres supplies durability, transactions and access control.
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE items (
id bigserial PRIMARY KEY,
category text,
embedding vector(3)
);
INSERT INTO items (category, embedding) VALUES
('a', '[1, 0, 0]'),
('b', '[0, 1, 0]'),
('a', '[0.9, 0.1, 0]');
CREATE INDEX ON items USING hnsw (embedding vector_cosine_ops);
SELECT id, embedding <=> '[1, 0, 0]' AS cosine_distance
FROM items
WHERE category = 'a'
ORDER BY embedding <=> '[1, 0, 0]'
LIMIT 2;The table is the collection. The category column is the payload. The <=> operator is cosine distance, and the operator class vector_cosine_ops must match it, otherwise the planner will not use the index for that ordering. A transaction can update the row and its embedding atomically, which many dedicated stores do not offer in the same form.
Common errors
- Metric mismatch. The index is built with L2, but the model expects cosine. Results look plausible but are ranked wrongly.
- Dimension mismatch. The collection was created for one model, then a different model is used for queries.
- Mixing models. Documents embedded with model A are searched with model B vectors. Scores are meaningless.
- Forgetting that the index is approximate. A missing result may be a recall miss, not a data problem. Compare against an exact search on a sample.
- Assuming deletes free memory at once. Tombstoned records still occupy space until compaction.
- Treating the vector store as the system of record. Keep the source data elsewhere, so you can re-embed when the model changes.
What the database adds
| Need | Bare index | Vector database |
|---|---|---|
| Persistence | Manual save and load | Write-ahead log and snapshots |
| Metadata | Separate lookup by you | Stored and indexed payload |
| Filtering | None | Planner combines filter and index |
| Updates | Often rebuild | Inserts, deletes, compaction |
| Concurrency | Your responsibility | Managed reads and writes |
| Scale | One machine | Sharding and replication |
| Security | None | Authentication, roles, tenancy |
None of this makes search itself better. The recall and latency still come from the index and its parameters. The database makes the index usable in a running service.
Exercise
A team stores 2 million 1024-dimension float32 embeddings in a collection. They plan to hold the raw vectors in RAM. About how much memory do the raw vectors need, before any index overhead? Then name one reason they should still keep the source documents outside the vector database.
Show answer
Each vector takes 1024 × 4 = 4096 bytes, so 2,000,000 vectors need about 8.2 GB. They should keep the source documents elsewhere because embeddings cannot be turned back into the original content, and if they switch to a new embedding model they must re-embed every document from the source.
Exercise
A multi-tenant application uses one shared collection with a tenant_id field. A developer forgets to add the tenant filter in one code path. What is the risk, and where should the filter be enforced?
Show answer
The query can return records from other tenants, which leaks data. The filter should be enforced in a server-side layer (or by database-level access rules such as row-level security) so that no client code path can skip it.