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.
12 min readAdvancedCommon · estimateFacts checked 26 November 2026
Say it out loud
Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.
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.
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.
Key words
- 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.
From files to a searchable index
10 million files
contracts, letters, call notes
Pipeline, run per file
Split into chunks
a few hundred tokens each
Turn into numbers
one vector per chunk
Store with labels
owner, date, who may read
Ready to search
updated by a sync job
- 1Each of the 10 million files is read once.
- 2Each file is cut into chunks, and each chunk becomes a list of numbers.
- 3Every chunk is saved with its labels, so the search can filter on them later.
- 4The 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.
One question, two searches, one filter
Question and login
login id comes from the server
Two searches, same filter
Keyword search
exact codes, names, ids
Meaning search
both see only allowed chunks
Merge and rerank
wide set re-sorted to top 5
Few chunks to model
with source names attached
- 1The question goes to the keyword search.
- 2The same question goes to the meaning search.
- 3Keyword matches go on to the merge step.
- 4Meaning matches go on to the same merge step.
- 5Only 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.
# 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
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."
BetterTen million files also need splitting, labels, a sync job and a filter, not only a database.
Trap"A bigger model will fix the misses."
BetterCheck 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."
BetterBy 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?
Quick check
Your search finds meaning well but misses exact product codes. What is the cleanest fix?
Can you answer these from memory?
Flash cards · tap a card to flip it
What will they ask next?
- Q146How do you pick top-k?11 min
- Q147When must you re-embed everything?Coming soon
- Q148What are vector and embedding security weaknesses?13 min
- Q149How do you clean and deduplicate documents first?11 min
- Q151"The answer was in the docs, but the bot missed it." How do you debug?12 min
- Q142What is parent-child (small-to-big) retrieval?Coming soon
Learn it in depth
Last updated on 26 November 2026
Building search over a large document store?
Venkat designs and builds AI products and agents, including the search, the permissions and the tests that show the answers are right. Book a short call and tell him how many documents you have and who may read them.