Vector databases

  • Beginner
  • 35 min
  • Lesson 2 of 2 in Embeddings and vector search
  • Last verified

Open slides(32 slides)

Before you start

You will be able to

  • Explain why exact nearest-neighbor search does not scale and what an approximate (ANN) index gives up in return.
  • Compare HNSW and IVF on how they search, their memory cost, and whether they need training.
  • Name the main recall-versus-latency knobs for HNSW and IVF, and say how to measure recall against brute-force search on a sample.
  • Choose between a Postgres extension, a vector library and a dedicated vector database for a given scale and team.
  • Explain why metadata filters and document updates need explicit design in a vector store.

The help centre has forty thousand articles, and the chat needs the three closest to a customer’s question in under a second. Kavya taps her desk. “One second,” she says, “and no more.”

You could compare the question with every article. You could group the articles into regions and look only in the nearest ones. You could keep a shortcut list that is usually right.

Which would you try first?

Concept

A vector databaseVector databaseA store built to hold embeddings and find the nearest ones quickly, usually with metadata filters. Start with what you already run and move when a measured limit forces it.Open the glossary entry stores embedding vectors and answers one question quickly: “which stored vectors are nearest to this query vector?” To do that at scale it uses an approximate nearest-neighbor (ANN)Approximate nearest neighbor (ANN) searchFinding very close, though not always the closest, vectors with an index. You trade some recall for lower latency and memory.Open the glossary entry index. ANN returns almost the true nearest neighbors, much faster than checking all of them.

The index internals and tuning below go deeper than the rest of this module. If you only need to choose a store, read the mental model, the exact-search section and the store comparison first.

Why It Matters

Every RAG system needs retrieval that is both fast and relevant at scale. Interviewers probe the “approximate” part, not whether you know a vector database “stores vectors”. You are expected to say what is traded away, which knob buys it back, and how you would check.

Mental Model

Picture a library of millions of books. Exact search walks every shelf with the query in hand. An ANN index is a floor plan that lets you walk straight to the right area and check only the shelves around it. You may miss the single best book, but you finish in a fraction of the time. You pay for the map in memory and build time.

Why exact search stops working

Exact (brute-force) search compares the query with every stored vector. The work grows with the number of vectors times the number of dimensions. For an illustrative 10 million vectors of 1,536 dimensions, one query needs about 10,000,000×1,536=15.410{,}000{,}000 \times 1{,}536 = 15.4 billion multiply-adds. That is feasible offline and too much for a request with a latency budget of tens of milliseconds, especially at many queries per second.

Brute force does have a place. It is exact and needs no index. As a rough guide (measure on your own hardware), a few thousand to tens of thousands of vectors is often fast enough. It is also your ground truth for measuring ANN recall, below.

Diagram: Brute force compares the query with every stored vector and is exact but slow.

Query vector

Brute force:
compare with all 10,000,000 vectors

ANN index:
jump to the right neighbourhood

Exact top-k
slow, no index to build

Almost the same top-k
fast, costs memory and build time

Diagram summary: Brute force compares the query with every stored vector and is exact but slow. An ANN index goes straight to the right neighbourhood, so it is fast but may miss a true neighbour, and it costs memory and build time.

Recall: what “approximate” costs

Recall@kRecall@kThe share of the relevant or true top-k items that appear in the top k results. For an ANN index it measures the recall lost to approximation.Open the glossary entry is the share of the true top-k neighbors that the ANN search actually returned. If the exact top 10 contains 10 vectors and ANN returns 9 of them, recall@10 is 0.9. Every ANN index has settings that trade recall against latency and memory. Higher recall costs more time or more memory. You choose a point on that curve, you do not get both.

An illustrative sweep of one knob, to show the shape of the trade (these are not measurements of any real index):

ef_searchRecall@10Time per query
100.821 ms
400.953 ms
1000.987 ms
4000.99526 ms

Each step up buys less recall for more time. Past the point where your recall target is met, extra effort is wasted latency.

Two index families

HNSW: a layered graph

HNSWHNSWHierarchical Navigable Small World, a layered graph index. Search starts in a sparse top layer and descends to a dense bottom layer. High recall, memory-heavy.Open the glossary entry (Hierarchical Navigable Small World, Malkov and Yashunin) links each vector to nearby vectors in a graph, and stacks several layers of it. The top layer is sparse with long links. The lower layers are denser. A search enters at the top, hops greedily toward the query, drops a layer, and repeats until the bottom layer. Think of a motorway, then a main road, then a street map.

  • Strengths: high recall at low latency, and new vectors can be inserted one at a time.
  • Costs: the graph is large and is usually held in memory next to the vectors. Building is slower than for simpler indexes.
  • Knobs: M is how many links each node keeps. ef_construction is how wide the search is while building (it affects build time). ef_search is how many candidates a query tracks. Higher values raise recall at the cost of memory, build time or latency. M and ef_construction are fixed at build time, so changing them means rebuilding. ef_search can be changed per query. In pgvector the defaults are m = 16, ef_construction = 64 and hnsw.ef_search = 40 (checked 2026-10-09).
Diagram: An HNSW search enters at the sparse top layer, hops toward the query, then drops to a denser layer and repeats.

hop greedily toward the query

drop a layer, repeat

Query vector

Top layer: few nodes, long hops
(the motorway)

Middle layer: more nodes
(main roads)

Bottom layer: all vectors, short hops
(street map)

Candidate top-k

Diagram summary: An HNSW search enters at the sparse top layer, hops toward the query, then drops to a denser layer and repeats. At the bottom layer it has reached the neighbourhood of the query and returns its best candidates.

IVF: clusters and probes

IVFIVFInverted file index: vectors are grouped into clusters around learned centres, and a query scans only the nearest clusters. Lighter on memory than HNSW, and needs training on representative data.Open the glossary entry (inverted file) splits the vectors into clusters, each with a center point. A query is compared with the centers first, and only vectors in the closest few clusters are scanned. Think of postal districts: you search the districts nearest the address, not the whole country.

  • Strengths: less memory and faster builds than HNSW. The trade-off, per the pgvector README, is that at the same recall its queries are usually slower (checked 2026-10-09).
  • Costs: it needs a training step to find the cluster centers from a sample of your data, so the table should hold representative data before you build the index. If the data later changes a lot, the clusters may fit it less well. A true neighbor sitting in an unprobed cluster is missed.
  • Knobs: nlist or lists is how many clusters. nprobe or probes is how many clusters each query scans. Search time grows roughly in step with probes, and probing all clusters equals brute force. FAISS documents this and defaults nprobe to 1. pgvector defaults ivfflat.probes to 1 and suggests starting lists at rows/1,000\text{rows} / 1{,}000 for up to 1 million rows, and probes near the square root of lists (both checked 2026-10-09). Treat those as starting points to measure, not answers. Raising nlist while nprobe stays fixed means each query scans a smaller share of the data. That is faster but usually lowers recall, and it needs a rebuild.
Diagram: An IVF query is compared with the cluster centres first, then only the nearest few clusters are scanned.

true neighbour in an unprobed cluster

Query vector

Compare with all cluster centres
(cheap: only nlist of them)

Pick the nprobe nearest clusters

Scan only the vectors inside those clusters

Candidate top-k

Missed

Diagram summary: An IVF query is compared with the cluster centres first, then only the nearest few clusters are scanned. A true neighbour that sits in a cluster that was not probed is missed, which is where IVF loses recall.

Quantization shrinks vectors

Product quantization (PQ)Product quantization (PQ)A vector compression method that splits each vector into sub-vectors and stores, for each, the id of its nearest entry in a small learned codebook. It saves memory and speeds up distance checks, at some loss of recall.Open the glossary entry and other compression schemes store a compressed approximation of each vector, so more fit in memory and distance checks are faster. The price is lower recall. A common pattern is to search the compressed vectors, then re-rank a short list with the original vectors.

The memory saving is large. For 1 million vectors of 1,536 dimensions:

StoragePer vectorTotal for 1 million vectors
32-bit floats6,144 bytesabout 6.1 GB
8-bit integers (scalar quantization)1,536 bytesabout 1.5 GB
Product quantization at 64 bytes (an illustrative setting)64 bytesabout 64 MB
Diagram: Compressed vectors are searched first because they are small and quick.

Query

Search the compressed vectors
(fast, small, approximate)

Short list, e.g. top 100

Re-rank with the original
full-precision vectors

Final top-k

Diagram summary: Compressed vectors are searched first because they are small and quick. A short list is then re-scored against the original full-precision vectors, which recovers most of the recall that compression cost.

How to measure recall

Never ship an ANN index without measuring it. A simple method:

  1. Sample a few hundred real queries, ideally from your logs or a realistic test set.
  2. For each, get the exact top-k with brute force. This is the ground truth.
  3. Get the ANN top-k with your index and settings.
  4. Compute recall@k as the overlap, and record latency.
  5. Change one knob at a time (ef_search, nprobe) and re-measure. Pick the cheapest setting that meets your recall target.

pgvector’s documentation describes the same check: compare approximate results with exact search, which you can force by disabling index scans in a transaction (checked 2026-10-09). Also check whether retrieval recall is what really limits your product. Sometimes the embeddings or the chunking matter more than the last point of ANN recall.

Choosing a store

Pick by your scale, your team and your other requirements, not by popularity.

OptionWhat it isGood whenWatch out for
Postgres extension (pgvector)Vector columns and ANN indexes inside the database you already runTens of thousands to a few million vectors, and you want one system with transactions and joinsIndex memory and build time on one node, tuning, and the index dimension limit
Library (FAISS)An index you embed in your own service, not a databaseOffline jobs, research, or a custom serving layerYou build persistence, updates, filtering and replication yourself
Dedicated vector database (for example Qdrant, Weaviate, Pinecone)A service built around vector indexes, filtering and scalingLarge corpora, high query volume, or teams that want built-in sharding and filteringAnother system to run, pay for and keep in sync

On hosting: do not equate “dedicated” with “managed only”. Qdrant is open source under the Apache 2.0 license, can be run yourself with Docker, and also has a managed cloud. Weaviate describes itself as open source, with Docker, Kubernetes and embedded options, plus a managed Weaviate Cloud. Pinecone’s documentation describes a managed database and mentions a bring-your-own-cloud option, where it runs in your cloud account. Check its current docs for deployment options (all checked 2026-10-09). Products and plans change, so confirm before you decide.

One concrete limit to know: pgvector’s vector type can be indexed up to 2,000 dimensions, and halfvec up to 4,000 (checked 2026-10-09). A 3,072-dimension embedding therefore needs half-precision storage or a shorter vector. Details like this can decide the choice, so read the current docs.

A rule of thumb (judgment, not a measured law): start with what you already run (often pgvector), measure, and move when a measured limit (latency, memory, ops burden, filtering) forces you.

Diagram: Start from what you already run.

yes

no

yes

no

yes

no

Already running Postgres and
under a few million vectors?

pgvector: measure first

Offline job or research,
no live service?

FAISS library

Dedicated vector database

Measured limit hit?
latency, memory, filtering, ops

Stay where you are

Diagram summary: Start from what you already run. Postgres with pgvector is the default for modest scale, FAISS fits offline or embedded use, and a dedicated database is for scale or needs you have measured, not guessed.

Metadata filters and freshness

Real queries are rarely pure similarity. They say “similar to this, and department = legal, and the user may read it”. Two things to design, not discover in production:

  • Filtering. With an approximate index, a filter may be applied after the index returns its candidates. pgvector’s documentation gives the example of a condition matching 10% of rows with ef_search = 40: only about 4 rows survive on average, so you can get fewer than kk results. Remedies include iterative index scans (pgvector 0.8.0 and later), a larger candidate list, partitioning by the filter field, or an engine that filters during the graph search. Test filtered queries for recall too. Permission filters are a security boundary, so a missed filter is a data leak, not a ranking quirk.
  • Freshness. Documents change. Update the stored vector when the text changes, and delete the old chunks. Use deterministic chunk IDs (for example document ID plus chunk number) so an update overwrites its old chunk instead of leaving a stale copy. Some indexes need periodic rebuilds or compaction after many deletes.
Diagram: On the left, a filter applied after the index search throws away most candidates and can leave fewer than k results.

Keeping the index fresh

Document edited

Re-chunk and re-embed

Same chunk ID overwrites the old vector

Delete chunks that no longer exist

Post-filtering (illustrative numbers)

ANN returns 40 candidates

Filter keeps the 10% that match

About 4 results, fewer than k = 10

Diagram summary: On the left, a filter applied after the index search throws away most candidates and can leave fewer than k results. On the right, an edited document is re-chunked and re-embedded, deterministic chunk IDs make the new vectors replace the old ones, and chunks that disappeared are deleted so no stale copy is served.

Real-World Application

A support-search team indexes 50,000 help articles. At that size, pgvector with an HNSW index, one WHERE filter for product and language, and a nightly recall check against brute force on 300 logged queries is a defensible design. A product catalogue of hundreds of millions of items, with per-second updates and several teams querying it, is where a dedicated, sharded system earns its cost. In both cases the discipline is the same: measure recall and latency, then change one knob.

Interview Perspective

Expect: “how does HNSW work at a high level”, “when would you not need a dedicated vector database”, “how do you keep an index fresh” and “how do you know your ANN index is good enough”. Common mistakes are treating ANN output as exact, never measuring recall, and ignoring filtering.

Provenance: Common industry pattern. These are standard topics for GenAI roles, not a verified list for any named company.

Practice Questions

These are generated practice questions, not reported from any company.

  • Basic: Why can you not just brute-force compare against every vector at scale?
  • Intermediate: Which knob raises recall in HNSW, which in IVF, and what does each cost?
  • Advanced: Your recall@10 is 0.95 on average but only 0.6 on queries with a tight metadata filter. What could explain the gap, and what would you test?
  • Product-Company: A document was just updated, but search still returns the stale version. What is happening and how do you fix it?
  • Product-Company: You must store 2 million vectors that need metadata filters, for a team that already runs Postgres. Choose a store and an index type, justify the choice with the recall-versus-latency trade-off, and say how you would test that filtered queries still return enough good results.

Activity

Decide in writing between pgvector and a dedicated vector database for two cases: a 50,000-document internal wiki, and a 500-million-item product catalogue. For each, state the numbers you would measure first (vector count, dimensions, queries per second, update rate, filter selectivity) and the result that would make you change your mind.

Homework

Using any library with an ANN index (FAISS, or pgvector if you have Postgres), build an index over a few thousand embeddings. Compute exact top-10 with brute force for 50 queries. Then vary ef_search or nprobe over at least four values, and record recall@10 and time per query. Write three sentences on where you would set the knob, and why.

Quick Revision

  • Exact search is accurate but costs vectors x dimensions per query; ANN trades a little recall for much lower latency.
  • HNSW: layered graph, high recall, memory-heavy, inserts incrementally; knobs M, ef_construction, ef_search.
  • IVF: clusters probed per query, smaller memory, needs training; knobs nlist and nprobe.
  • Measure recall@k against brute force on real queries; change one knob at a time.
  • Start with what you run (often pgvector); move when a measured limit forces it. Open-source dedicated databases can be self-hosted.
  • Design filters and updates explicitly; post-filtering can return too few results.

Check yourself

  • Walk-through: Compare HNSW and IVF for a 50M-vector index with frequent updates. Which knobs trade recall for latency, and how do you verify recall?
  • Variation: Your HNSW index is 2x over the memory budget. Name two options (quantization, shorter vectors) and the likely recall cost of each, and say why switching to IVF alone is unlikely to be enough (IVF-Flat stores the same full vectors).
  • Common misconception: Treating ANN as exact, never measuring recall, or ignoring the cost of metadata filtering.

Resources & Further Reading

Checked sources to go deeper. The essential ones come first in each group.

Official documentation

  • Vector embeddings (opens in a new tab)

    OpenAI2026Documentation

    Explains the dimensions parameter, which is the lever you reach for when a 3,072-dimension embedding hits an index dimension limit.

Hands-on

More for this module: Embeddings and vector search resources

Practice

Test what you just read. Quizzes and cards are grouped by module: Embeddings and vector search.

Check yourself11 questions on what this lesson set out to teach

Checking answers needs JavaScript. You can still read every question and option below.

0 of 11 answered

Keyboard: arrow keys move between options. Space or Enter selects the focused option.

ObjectiveExplain why exact nearest-neighbor search does not scale and what an approximate (ANN) index gives up in return.

  1. Question 1 of 11A prototype does exact search over 20,000 vectors and answers in a few milliseconds. The corpus will grow to 200 million. What is the main reason to plan an ANN index?

    Choose one answer.

  2. Question 2 of 11An ANN index returns 9 of the 10 true nearest neighbors for a query. What is the recall@10, and what does it tell you?

    Choose one answer.

  3. Question 11 of 11A team's brute-force vector search is too slow at 10 million vectors, so they move to an approximate nearest-neighbour index and queries get much faster. What have they most likely given up?

    Choose one answer.

ObjectiveCompare HNSW and IVF on how they search, their memory cost, and whether they need training.

  1. Question 3 of 11Memory is tight and the corpus is mostly static, so you pick IVF over HNSW. When should you create the index?

    Choose one answer.

  2. Question 4 of 11Two engineers describe how an HNSW query avoids visiting every vector. Which description is accurate?

    Choose one answer.

ObjectiveName the main recall-versus-latency knobs for HNSW and IVF, and say how to measure recall against brute-force search on a sample.

  1. Question 5 of 11Latency is fine but recall@10 is 0.82 against brute force on your sample queries. Which change is the most direct way to raise recall on an IVF index?

    Choose one answer.

  2. Question 6 of 11How should you measure the recall of your ANN index before launch?

    Choose one answer.

ObjectiveChoose between a Postgres extension, a vector library and a dedicated vector database for a given scale and team.

  1. Question 7 of 11A team with 80,000 documents already runs Postgres for its product. A colleague wants to add a dedicated vector database "to be safe". What is the best advice?

    Choose one answer.

  2. Question 8 of 11Your security team asks whether you must use a managed cloud service to run Qdrant or Weaviate inside your own network. What is the accurate answer?

    Choose one answer.

ObjectiveExplain why metadata filters and document updates need explicit design in a vector store.

  1. Question 9 of 11You ask for the 10 nearest chunks where department = legal. The corpus holds thousands of legal chunks, but only 3 come back. What is the most likely cause?

    Choose one answer.

  2. Question 10 of 11A policy page was edited yesterday. Search still returns the old wording next to the new one. Which design most likely prevents this?

    Choose one answer.

Build with this