Drills D01

Production RAG over 10M documents at 1K QPS

Design a question-answering system over 10 million internal documents that serves 1,000 queries per second at peak, with cited answers. Walk me through the architecture, the numbers and how you know it works.

45 minutes · revise 15.09 · 15.10 · 16.03 · 16.09 · 17.04 · 17.11 · 31.03

The most reported LLM design prompt. The interviewer wants scoping before drawing, hybrid retrieval, reranking and an abstain path without prompting, and index and token sizing on the whiteboard. The trap is “vector DB plus GPT” with no numbers.

Clarify and scope

Spend five minutes asking, then write the assumptions where the interviewer can see them.

Out of scope for v1: multi-hop research, actions, chat memory beyond condensing the last turn.

Baseline architecture

Two pipelines share storage. The offline path turns documents into searchable chunks; the online path turns a question into a cited answer inside the latency budget.

OFFLINE (event driven)
 doc change ─► queue ─► worker: fetch+hash ─► parse/OCR ─► chunk (structure-aware)
                         ─► enrich (title/heading prefix) ─► embed (pinned model id)
                         ─► upsert by content hash ─► [chunk store | ANN index | BM25 index]

ONLINE (per query, p95 TTFT ~1.5 s)
 question ─► auth + rate limit ─► route (small talk / out of scope / RAG)
          ─► condense with last turn ─► embed query
          ─► ┌ dense top-50 (HNSW, ACL filter in query) ┐
             └ BM25 top-50   (ACL filter in query)     ┘ ─► RRF fuse
          ─► cross-encoder rerank 40 → 6 ─► abstain gate (score floor)
          ─► prompt: static rules (cached) + numbered passages + question
          ─► LLM, structured {claims:[{text, passage_ids}]} ─► stream
          ─► verify citations and numbers ─► answer + sources ─► trace

Each box earns its place. The router keeps greetings off the retrieval path. Condensing makes “and for contractors?” standalone, since the retriever cannot see history. BM25 catches policy numbers and product codes that vectors blur; RRF fuses by rank, sidestepping incompatible score scales. The reranker reads query and passage together, precise but only affordable on a few dozen candidates. The abstain gate skips the LLM call when nothing relevant came back, and verification drops claims citing passages never shown. The RAG pipelines chapter has this path as runnable Python.

Key choices and trade-offs

Chunking. Structure-aware chunks of roughly 300 to 500 tokens, split on headings and sections, tables kept whole, each chunk prefixed with document title and heading path. Index small, hand the model the parent section when a chunk is too thin. See chunking.

Embedding model. A multilingual model around 1,024 dimensions, chosen by recall on our labelled queries, not a leaderboard. Store the model id on every chunk: two models can emit same-width vectors, and mixing them fails silently. A model change is a full re-embed into a new index behind an alias.

Vector index. HNSW for low-latency high recall, with int8 or binary-quantized vectors in RAM and full-precision vectors on SSD to rescore the top 100. IVF-PQ if RAM is the constraint and lower recall is acceptable. Name requirements, not a vendor: filtered search, upserts and deletes, shards and replicas.

Filtering. ACL and document status filters must run inside the ANN search. Post-filtering a top-50 can leave nothing for a user who can see little, and the ANN never looks further. Use filtered search, or partition when tenants are few and large.

Hybrid is the default, but measure it. BM25 usually rescues identifier queries; on some corpora it hurts. Keep the fusion weight in config and let eval recall decide.

Generator. A mid-size model, schema-enforced output, temperature 0, with easy queries routed to a smaller model. No fine-tuning: RAG holds the facts.

Evals

Build the eval set before tuning anything.

Guardrails and fallbacks

Observability and cost

One trace per query, one span per stage. Log chunk ids and scores, prompt version, model id, tokens in, out and cached, and the verification result. Dashboards: per-stage p95, abstain rate, verification drop rate, tokens per query, cost per 1K queries, index lag, and error rate per provider.

Generation dominates the bill, not the index. Levers in order: fewer passages (n), prompt caching of the static prefix, routing easy questions to a small model, and an exact answer cache for popular questions (ACL in the cache key).

The numbers

Index size.

tokens        10M docs × 2,000            = 20B tokens
chunks        2,000 / (400 − 50 overlap)  ≈ 6 per doc → 60M chunks
vectors fp32  60M × 1,024 dims × 4 B      ≈ 246 GB
vectors int8  60M × 1,024 × 1 B           ≈ 61 GB
binary        60M × 1,024 bits / 8        ≈ 7.7 GB
HNSW links    M=16 → ~32 neighbours × 4 B at layer 0 + upper layers
              ≈ 140 B per vector × 60M    ≈ 8.4 GB
chunk text    60M × 400 tokens × ~4 B     ≈ 96 GB (disk / doc store)

With int8 in RAM plus graph, about 70 GB; split into 4 shards of about 18 GB. Every query fans out to every shard. If one replica sustains R QPS at target recall (measure it, do not guess), replicas per shard = 1,000 × headroom / R.

Ingestion. Daily churn is 1% of 10M = 100K docs ≈ 600K chunks ≈ 7 chunks per second averaged over a day: trivial. The initial build is 60M / T seconds per GPU, where T is measured embedding throughput. At an illustrative 1,000 chunks/s that is 60,000 s, about 17 hours on one GPU, so parallelise.

Reranker compute. 40 pairs × 500 tokens = 20K tokens per query. Forward FLOPs ≈ 2 × params × tokens. For a small 33M-parameter cross-encoder: 2 × 33M × 20K ≈ 1.3 TFLOP per query, so 1.3 PFLOP/s at 1K QPS. That is a handful of GPUs at realistic utilisation; a 0.6B reranker needs roughly 18× more. This is why the reranker size is a cost decision.

Tokens per request. 600 (system rules) + 6 × 475 (passages) + 150 (question and history) ≈ 3,600 input; about 250 output.

Cost per 1K requests (prices in dollars per million tokens, check current prices):

cost / 1K requests = 3.6 × P_in + 0.25 × P_out
with prompt caching of the 600-token prefix at fraction c of P_in:
                   = (3.0 + 0.6 × c) × P_in + 0.25 × P_out
with answer cache hit rate h: multiply everything by (1 − h)
daily volume at 400 avg QPS = 400 × 86,400 ≈ 34.6M requests

Throughput limits. At 1K QPS peak the generator sees 3.6M input tokens and 250K output tokens per second. On an API, check that your rate-limit tier covers about 216M input tokens per minute. Self-hosted, replicas = 250,000 / (measured output tokens per second per replica at your latency target).

Latency budget (p95 targets to measure against). Route and auth 10 ms, condense 0 to 200 ms (only for follow-ups, small model), query embed 20 to 50 ms, both search legs in parallel 30 to 60 ms, fetch chunk text 10 to 20 ms, rerank 50 to 100 ms, prefill of 3.6K tokens a few hundred ms. TTFT lands around 0.5 to 1 s, inside the 1.5 s target with room for a slow provider.

Follow-ups they will ask

How to bring in your own work

You have worked on this pipeline at small scale, partly as your own code and partly as a teammate’s. Keep the two apart, then say what changes at 10M.

esc

Narrow with repo:jm layer:eval q:naive mine · ↑↓ to move, enter to open