Interview's MateAI Engineering
Interview's Mate

AI Engineering

LLMs, RAG, agents & evals

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
AI Engineering topics
Lesson 24/ 160
02. RAG

Vector Databases

Specialized stores for ANN search with filters, hybrid retrieval, and scale considerations.

IntermediateANN Search · Filters3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidHands-onQuiz (3)Practice

In plain words

It's a database built to quickly find the stored items whose number-lists are most like your question's, instead of comparing against all of them.

Think of it like…

A library with a smart catalogue: instead of walking every shelf to find books similar to your request, the catalogue jumps you to the right neighbourhood and checks a few candidates. It might occasionally miss the very best match (approximate), but it answers in milliseconds.

Where you’ve seen it

A SaaS company serves 2,000 tenants with about 30 million chunks (1,024 dimensions). Every query must be restricted to one tenant and respect document ACLs. They compare options: pgvector would put 120 GB of vectors beside their relational data, a dedicated vector database offers native filtered search and sharding, and a managed service removes operations. They pick a dedicated store with per-tenant filtering, benchmark p95 latency under 80 ms at 95 percent recall, and keep the source of truth in PostgreSQL.

Words you’ll hear

ANN (approximate nearest neighbour)
Fast search that returns very close, not guaranteed exact, neighbours.
Recall
Share of the true nearest neighbours that the ANN index actually returns.
Filtered search
Restricting vector search by metadata such as tenant, date or access rights.
Hybrid search
Combining vector similarity with keyword (BM25) scoring.

Visual diagram

Loading interactive diagram

Theory & deep dive

What a vector database must do well

Search: build and maintain ANN indexes (HNSW graphs, IVF clusters, optionally with product quantisation) and expose a recall-versus-speed setting. Filtering: apply metadata conditions during the search, not after it. Post-filtering the top 10 results can leave you with two, or none, when the filter is selective; good systems integrate filters into the index traversal or pre-filter, and you should test recall under your real filters. Hybrid: combine dense similarity with BM25 and fuse the rankings, because vectors alone miss exact identifiers and rare terms.

Operations: upserts and deletes without full rebuilds, replication and backups, sharding for scale, multi-tenancy, access control and observability. Memory is the main cost driver: float32 vectors take dimensions × 4 bytes each, so 30 million 1,024-dimension vectors are about 123 GB before index overhead (HNSW adds roughly 20 to 50 percent). Quantisation (8-bit or binary) and lower dimensions cut that substantially at some recall cost.

Capacity check

30M vectors × 1,024 dims × 4 bytes ≈ 123 GB. With int8 scalar quantisation ≈ 31 GB, plus the graph. That is the difference between a multi-node cluster and a single large machine.

Key takeaway: ANN index + correct filtering + hybrid + operations; memory sets the price.

Choosing between options

Start from requirements: corpus size, dimension count, filter complexity, update rate, latency target, hybrid needs, team skills and compliance. For up to a few million vectors, with data already in PostgreSQL, pgvector is often enough and keeps one operational system with transactional consistency and SQL joins. Search platforms you already run (Elasticsearch/OpenSearch) add vector fields and give strong hybrid search. Dedicated databases (Qdrant, Weaviate, Milvus, Pinecone) shine at tens of millions to billions of vectors, heavy filtering, multi-tenancy and sharding, or when you want a managed service.

Benchmark on your own data with realistic filters: measure recall@k against exact search, p95 latency under concurrency, ingestion speed and memory. Keep the vector store disposable by retaining raw text and metadata in a system of record and storing embedding model version with each vector, so you can rebuild or migrate. Avoid premature scale: a simple store you understand beats a distributed one you don't.

A decision rule of thumb

Under ~5M vectors with SQL filters and joins: pgvector. Existing search cluster with strong keyword needs: OpenSearch/Elasticsearch. 50M+ vectors, strict latency, heavy multi-tenant filtering: dedicated vector database.

Key takeaway: Pick by requirements, benchmark with real filters, and keep a rebuildable source of truth.

How it works, step by step

Step 1 of 4
Planning

1. Size the problem

Estimate vector count, dimensions, update rate, latency target and filter patterns, then compute memory.

30M × 1,024 × 4 B ≈ 123 GB
Key numbers at this step
Memory
drives cost

When to use it & trade-offs

What it is

A vector database stores embeddings with their metadata and answers nearest-neighbour queries quickly using approximate nearest neighbour (ANN) indexes such as HNSW or IVF. Beyond raw similarity it adds what retrieval needs in practice: metadata filters (tenant, date, permissions), hybrid search (vectors plus keywords), updates and deletes, replication and scaling. Options include dedicated systems (Qdrant, Weaviate, Milvus, Pinecone) and add-ons to existing stores (pgvector for PostgreSQL, Elasticsearch/OpenSearch vector fields, MongoDB Atlas Vector Search).

Why it matters: Exact nearest-neighbour search compares the query to every vector, which is fine for 50,000 vectors and far too slow for 50 million. Choosing a store affects recall, latency, cost, filtering correctness and how much infrastructure you operate. The most common mistake is picking a trendy dedicated database for a corpus that PostgreSQL could handle, or the reverse.

When to reach for it

  • Semantic search and RAG at scale
  • Recommendations and duplicate detection
  • Multi-tenant retrieval with ACL filters

When to avoid or be careful

  • Choosing a distributed vector database for 200,000 vectors
  • Post-filtering a tiny top-k and returning nothing
  • Treating the vector store as the only copy of your data

Where to store vectors

pgvector in PostgreSQL
Best for: Up to a few million vectors; SQL joins and transactions

Strengths: One system; ACID; familiar tooling.

Watch out: Scaling and heavy filtered ANN need tuning; RAM-bound at large scale.

Search platform (Elasticsearch / OpenSearch)
Best for: Teams already running search; strong hybrid retrieval

Strengths: BM25 + vectors in one engine.

Watch out: Cluster operations; vector features vary by version.

Dedicated vector database
Best for: Tens of millions to billions of vectors, heavy filtering, multi-tenancy

Strengths: Built for ANN at scale; native filtering and sharding.

Watch out: Another system; consistency with the source data to manage.

Interview signals & numbers to know

When this topic comes up

  • “Which vector database should we use?”
  • “Search latency grows with data”
  • “Filtered queries return too few results”
  • Memory cost of embeddings
  • Multi-tenant RAG

Benchmarks & rules of thumb

dims × 4 bytes -
float32 memory per vector
+20 – 50% -
typical HNSW index overhead
95%+ -
common recall target for ANN

Common mistakes & follow-ups

Common interview mistakes

  • Benchmarking recall without filters, then finding it collapses when queries are restricted to one tenant.
  • Deploying a cluster before measuring whether one node with pgvector would do.
  • Having no raw text copy, so changing the embedding model means re-ingesting everything from source systems.

Likely interviewer follow-ups

  • How would you choose between pgvector and a dedicated vector database?
  • Why is filtered ANN search hard?
  • How would you estimate memory for 50 million vectors?
Interview tip: Answer with the decision criteria (scale, filters, hybrid, ops) and say you'd benchmark with real filters. It shows you've been burned by demo-only choices.
Loading hands-on exercise

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Vector Databases with generative AI interview Q&A and hands-on practice.

Open Generative AI Q&A →Tech Learn Q&A
Previous topic
Chunking Strategies
Next topic
pgvector