Design a Document Processing Pipeline for LLMs and RAG
System design for ingesting millions of documents for LLMs and RAG: parsing PDFs and scans, chunking, embedding at scale, incremental updates and deletes.
The question: design a pipeline that ingests and indexes large volumes of heterogeneous documents (PDFs, scans, Word files, slides, spreadsheets, HTML, emails) so they can be used by LLM applications such as RAG chatbots and extraction workflows. It must handle tens of millions of documents, keep up with updates and deletes, parse messy formats well, and do it at a reasonable cost. Most RAG quality problems start here, which is why interviewers ask it as a question on its own.
1. Clarify requirements
| Question | Answer |
|---|---|
| Volume? | Backfill of 50M documents; then ~500K new or changed documents per day |
| Formats? | PDF (born-digital and scanned), DOCX, PPTX, XLSX, HTML, email with attachments |
| Outputs? | Clean text with structure, chunks with metadata and ACLs, embeddings, keyword index; extracted tables |
| Freshness? | Changes indexed within 15 minutes; deletes within 5 minutes |
| Multi-tenant? | Yes: many customers, strict isolation |
2. Architecture
3. Parsing heterogeneous documents
| Input | Approach | Pitfalls |
|---|---|---|
| Born-digital PDF | Layout-aware text extraction (reading order, columns, headings) | Headers/footers repeated per page; two-column interleaving |
| Scanned PDF / images | OCR, or a vision-language model for complex layouts | Slow and costly; handwriting; low-quality scans |
| Tables | Table structure detection; output as rows with headers or markdown | Merged cells; tables spanning pages |
| Office files | Native parsers for DOCX/PPTX/XLSX | Speaker notes, hidden sheets, embedded objects |
| Body plus attachments as child documents; thread structure | Quoted replies duplicate content | |
| Figures and charts | Vision model captions describing the content | Cost; hallucinated details |
- Route by type and difficulty: cheap fast parsers first; escalate only hard pages (scans, complex tables) to OCR or vision models.
- Keep page numbers and bounding boxes so answers can cite the exact page.
- Store parsed output as structured intermediate files, so re-chunking doesn't require re-parsing.
4. Chunking, metadata and embedding
- Structural chunking by headings with the section path prepended; parent-child chunks for long sections. See RAG chunking strategies.
- Metadata per chunk: tenant, document id and version, source, ACL, page, section, language, timestamps, embedding model version.
- PII detection tags or redacts sensitive fields per tenant policy.
- Embedding on a GPU pool with large batches; content hashes skip unchanged chunks.
5. Throughput and cost
Backfill : 50M docs × ~10 pages = 500M pages
Parsing : fast path ~20 pages/s per CPU core → 25M core-seconds ≈ 290 core-days
e.g. 1,000 cores → ~7 hours (plus OCR for the ~10% scanned pages,
routed to a GPU/OCR pool with its own budget)
Chunks : ~4 chunks/page → 2B chunks
Embedding : 2B chunks at ~3,000 chunks/s per GPU → ~185 GPU-hours
e.g. 40 GPUs → ~4.6 hours
Vectors : 2B × 768 dims × 1 B (int8) ≈ 1.5 TB → sharded index per tenant group
Daily delta : 500K docs → ~1% of backfill work, runs continuously
(illustrative throughputs)Backfills run at lower priority than live updates, in separate queues, so a new customer's 10-million-document import doesn't delay everyone's fresh edits.
6. Updates, deletes and consistency
- A document registry tracks every document's current version, status per stage, and checksums; it's the source of truth for reconciliation.
- Out-of-order events (an old update processed after a newer one) are rejected by version comparison.
- Deletes and permission revocations take a fast path: mark the document as hidden in the index immediately (a filter), then purge chunks and vectors asynchronously.
- Periodic reconciliation compares the registry with each source and with the indexes to catch missed events.
7. Reliability
- Each stage is idempotent (keyed by document id and version), so retries are safe; see idempotency.
- Poison documents (parser crashes, huge files) go to a dead-letter queue after a few attempts, with alerts.
- Per-tenant quotas and fair scheduling prevent one tenant from starving others; see multi-tenancy.
- Sandboxed parsers: documents are untrusted input (malformed files, macros, zip bombs).
8. Quality monitoring
- Parse quality signals: empty or garbled text ratio, OCR confidence, table detection rate per source and format.
- Freshness: lag from source change to searchable, per connector; delete propagation time.
- Downstream retrieval recall on a golden question set after any parser or chunker change; see LLM evaluation.
Where it’s used
Feeds permission-aware indexes for internal assistants; see RAG chatbot.
Contracts, invoices and claims parsed into structured fields with page-level citations.
Large archives made searchable with strict deletion and audit guarantees.
In an interview
- Split into queue-connected stages that scale and retry independently.
- Route documents to cheap parsers first and escalate hard pages to OCR or vision models.
- Size backfill parsing and embedding, and isolate backfills from live updates.
- Handle versions, deletes and revocations with a registry, fast hide and reconciliation.
Frequently asked questions
How do you ingest millions of documents for RAG?
Use a staged, queue-connected pipeline: detect type, parse with cheap parsers first and OCR or vision models only for hard pages, enrich, chunk with metadata and access lists, embed in large GPU batches, and index, with each stage idempotent and independently scalable.
How do you handle document updates and deletes?
Track versions in a document registry, index the new version fully before removing the old one, reject out-of-order events by version, hide deleted or revoked documents immediately and purge them asynchronously, and reconcile periodically.
Further reading
- Docling Technical ReportIBM Research, 2024
- Retrieval-Augmented Generation for Knowledge-Intensive NLP TasksLewis et al., NeurIPS 2020