Lesson 4.4: Vector stores and indexes
Learn what a vector store does, how flat, clustered and graph indexes trade speed against recall and memory, how to size one, and how to choose and operate it.
You now know how to turn text into vectors, how to search them and how to cut documents into chunks. The remaining piece of the retrieval puzzle is where all this lives and how it stays fast. A vector store, often called a vector database, is the system that holds your vectors, their text and their metadata, builds the helper structures called indexes and answers similarity queries quickly. Choosing and running one is a practical engineering decision, with real consequences for cost, speed and the quality of answers. This lesson explains what the common index types do, in plain terms, and gives you the arithmetic to size and compare them. By the end you will be able to describe what a vector store stores and does, explain the three main kinds of index (flat, clustered and graph-based) and the trade-off each makes, calculate roughly how many comparisons a clustered index saves and how much memory a collection and its index need, explain how compression reduces memory and what it costs, handle updates, deletes and model changes, choose between an in-process library, a database extension and a dedicated service, and set up a simple plan to test a store on your own data. You should have read Lessons 4.1 to 4.3 in this track. You do not need to be a database expert. The arithmetic is multiplication and division, and the simulators do it for you.
What a vector store does
At its heart, a vector store does four jobs.
1. Stores records. Each record holds a vector, usually the original text of the chunk and metadata such as the document title, date, owner and language, as Lesson 4.3 described.
2. Builds indexes. It organises the vectors in a structure that makes nearest-neighbour search fast, as Lesson 4.2 explained.
3. Answers queries. Given a query vector, a number k and optional filters on the metadata, it returns the k closest records with their scores.
4. Keeps the data up to date. It lets you add new records, change existing ones and delete others, and it persists everything to disk so a restart loses nothing.
Beyond these, most stores offer extras: keyword search for hybrid queries, access control, backups, multiple collections and monitoring. How much of this you need depends on your product.
It is worth being clear about what a vector store is not. It does not create embeddings. You do that with an embedding model, and send the vectors to the store. It does not understand your documents. And it is not a replacement for your main database: it holds a searchable copy of content that probably also lives somewhere else.
Three families of index
Lesson 4.2 introduced exact search, which compares a query with every vector, and approximate search, which examines only a part of the collection. The index decides which part. Three families cover most real systems.
Flat: no index at all
A flat index simply stores the vectors in a list and compares the query with all of them. It is exact, so its recall is 100 per cent. It needs no building time, handles updates trivially and uses no extra memory beyond the vectors. Its cost is the one from Lesson 4.2: time proportional to the number of vectors.
Flat is a good choice more often than people expect. For up to a few hundred thousand vectors, a flat search can be fast enough, and its simplicity and perfect recall are worth a lot. It is also the benchmark against which other indexes are judged: to measure recall, you compare an approximate result with the flat result.
Clustered: look only in the nearest groups
A clustered index, commonly called an inverted file or IVF index, works like a library with sections. In advance, it groups the vectors into clusters of similar vectors. Each cluster has a centre, a representative vector called a centroid. To answer a query, the index first compares the query with the centroids, which is cheap because there are only a few, picks the few nearest clusters and then searches only the vectors inside those clusters.
Two numbers control it: the number of clusters, often written nlist, and the number of clusters you examine per query, often written nprobe.
A worked example. A collection has 1,000,000 vectors, grouped into 1,000 clusters, so each holds about 1,000 vectors. A query compares itself with the 1,000 centroids and then, with nprobe set to 10, searches the 10 nearest clusters, about 10,000 vectors. The total is about 1,000 + 10,000 = 11,000 comparisons, against 1,000,000 for flat search. That is about 91 times fewer.
The catch is recall. If the true nearest neighbour lies in a cluster that was not among the 10 examined, because the query sits near the border between clusters, the index misses it. Raising nprobe examines more clusters, which finds more of the true neighbours and costs more time. At nprobe of 1,000, every cluster is searched, and the result is exact, but there is no saving. So nprobe is a dial between speed and recall.
Graph-based: hop towards the answer
A graph index, of which the most widely used is called HNSW (hierarchical navigable small world), connects each vector to a handful of its near neighbours, forming a network. To search, you start at an entry point and repeatedly move to the neighbour that is closest to the query, like following signposts, until you cannot get closer. To make the journey short, the graph has several layers: a sparse top layer lets you take long strides across the collection, and denser lower layers refine the position.
The result is that a query examines only a small, well-chosen part of the collection, often a few hundred vectors out of millions, and still reaches the true neighbours very often. Graph indexes usually give a better balance of speed and recall than clustered ones, which is why they are popular.
They have two main settings. One controls how many neighbours each vector is linked to, often written M. More links make a denser, better graph, with higher recall and more memory. The other controls how widely a query explores as it searches, often written efSearch. A larger value searches more, with higher recall and slower queries. The settings are dials, as nprobe is for clustered indexes.
The costs of graphs are memory and build time. Each vector carries its list of links, and building the graph means inserting vectors one at a time, finding its neighbours as you go. Updates are possible but a heavily changing collection can degrade the graph over time, so some systems rebuild or compact periodically. Deleting is also awkward, as the next section describes.
Which to choose?
· Flat · Clustered (IVF) · Graph (HNSW)Recall · 100% · Adjustable, often high · Adjustable, often high
Speed on large data · Slow · Fast · Very fast
Extra memory · None · Small (centroids) · Substantial (links)
Build time · None · Moderate (clustering) · Longer
Updates · Trivial · Fine, may need re-clustering · Fine, may degrade
Best for · Up to a few hundred thousand vectors, or when exactness matters · Very large collections, memory-limited · Large collections where speed and recall both matter
These are generalisations. Real systems also combine ideas, for example a clustered index with compressed vectors, and the best choice depends on your data. The rule is to start simple, with flat, and move to an approximate index only when measurements show you need it.
Counting the savings from clustering
The savings from a clustered index are easy to compute. The simulator below counts the comparisons.
A common rule of thumb is to choose the number of clusters near the square root of the number of vectors, which keeps both the centroid comparisons and the in-cluster comparisons modest. For one million vectors, that is about 1,000. It is a starting point, not a law.
Sizing memory
Speed is one concern. Memory is the other, and it often decides what you can afford. A vector store usually keeps its index, and ideally its vectors, in memory for fast access, because reading from disk is much slower.
Start with the vectors. As Lesson 4.1 showed, each takes dimensions × bytes per number. For 10,000,000 vectors of 768 dimensions at 4 bytes, that is 10,000,000 × 768 × 4 = 30.7 billion bytes, about 30.7 gigabytes.
Then add the index. A flat index adds nothing. A clustered index adds the centroids, which are tiny. A graph index adds the links. If each vector has about 2 × M links at the bottom layer, and each link is a 4-byte number, then with M = 16 that is 32 links × 4 bytes = 128 bytes a vector, or 1.28 gigabytes for 10 million vectors. That is small beside the vectors themselves at 768 dimensions, though it matters more when vectors are small. Add the text and metadata, which can be larger than the vectors if chunks are long, and some working space.
Compression can shrink the vectors. The simplest form, scalar quantisation, stores each number in fewer bits: 8 bits instead of 32 gives a four-fold reduction. A more aggressive form, product quantisation, splits each vector into parts and replaces each part with a short code from a shared table, and can shrink vectors by 10 to 30 times or more. The price is accuracy. Compressed vectors are approximations, so distances are slightly off and recall falls, sometimes by a few points. A common remedy is to use compressed vectors to find candidates quickly and then re-score the best few with the original full-precision vectors. As always, measure recall on your own data before and after.
If the numbers are too big for one machine, there are several options: compress, reduce dimensions if your embedding model allows it, keep text on disk and only vectors in memory, store the index on fast disk with a smaller in-memory part or split the collection into shards across machines. Each has a cost in complexity, speed or accuracy.
Updates, deletes and changing models
Real data changes. Documents are added, edited and removed. A vector store must cope.
Adding is straightforward for flat and usually efficient for graph and clustered indexes.
Editing a document means replacing the vectors of its chunks. The usual method is to delete the old chunks and add the new ones, using a stable document identifier so that you know which chunks belong to which document.
Deleting is trickier in some indexes. Many systems mark a record as deleted, called a tombstone, and remove it for real later, during a compaction or rebuild. Until then, deleted items may still occupy space. If a deletion is a legal or privacy requirement, check that the system really removes the data, and when.
Freshness. Some stores make new records searchable immediately. Others batch changes, so there is a delay. If your users need to find a document seconds after it is uploaded, check this.
Changing the embedding model is the biggest update of all. Every vector depends on the model that made it, so a new model means re-embedding everything, as Lesson 4.1 warned. Plan for it. Store the model name and version with each vector. Build the new index beside the old one, test it, and switch over when ready, so that you can roll back.
Changing the chunking is similar: it changes what is embedded, so it requires rebuilding.
A tidy design keeps the original documents and their processing settings, so that you can rebuild the vector store from scratch at any time. Treat the vector store as a derived, rebuildable copy, and not as the only home of your content.
Choosing a store
There are many products, and they change quickly, so this lesson will not rank them. Instead, think in categories, and match the category to your needs.
An in-process library. A code library that runs inside your application, holding the index in memory. Simple, fast and free of extra servers. Good for prototypes and for modest collections that fit on one machine. You handle saving to disk and scaling yourself.
An extension to a database you already use. Many general-purpose databases can store vectors and search them. The attraction is one system for everything: your relational data, access rules, backups and vectors together. For many products with up to some millions of vectors, this is the simplest sound choice.
A dedicated vector database. A service built for this job, with advanced indexes, filtering, scaling and monitoring. It makes sense at large scale or with demanding performance needs, though it adds a system to run and pay for.
A managed cloud service. A hosted version, which removes the work of running it, at a price, and with the data sitting with a provider. Check the terms for sensitive data.
Questions to ask when choosing:
1. How many vectors now, and in a year? Under a few hundred thousand, almost anything works.
2. What query speed and recall do you need? Test, do not guess.
3. How complex are your filters? Strict filters need a store that filters during search, as Lesson 4.2 showed.
4. Do you need hybrid search? Keyword plus vector in one system is convenient.
5. How often does the data change? Heavy updates favour some indexes over others.
6. What are the privacy, security and access-control needs? Can it enforce permissions? Where is the data held?
7. What can your team operate? A simpler system that you can run well beats an advanced one that you cannot.
8. What does it cost? Memory, machines, a managed fee and the engineering time.
Testing a store on your own data
A benchmark from a vendor's website tells you little about your data. Run your own test.
1. Take a representative sample of your vectors, or all of them, with real metadata.
2. Build a flat index as the reference, and use it to find the true top 10 for 200 or more real queries.
3. Build the candidate index with a few settings, for example three values of nprobe or efSearch.
4. For each setting, measure the recall at 10 against the flat reference, and the time per query, at the query rate you expect.
5. Plot recall against speed. Choose the setting that meets your recall target at the lowest cost.
6. Test your filters, your updates and your deletes. Time a rebuild.
7. Repeat with more data if you expect to grow.
This gives you the trade-off curve for your own situation, and a number you can defend when someone asks why you chose it.
Three worked examples
Example 1: a small help centre. A software company has 40,000 chunks of 768 dimensions. The vectors take 40,000 × 768 × 4 = 123 million bytes, about 0.12 gigabytes. A flat search takes well under a millisecond on one core at the speeds used in Lesson 4.2. The team uses a vector extension on the database they already run, with a flat index. There is no approximate index, no new service and perfect recall. They revisit when the collection passes a million vectors.
Example 2: a large catalogue. A marketplace has 50 million product vectors of 384 dimensions, at 4 bytes: 50,000,000 × 384 × 4 = 76.8 gigabytes. Too much for one machine's memory at full precision, they quantise to 8 bits, which brings it to 19.2 gigabytes, and use a graph index with M = 16, which adds about 6.4 gigabytes of links, for a total of roughly 25.6 gigabytes before text and headroom. They test recall against a flat reference on 500 queries, and find 96 per cent at 10 for the 8-bit vectors, compared with 98 per cent at full precision, which they judge acceptable for product search. They re-score the top 50 with the full-precision vectors, held on disk, to recover most of the gap.
Example 3: a multi-tenant product. A support platform serves 800 customers, each with its own documents, in one store of 20 million vectors. Every query must be filtered to a single customer. The team finds that post-filtering returns too few results for small customers, and moves to a design in which each customer's vectors form a separate partition, so a query searches only that partition. Queries become faster, results complete and permissions simpler to enforce.
Common mistakes
Adding an approximate index too early. Measure first. Flat search is exact and simple.
Never measuring recall. An approximate index without a recall measurement is a guess.
Copying a vendor's settings, instead of testing on your own data.
Underestimating memory, including the index, the text and the headroom.
Forgetting that deletes may not be immediate. If deletion is a legal requirement, verify it.
Not storing the model name with the vectors, which makes model changes painful.
Treating the store as the only copy of the data. Keep sources and settings so that you can rebuild.
Choosing on fashion. The best store is one that meets your needs and that your team can run.
Key takeaways
A vector store holds vectors, text and metadata, builds indexes, answers similarity queries with filters and keeps data up to date. It does not create embeddings.
A flat index is exact and simple, and is often enough up to a few hundred thousand vectors. A clustered index searches only the nearest clusters. A graph index follows links towards the answer and often gives the best balance of speed and recall.
Approximate indexes have dials, such as nprobe and efSearch, that trade recall for speed. Always measure recall against a flat reference.
A clustered index with 1,000,000 vectors and 1,000 clusters, examining 10 clusters, does about 11,000 comparisons, roughly 91 times fewer than flat search.
Memory is vectors (dimensions times bytes) plus index links, text and headroom. Compression shrinks vectors at a cost in accuracy that must be measured.
Plan for updates, deletes and model changes, keep the source data so that you can rebuild, choose the simplest store that meets your needs and test it on your own data.
Check yourself
1. What are the four jobs of a vector store?
It stores records, builds indexes, answers queries with filters and keeps the data up to date.
2. A clustered index has 4,000,000 vectors in 2,000 clusters and examines 20 clusters per query. About how many comparisons does one query need?
2,000 centroids plus 20 × 2,000 = 40,000 vectors, about 42,000 comparisons.
3. Why is recall the important measure for an approximate index?
It says what share of the true nearest neighbours the index finds, so it shows how much accuracy was given up for speed.
4. 20,000,000 vectors of 512 dimensions are stored at 2 bytes a number. How much memory do the vectors need?
20,000,000 × 512 × 2 = 20.48 billion bytes, about 20.5 gigabytes.
5. Why should you store the embedding model's name with each vector?
A different model makes a different space. If you change models you must know which vectors to re-embed and avoid mixing the two.
6. When is a flat index a good choice?
When the collection is up to a few hundred thousand vectors, or when exact results matter, because it is simple, exact and needs no extra memory or build time.
Try this
Set aside 30 to 45 minutes. You need a spreadsheet. The last step is optional and needs access to a store or library you are allowed to use.
1. Choose a collection you might build. Estimate the vectors you will have now and in a year, and decide the dimensions and bytes per number.
2. Use the memory simulator to size it with and without 8-bit compression. Add the text and metadata you would keep. Write down the totals and the machine size they suggest.
3. Use the clustered-index simulator with a cluster count near the square root of your vector count and three values of nprobe. Write the comparisons and speed-up for each.
4. Decide which index family you would start with, and write two sentences of reasoning that use your numbers. State at what size you would revisit the decision.
5. List the filters your product needs, and say whether post-filtering would give too few results. Decide whether you need partitions or pre-filtering.
6. Write a plan for the day you change the embedding model: what you will store, how you will build the new index, how you will test it and how you will switch over.
7. Optional: build a flat and an approximate index on a small sample, find the true top 10 for 50 queries with the flat index and measure the recall and time of the approximate one at two settings.
A good result is a memory estimate with its assumptions, a table of comparisons and speed-ups for three settings, an index decision with a revisit point, a note on filters and a model-change plan.
Tags: AI Engineering Vector database Indexes HNSW Retrieval