Choosing a Vector Index: Flat, HNSW or IVF-PQ, a Memory Estimate, and Filters That Starve Small Customers
Millions of vectors, a filter per customer and a tight latency budget: which index? Estimate memory (15,360 MB flat, 16,640 MB HNSW, about 240 MB IVF-PQ for 10 M x 384 dims, arithmetic), turn the IVF dial (recall 0.55, 0.892, 0.994, simulated), and see why filtering after the search gives the smallest customer a recall of 0.052.
What you will learn
- Name the three things every vector index trades: recall, latency and memory
- Compare flat, HNSW and IVF-PQ, and estimate their memory out loud
- Turn the IVF dial and read what the last bit of recall costs
- Explain why filtering after the search starves a small tenant, and what to do instead
Before you start
- Similarity Search: Finding the Closest Vectors
- Vector Databases Hands-On: Chroma and pgvector
The interview version: you've got millions of vectors, a filter for each customer, and a tight latency budget. What index do you pick? Most people say "HNSW, because it's fast". A strong answer starts from the constraint, not from a favourite name.
A vector index is how a search finds the closest vectors without checking all of them. Numbers below come either from a small simulation with synthetic vectors, or from plain arithmetic, and each says which. If brute-force search is new to you, start with Similarity Search.
Three things to trade
- Recall. Of the true ten closest, how many did we actually get back?
- Latency. How long does one search take?
- Memory. How much has to sit in the machine to make it fast?
Push one up, and usually another pays. On top of that sits one more question: when do you apply the customer's filter?
Knocking on every door
You're looking for a house in a big city. You could knock on every door. You'd definitely find it, and you'd be at it all year. So you use a map: go to the right neighbourhood first, and only knock there. Much faster, but if the house is just over the border, you miss it. So you check a couple of neighbourhoods and buy back some certainty with more walking. An index skips most doors on purpose, so it can miss, and you decide how much missing is okay.
Flat, HNSW and IVF-PQ
| Index | How it works | Rule of thumb |
|---|---|---|
| Flat | Compares the query with every vector; recall is 1 | Exact, and expensive as data grows |
| HNSW | A graph, like roads between neighbours; the search keeps hopping to the closer neighbour | Spends memory to save time |
| IVF-PQ | IVF groups vectors into lists and searches only the nearest few; PQ shrinks each vector to a short code | Saves memory, gives up some recall |
HNSW in practice is shown in Vector Databases Hands-On.
The memory estimate
An interviewer usually wants a memory estimate, and you can do it out loud. This is arithmetic for illustration, not measured: 10 million vectors x 384 dimensions x 4 bytes per number.
So you don't pick by name:
- A small partition, or you need exact results: flat.
- Memory to spare and tight latency: HNSW is usually first in line.
- Memory is the wall: IVF-PQ, and rerank the top few to win back recall.
The dial
These indexes come with a dial. A small simulation with 20,000 synthetic vectors and 64 lists:
corpus: 20000 x 32 (synthetic)
flat recall@10=1.000 scanned= 20000
ivf nprobe=1 recall@10=0.550 scanned= 371
ivf nprobe=4 recall@10=0.892 scanned= 1303
ivf nprobe=16 recall@10=0.994 scanned= 5075
Search one list, and recall is 0.55. Four lists, 0.892. Sixteen lists, 0.994, scanning about 5,000 vectors: a quarter of everything. The last bit of recall costs the most work. Set the dial from your recall target, then check latency at p95. No HNSW or PQ was built in this simulation, so there are no measured numbers for those.
Ask for ten, get almost nothing
Now the failure. There are eight synthetic customers, and the smallest one holds about half a percent of the vectors. Search everything, take the top ten, then filter down to that customer:
Recall at 10 is 0.052. We asked for ten and got about half of one result. The obvious fix is to fetch more: fetch 100, and recall only climbs to 0.14. For the biggest customer, the same two settings give 0.522 and 1.0. So a test on your biggest customer passes while your smallest one is starved.
Filter first, so the search only looks at that customer's vectors, and exact search gives a recall of 1 for both. But that exact search still scored every vector: we fixed the result, not the work. With IVF and four lists searched after a filter, the biggest customer gets 0.888 and the smallest gets 0.446, only 7 results out of 10, because those four lists don't hold enough of its vectors.
A graph has its own version of this: if most neighbours belong to other customers, the hops run out of matches. That's reasoning; no graph was built here. So small customers often get their own partition, and you report recall per customer, especially the smallest, before trusting an average.
The follow-ups
- Why not filter after, with a bigger fetch? Fetching 100 gave the smallest customer only 0.14, and the fetch keeps growing as the customer shrinks.
- HNSW or IVF-PQ? It depends whether memory is the wall. If it is, IVF-PQ, and rerank to win back recall.
- How do filters interact with the graph? The hops can land mostly on other customers, so use a filter-aware search or a separate partition.
| Level | What it sounds like |
|---|---|
| Weak | "HNSW, because it's fast." |
| Okay | Compares the indexes with a memory estimate, and states its assumptions. |
| Strong | Starts from the constraint, handles filters and small customers, and keeps measured numbers apart from estimates. |
Recap
| Idea | Remember |
|---|---|
| Trade-off | Recall, latency, memory; an index skips doors on purpose |
| Memory | vectors x dims x 4 bytes; let the tightest constraint choose |
| The dial | 0.55, 0.892, 0.994 recall for 1, 4, 16 lists (simulated) |
| Filters | Filter after starves the smallest customer (0.052); filter first, measure per customer |
Try it yourself
Take a search system you know. Estimate its vectors and dimensions, work out flat memory, and name the one constraint that decides its index.
Related course on Udemy
Machine Learning Engineer: From First Model to ML Engineer
Learn machine learning in Python, then ship it: data analysis, regression, classification, tuning, deployment and MLOps.View on Udemy ↗: Machine Learning Engineer: From First Model to ML Engineer (paid course, opens in a new tab)Related posts
The RAG Pipeline Explained: 7 Stages From Documents to a Cited Answer
Ingest, chunk, embed, store, retrieve, rerank and generate, in one Python file. Follow one question through a 26-chunk employee handbook to an answer that cites its source.
Similarity Search: Finding the Closest Vectors with NumPy and NearestNeighbors
Ask one question of a whole collection of vectors: which items are closest to this one? Normalise, score every row with one matrix multiply, keep the top k or everything above a threshold, and check it with NearestNeighbors.
Why RAG Exists: The Problem It Solves, Compared With Pasting Everything and Fine-Tuning
A smart model has never read your company's files, and they change every month. Three ways to give it the facts: paste everything, fine-tune, or retrieve only the right pieces. Follow a refund-policy question through all three and see why RAG wins when documents change.
Real Embedding Models: an Open Model or a Provider API, Then Search, Duplicates and Recommendations
Swap a toy embedder for a model that learned. Two roads (all-MiniLM-L6-v2 on your machine, or OpenAI's API with your key), three sizes (384, 1,536 and 3,072 numbers), one rule, and three use cases built on the same rank function, with real scores.