High-Level Architecture: The Ingestion-Extraction Pipeline
A common mistake when designing document processing systems is attempting to process uploaded documents synchronously within an HTTP request handler. High-resolution multi-page PDF rendering, OCR extraction, and multi-modal model inference can easily take between 15 and 45 seconds per document. A synchronous endpoint will trigger browser timeouts, overwhelm connection pools, and drop files whenever a minor network hiccup occurs.
An enterprise document pipeline must be asynchronous and event-driven. The client requests a presigned S3/R2 URL, uploads the document directly to object storage, and dispatches an ingestion job to a background worker queue (such as BullMQ or RabbitMQ). The web server responds immediately with an HTTP 202 Accepted and a tracking document ID.
The background worker drives the document through six discrete lifecycle stages: (1) Pre-processing & DPI normalization, (2) Layout detection & visual chunking, (3) Multimodal schema extraction via Structured Outputs, (4) Deterministic cross-validation, (5) Confidence scoring & anomaly routing, and (6) Database persistence and webhook notification.
- Decouple file upload from processing: Direct-to-storage uploads via presigned URLs prevent memory saturation on API gateway nodes.
- Event-driven worker orchestration: Execute extraction steps as discrete queue jobs with automated retries and dead-letter queue isolation.
- Real-time UI feedback: Broadcast pipeline progression events (`parsing`, `extracting`, `validating`, `ready`) to the client over WebSockets or SSE.
