Skip to content

Instantly share code, notes, and snippets.

@deepaks7n
Created May 22, 2025 21:30
Show Gist options
  • Select an option

  • Save deepaks7n/d44b597ca9de6c0a73680e3581ec2f8b to your computer and use it in GitHub Desktop.

Select an option

Save deepaks7n/d44b597ca9de6c0a73680e3581ec2f8b to your computer and use it in GitHub Desktop.

PDF Invoice Parser System - High Level Design (HLD)

1. Executive Summary

The PDF Invoice Parser System is a scalable, cloud-native solution designed to process invoices from 1000+ vendors with unique formats. The system employs a strategy pattern for vendor-specific parsing, supports both digital and scanned PDFs, and is optimized for serverless deployment.

2. System Architecture Overview

2.1 High-Level Architecture

graph TB
    subgraph "Client Layer"
        A[Web Portal]
        B[API Clients]
        C[Batch Upload]
    end
    
    subgraph "API Gateway"
        D[Load Balancer]
        E[Auth Service]
        F[Rate Limiter]
    end
    
    subgraph "Processing Layer"
        G[Ingestion Service]
        H[Message Queue]
        I[Vendor Detection]
        J[Parser Orchestrator]
    end
    
    subgraph "Parser Engines"
        K[Text Parser]
        L[OCR Engine]
        M[AI Parser]
    end
    
    subgraph "Data Layer"
        N[Document Storage]
        O[Parser Registry]
        P[Results Database]
    end
    
    subgraph "Supporting Services"
        Q[Monitoring]
        R[Logging]
        S[Cache Layer]
    end
    
    A --> D
    B --> D
    C --> D
    D --> E
    E --> F
    F --> G
    G --> H
    H --> I
    I --> J
    J --> K
    J --> L
    J --> M
    K --> P
    L --> P
    M --> P
    G --> N
    I --> O
    J --> S
    
    Q -.-> G
    Q -.-> J
    R -.-> G
    R -.-> J
Loading

2.2 Component Architecture

flowchart LR
    subgraph "Ingestion Layer"
        A1[API Handler]
        A2[File Validator]
        A3[Queue Publisher]
        A1 --> A2
        A2 --> A3
    end
    
    subgraph "Detection Layer"
        B1[Text Extractor]
        B2[Vendor Matcher]
        B3[AI Classifier]
        B1 --> B2
        B2 --> B3
    end
    
    subgraph "Parsing Layer"
        C1[Strategy Selector]
        C2[Parser Factory]
        C3[Parser Pool]
        C1 --> C2
        C2 --> C3
    end
    
    subgraph "Storage Layer"
        D1[(S3 Bucket)]
        D2[(DynamoDB)]
        D3[(Redis Cache)]
    end
    
    A3 --> B1
    B3 --> C1
    C3 --> D2
    A2 --> D1
    B2 --> D3
Loading

3. Data Flow Architecture

3.1 Processing Pipeline Flow

sequenceDiagram
    participant Client
    participant API
    participant Queue
    participant Detector
    participant Parser
    participant Storage
    participant Cache
    
    Client->>API: Upload PDF Invoice
    API->>API: Validate & Generate ID
    API->>Storage: Store Original PDF
    API->>Queue: Publish Parse Job
    API-->>Client: Return Job ID
    
    Queue->>Detector: Dequeue Job
    Detector->>Storage: Fetch PDF
    Detector->>Detector: Extract Text
    Detector->>Cache: Check Vendor Cache
    
    alt Vendor Found in Cache
        Detector->>Parser: Send with Vendor ID
    else Vendor Not in Cache
        Detector->>Detector: Run AI Classification
        Detector->>Cache: Update Cache
        Detector->>Parser: Send with Vendor ID
    end
    
    Parser->>Parser: Select Strategy
    Parser->>Parser: Execute Parsing
    Parser->>Storage: Store Results
    Parser->>Queue: Publish Completion
    
    Client->>API: Poll/Webhook Status
    API->>Storage: Fetch Results
    API-->>Client: Return Parsed Data
Loading

3.2 Parser Strategy Selection Flow

flowchart TD
    A[Receive PDF Document] --> B{Is Digital PDF?}
    B -->|Yes| C[Text Extraction Module]
    B -->|No| D[OCR Module]
    
    C --> E{Extraction Quality Check}
    D --> F{OCR Confidence Check}
    
    E -->|High Quality| G[Rule-Based Parser]
    E -->|Medium Quality| H[Hybrid Parser]
    E -->|Low Quality| I[AI Parser]
    
    F -->|High Confidence| G
    F -->|Medium Confidence| H
    F -->|Low Confidence| I
    
    G --> J[Apply Vendor Template]
    H --> K[Template + AI Assist]
    I --> L[Full AI Processing]
    
    J --> M[Validate Results]
    K --> M
    L --> M
    
    M --> N{Validation Pass?}
    N -->|Yes| O[Store Results]
    N -->|No| P[Error Handler]
    P --> Q{Retry Available?}
    Q -->|Yes| I
    Q -->|No| R[Manual Review Queue]
Loading

4. Serverless Architecture

4.1 Lambda Function Distribution

graph LR
    subgraph "API Functions"
        A1[Upload Handler]
        A2[Status Handler]
        A3[Result Handler]
    end
    
    subgraph "Processing Functions"
        B1[Vendor Detector]
        B2[Text Parser]
        B3[OCR Parser]
        B4[AI Parser]
    end
    
    subgraph "Support Functions"
        C1[Cache Warmer]
        C2[Health Check]
        C3[Cleanup Job]
    end
    
    subgraph "Triggers"
        D1[API Gateway]
        D2[SQS Queue]
        D3[EventBridge]
    end
    
    D1 --> A1
    D1 --> A2
    D1 --> A3
    D2 --> B1
    D2 --> B2
    D2 --> B3
    D2 --> B4
    D3 --> C1
    D3 --> C2
    D3 --> C3
Loading

4.2 Container Architecture

graph TB
    subgraph "Container Registry"
        A[Base Python Image]
        B[Parser Libraries Layer]
        C[AI/ML Models Layer]
    end
    
    subgraph "Lambda Containers"
        D[Vendor Detector Container]
        E[Text Parser Container]
        F[OCR Parser Container]
        G[AI Parser Container]
    end
    
    subgraph "ECS/Fargate Tasks"
        H[Batch Processor]
        I[Model Training]
        J[Data Pipeline]
    end
    
    A --> D
    A --> E
    A --> F
    A --> G
    B --> D
    B --> E
    B --> F
    C --> F
    C --> G
    
    A --> H
    B --> H
    C --> I
Loading

5. Database Schema

5.1 Data Model

erDiagram
    INVOICE_JOB {
        string job_id PK
        string status
        timestamp created_at
        timestamp updated_at
        string vendor_id FK
        string document_url
        json metadata
    }
    
    VENDOR {
        string vendor_id PK
        string vendor_name
        string parser_strategy
        json parser_config
        boolean is_active
        timestamp last_updated
    }
    
    PARSE_RESULT {
        string result_id PK
        string job_id FK
        json extracted_data
        float confidence_score
        string parser_used
        json validation_errors
    }
    
    PARSER_TEMPLATE {
        string template_id PK
        string vendor_id FK
        string template_version
        json field_mappings
        json extraction_rules
    }
    
    INVOICE_JOB ||--|| VENDOR : "belongs to"
    INVOICE_JOB ||--|| PARSE_RESULT : "produces"
    VENDOR ||--o{ PARSER_TEMPLATE : "has"
Loading

6. Security Architecture

6.1 Security Layers

graph TD
    subgraph "Network Security"
        A[CloudFront CDN]
        B[WAF Rules]
        C[VPC Private Subnets]
    end
    
    subgraph "Application Security"
        D[API Key Auth]
        E[JWT Tokens]
        F[Role-Based Access]
    end
    
    subgraph "Data Security"
        G[Encryption at Rest]
        H[Encryption in Transit]
        I[Data Masking]
    end
    
    subgraph "Compliance"
        J[GDPR Controls]
        K[Audit Logging]
        L[Data Retention]
    end
    
    A --> B
    B --> C
    D --> E
    E --> F
    G --> I
    H --> I
    J --> K
    K --> L
Loading

7. Monitoring and Observability

7.1 Monitoring Architecture

graph LR
    subgraph "Metrics Collection"
        A[CloudWatch Metrics]
        B[Custom Metrics]
        C[Application Metrics]
    end
    
    subgraph "Logging"
        D[CloudWatch Logs]
        E[Application Logs]
        F[Audit Logs]
    end
    
    subgraph "Tracing"
        G[X-Ray Traces]
        H[Distributed Tracing]
    end
    
    subgraph "Dashboards"
        I[Performance Dashboard]
        J[Error Dashboard]
        K[Business Metrics]
    end
    
    A --> I
    B --> I
    C --> I
    D --> J
    E --> J
    F --> K
    G --> I
    H --> J
Loading

8. Scalability Considerations

8.1 Auto-Scaling Architecture

graph TB
    subgraph "Load Metrics"
        A[Queue Depth]
        B[Response Time]
        C[Error Rate]
    end
    
    subgraph "Scaling Policies"
        D[Lambda Concurrency]
        E[ECS Task Count]
        F[DynamoDB Capacity]
    end
    
    subgraph "Scaling Actions"
        G[Scale Out]
        H[Scale In]
        I[Circuit Breaker]
    end
    
    A --> D
    B --> E
    C --> F
    D --> G
    D --> H
    E --> G
    E --> H
    F --> I
Loading

9. Deployment Architecture

9.1 CI/CD Pipeline

graph LR
    subgraph "Source Control"
        A[GitHub Repository]
        B[Feature Branches]
        C[Main Branch]
    end
    
    subgraph "Build Stage"
        D[Unit Tests]
        E[Integration Tests]
        F[Container Build]
    end
    
    subgraph "Deploy Stage"
        G[Dev Environment]
        H[Staging Environment]
        I[Production Environment]
    end
    
    subgraph "Validation"
        J[Smoke Tests]
        K[Performance Tests]
        L[Rollback Trigger]
    end
    
    B --> D
    C --> D
    D --> E
    E --> F
    F --> G
    G --> J
    J --> H
    H --> K
    K --> I
    K --> L
Loading

10. Performance Optimization

10.1 Caching Strategy

graph TD
    subgraph "Cache Layers"
        A[CDN Cache]
        B[API Gateway Cache]
        C[Redis Cache]
        D[Lambda Memory Cache]
    end
    
    subgraph "Cached Data"
        E[Vendor Mappings]
        F[Parser Templates]
        G[ML Model Weights]
        H[Frequent Results]
    end
    
    subgraph "Cache Policies"
        I[TTL Management]
        J[Invalidation Rules]
        K[Warm-up Strategy]
    end
    
    A --> E
    B --> F
    C --> E
    C --> F
    D --> G
    D --> H
    I --> A
    I --> B
    I --> C
    J --> C
    K --> D
Loading

11. Cost Optimization

11.1 Resource Allocation

pie title "Estimated Cost Distribution"
    "Lambda Functions" : 35
    "Storage (S3)" : 20
    "Database (DynamoDB)" : 15
    "API Gateway" : 10
    "Data Transfer" : 10
    "ML/AI Services" : 10
Loading

12. Key Design Decisions

  1. Serverless First: Lambda for stateless processing, ECS for long-running tasks
  2. Event-Driven: SQS/SNS for decoupling components
  3. Multi-Strategy Parsing: Text, OCR, and AI approaches based on document quality
  4. Vendor Registry: Centralized configuration for parser strategies
  5. Horizontal Scaling: All components designed for horizontal scaling
  6. Fault Tolerance: Circuit breakers, retries, and dead letter queues
  7. Cost Optimization: Resource pooling and intelligent caching

13. Technology Stack

  • Languages: Python 3.11+
  • Frameworks: FastAPI, AWS Lambda Runtime
  • PDF Processing: pdf-plumber, PyPDF2, pdfminer
  • OCR: Tesseract, AWS Textract, Google Vision API
  • AI/ML: OpenAI API, Anthropic Claude, AWS Comprehend
  • Storage: S3, DynamoDB, Redis
  • Compute: Lambda, ECS/Fargate
  • Messaging: SQS, SNS, EventBridge
  • Monitoring: CloudWatch, X-Ray
  • Security: IAM, KMS, Secrets Manager
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment