KatInterview

Design a PDF-to-Markdown Document Conversion System

System DesignModel answer
HardModel answerSystem Designsystem-designmachine-learningdistributed-systems

Last updated

Design a scalable, multi-stage document processing architecture to transform complex PDFs into Markdown across synchronous streaming and asynchronous batch workflows.

Architect an enterprise-grade inference engine that converts incoming PDF documents into formatted Markdown text.

Assume the following pretrained/prebuilt modules are available as black-box dependencies:

  1. split_pdf(pdf_bytes) -> List[np.ndarray]: A CPU-bound task that extracts individual pages from a document and rasterizes them into high-resolution image matrices.
  2. ocr(page_images) -> List[str]: A GPU-bound deep learning model that performs visual layout detection and optical character recognition on a batch of page images, returning structured text elements.
  3. to_markdown(ocr_elements) -> str: A memory-bound post-processing worker that formats OCR structured outputs into Markdown syntax and assembles final page sections.

System Requirements & Bottlenecks

The processing pipeline contains three distinct, resource-heterogeneous phases:

  • CPU-heavy page extraction & rendering
  • GPU-heavy deep-learning OCR inference
  • Memory-heavy document assembly and post-processing

You must account for two distinct operating modes:

  1. Low-Latency Synchronous Pipeline (Single-Doc Focus): A client submits a single large PDF (up to ~1,000 pages) over an active API connection and expects formatted Markdown results returned with minimal end-to-end processing delay.
  2. High-Throughput Asynchronous Pipeline (Multi-Tenant Focus): Hundreds of tenants concurrently submit documents of varying sizes (from 1 page to thousands of pages). The service must optimize for overall throughput, cost efficiency, GPU utilization, and fair multi-tenant queue scheduling.

Your architecture should specify API contracts, stream/batch orchestration, dynamic scaling mechanisms, failure recovery, state persistence, and resource management across both modes.

Hints

Hint 1

Consider using object storage references (S3 pointers) rather than passing raw image bytes across network boundaries or message queues.

Hint 2

For synchronous streaming, map-reduce page processing using HTTP/2 Server-Sent Events (SSE) or WebSockets allows progressive delivery while maintaining strict page ordering.

Hint 3

Dynamic micro-batching on the GPU stage is critical to prevent idle GPU cycles during low-traffic periods while maintaining high batch efficiency under heavy load.