# Designing RAG for 10 Million Documents

> How to design retrieval over 10 million documents: chunking, approximate search, reranking, permissions and a fresh index, explained for your AI interview.

**In short:** Split every document into chunks, store them in an index built for fast search, and search with keywords and meaning together. Rerank the best candidates, and filter by who may read each chunk inside the search, not after it.

AI and LLM interview question Q150, Retrieval-augmented generation (RAG). Advanced, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/how-would-you-design-rag-for-10-million-documents

## Say it in 30 seconds

At ten million documents, I would build RAG as two pipelines. One splits, labels and embeds every file once, then keeps the index in sync with changes. The other takes a question, runs keyword and meaning search with the same permission filter, reranks the best candidates, and sends a few chunks to the model. The permission filter goes inside the search, never after it.

## Say it in 2 minutes

Let me start with the data. If each file is cut into about eight chunks, ten million documents become about 80 million chunks. I would run indexing as a pipeline. Each file is read once, split, labelled with its owner and who may read it, turned into vectors, and stored. A sync job then handles only the files that changed, and it removes the chunks of deleted files.

For a question, I run two searches at once: one for keywords and one for meaning. Exact search over 80 million vectors is too slow, so I use an approximate index and measure its recall, meaning how many of the true best matches it finds, against a sample of exact results. Both searches carry the same permission filter, taken from the server's login, so nothing a person may not read ever reaches the model.

Then I rerank. Each search returns a wide set of candidates. Anthropic, in its 2024 test, took the top 150 chunks, let a reranker sort them, and kept the top 20. It reported that its contextual method cut failed retrievals by 49%, and by 67% once reranking was added. I would test my own question set the same way.

Last, I would keep a set of real questions with the right chunks marked, run it on every change, and treat a drop in the share of questions that find their right chunk as a failed release.

## What is RAG at 10 million documents, in plain words?

RAG means the AI looks up the right passages first, then writes its answer from them. With 10 million documents, the model can't read everything for each question, so the system keeps a catalogue and fetches only the few passages a question needs.

Picture a county records office with 10 million files. Nobody photocopies the whole room for each enquiry. Clerks index every file once, keep that index current, and pull the handful of folders that match a request. The model is the reader at the desk, and the index is the catalogue.

At this size, the hard parts are the index, the speed of each search, keeping it fresh, and making sure each person sees only what they may read.

- **Chunk:** A piece of a document, usually a few hundred tokens (small pieces of text, often part of a word), so a search can return one passage.
- **Embedding:** A vector, which is a list of numbers that stands for the meaning of a text. Similar texts get similar lists.
- **Index:** The stored structure that finds the closest chunks fast, without reading every one.
- **Reranker:** A slower model that re-sorts the best candidates by how well each one answers the question.
- **Permission filter:** A rule that drops any chunk the person asking has no right to read.

## How do 10 million documents get in, and stay current?

Indexing runs as a pipeline. Each file is read once, cut into chunks, labelled, turned into numbers and stored. Microsoft's guidance for this kind of system lists the same stages, from chunking through to storing the chunks in the search index.

The sums matter. If each file gives about eight chunks, 10 million files make about 80 million chunks, and each chunk is embedded once. The first load is a large batch job that you run in parts, with a queue and retries, not a single request on a web page.

*Diagram, From files to a searchable index: Each file goes through four stages once. A sync job repeats them only for files that changed, and a question never goes back to the source files.*
1. Each of the 10 million files is read once.
2. Each file is cut into chunks, and each chunk becomes a list of numbers.
3. Every chunk is saved with its labels, so the search can filter on them later.
4. The index is ready, and a sync job keeps it current.

After the first load, a sync job compares each file with the last run. It embeds new and edited files and removes the chunks of deleted ones. Deletion is the step people forget, and a removed contract that stays in the index can still be found and quoted.

Updates cost money too. Google's managed vector search prices the building and the updating of indexes. So update cost is a design input you plan for from the start.

## How does one question find the right chunks?

Exact search compares the question with every stored vector. It is accurate, but at 80 million chunks it is too slow. Microsoft says exhaustive search is computationally intensive, so it suits small to medium datasets. At this size you use an approximate index instead.

The common approximate index is HNSW. Microsoft describes it as built for high recall and low latency. You trade a little accuracy for speed, and you measure that trade with recall at k: the share of the true best results that the fast search also returns. This is about approximate search only; the test set below measures whether the right chunks come back.

> "Because the database size can easily be in the millions or even billions, MIPS is often the computational bottleneck to inference speed, and exhaustive search is impractical." (Google Research, on ScaNN)
>
> "Combining Contextual Embeddings and Contextual BM25 reduced the top-20-chunk retrieval failure rate by 49% (5.7% → 2.9%)." (Anthropic, Contextual Retrieval (2024))

Meaning search alone often misses exact codes, claim numbers and product names. So a common design runs keyword search too and merges the two lists. Microsoft's own description of this pattern is that hybrid queries combine keyword and vector search for better recall.

*Diagram, One question, two searches, one filter: One question runs a keyword search and a meaning search, and both are limited to chunks the person may read. A reranker then keeps the best few for the model.*
1. The question goes to the keyword search.
2. The same question goes to the meaning search.
3. Keyword matches go on to the merge step.
4. Meaning matches go on to the same merge step.
5. Only the best few chunks reach the model, with their source names.

Then rerank. Take a wide set of candidates. Anthropic, in its test, pulled the top 150 chunks, let a reranker sort them, and kept the top 20. It measured the share of relevant documents that failed to come back in the top 20 chunks, and the drop was largest once reranking was added.

## How big does the index get?

Do the arithmetic before you pick hardware. The code below uses made-up numbers: 10 million documents, eight chunks each, and 1,024 numbers per vector. It prints the rough size of the raw vectors, the size if each number is shrunk to one byte, and how many chunks change each day.

```python
# Rough sizing for a made-up bank document store. Every number is an assumption.
DOCS = 10_000_000
CHUNKS_PER_DOC = 8
DIMS = 1024
DAILY_CHANGE = 0.02

chunks = DOCS * CHUNKS_PER_DOC
raw_gb = chunks * DIMS * 4 / 1e9
small_gb = chunks * DIMS * 1 / 1e9
changed = int(chunks * DAILY_CHANGE)

print(f"Chunks to index: {chunks:,}")
print(f"Raw vectors, 4 bytes each: {raw_gb:,.0f} GB")
print(f"Shrunk to 1 byte each: {small_gb:,.0f} GB")
print(f"Chunks to re-embed per day: {changed:,}")
```

Output:

```text
Chunks to index: 80,000,000
Raw vectors, 4 bytes each: 328 GB
Shrunk to 1 byte each: 82 GB
Chunks to re-embed per day: 1,600,000
```

About 328 GB of raw vectors is more than a typical laptop or small server holds in memory, so the index is split into parts that are searched together. Shrinking each number to one byte brings it to about 82 GB, but that can cost some accuracy, so test it on your own questions. The vector size is an example, not a figure for any product, and the sums are simple arithmetic on the assumptions.

> **Tip:** Say the sums out loud in the interview. Interviewers want to hear you size the problem before you name a tool.

## Who may see which chunk?

A search that returns a chunk a person may not read is a leak, even if the answer looks right. The safe rule: take the user's identity from the server's login, never from the question, turn it into a filter, and apply that filter inside the search itself.

> "In multi-tenant deployments, similarity search frequently runs across the full index before access control is applied at the application layer." (OWASP, 2026)

The order is the risk. OWASP says an attacker can probe a shared index and infer what other tenants hold from result counts and timing, without ever seeing the documents. The attack works even when every document is correctly tagged, because the access check runs after the search. So the filter goes before the ranking, not after it.

Microsoft's security filter pattern follows the same idea. It trims results with a filter on a security principal, inside the query, but the application must authenticate users, obtain trusted principal values, and apply the filter to every query.

Each chunk carries its permission label from the day it was indexed. When a permission changes, the chunks need new labels, so the sync job must handle permission changes as well as file edits.

### How would you design it for a bank's files?

A bank's branch staff ask a question, and the answer must come from policies, loan files and complaint letters. Some files are only for one branch. Here is the design in five steps.

1. **Chunk each type differently:** A loan file keeps its section headings, and a call note stays whole. One rule for every file type loses the structure that makes answers accurate.
2. **Label every chunk:** Store the owner, product, branch, date and who may read it. Labels are cheap at ingest and slow to add later.
3. **Index two ways:** Build a meaning index and a keyword index over the same chunks, and merge their results at query time.
4. **Filter inside both searches:** The identity from the login becomes the filter. With no identity, return nothing.
5. **Measure on real questions:** Keep about 300 real questions with the right chunks marked. Run them on every change, and block a release that lowers recall.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] Two pipelines: indexing once, and answering each question.
- [ ] Chunks, labels and an index sized with simple arithmetic.
- [ ] Approximate search, measured with recall, not exact search for everything.
- [ ] Hybrid search, then a reranker over a wider candidate set.
- [ ] Permission filter inside the search, identity from the server.
- [ ] A sync job for changes and deletes, and a test set that catches drops.

**Common traps**

- Trap: "Put everything in one big vector database." Better: Ten million files also need splitting, labels, a sync job and a filter, not only a database.
- Trap: "A bigger model will fix the misses." Better: Check what was retrieved before you change the model. A better model can't answer from a passage the search never found.
- Trap: "Filter the answer after the model writes it." Better: By then the model has already read the chunk. Filter the search itself.

## Can you spot the safe design?

**Spot the safe query:** Which search call is safe for a branch-only loan file?

A. search(query, top_k=20)  # the app hides rows afterwards
B. search(query, top_k=20, filter=allowed_for(login_user))  # identity from the server login
C. search(query, filter=f"branch eq '{request.args['branch']}'")  # branch from the URL
D. search(query, top_k=20)  # the model picks which results to show

**Answer:** B. Only the server's login, applied as a filter inside the search, limits what comes back. Hiding rows afterwards lets them be read first, a branch from the URL can be typed by anyone, and a model should never decide who sees what.

**Quick check:** Your search finds meaning well but misses exact product codes. What is the cleanest fix?

A. A bigger embedding model
B. Add a keyword search and merge it with the meaning search
C. A longer system prompt
D. A lower temperature

**Answer:** B. Keyword search finds exact codes and names, and hybrid search merges it with meaning search. Model size, prompts and temperature don't change what the search can find.

## Can you answer these from memory?

**Flash cards**

- **What does RAG do at 10 million documents?** Finds the few right chunks from a prebuilt index, then answers from them.
- **Why not search every vector?** Exact search is too slow at this size. Use an approximate index and measure recall.
- **What is hybrid search?** Keyword search and meaning search run together, and their results are merged.
- **What does a reranker do?** A slower model re-sorts a wide set of candidates, so the best few reach the model.
- **Where does the permission filter go?** Inside the search itself, with the identity taken from the server login.
- **What happens when a file is deleted?** Its chunks must leave the index too, or the search can still find them.

## Likely follow-up questions

- Q146 How do you pick top-k? https://designervenkat.online/interview/ai/how-do-you-pick-top-k
- Q147 When must you re-embed everything? (full answer coming)
- Q148 What are vector and embedding security weaknesses? https://designervenkat.online/interview/ai/what-are-vector-and-embedding-security-weaknesses
- Q149 How do you clean and deduplicate documents first? https://designervenkat.online/interview/ai/how-do-you-clean-and-deduplicate-documents-first
- Q151 "The answer was in the docs, but the bot missed it." How do you debug? https://designervenkat.online/interview/ai/debug-a-rag-bot-that-missed-the-answer
- Q142 What is parent-child (small-to-big) retrieval? (full answer coming)

## Learn it in depth

- [Indexing Embeddings](https://designervenkat.online/learn/ai-engineering/indexing-embeddings)
- [From question to context](https://designervenkat.online/learn/ai-engineering/retrieval-process)
