All case studies

Scaling to 6,000 PDFs Per Hour

How I optimized a document generation system to process 6,000 PDFs per hour on a single Railway worker, a throughput number that typically requires 5-10 workers.

  • High Scale
  • Single Worker
  • 99.7% Uptime
Throughput
6,000/hr
PDFs generated per hour
P95 Latency
<2s
Response time at 95th percentile
Infrastructure
1 Worker
Single Railway instance
Success Rate
99.7%
Job completion rate

The Challenge

KamkmPDF started as a simple Next.js app with a cron job. It worked for 50 users, but when hundreds started uploading simultaneously, the queue backed up. Most teams would deploy 5-10 workers. I wanted to see how far a single optimized worker could go.

Starting PointFinal Results
Throughput200-300/hr6,000/hr
Generation time5-10s<2s
Concurrent jobs3 before crash10 stable
AI keys1 (rate limits)11 with failover

Key Optimizations

Four major changes that transformed performance

  1. Go Worker Rewrite

    Migrated from TypeScript to Go for better concurrency and memory efficiency using goroutines.

    2x throughput increase

  2. Cerebras AI Integration

    Implemented priority-based AI model selection with Cerebras GPT-oss-120b as primary for high-speed inference.

    5-10x faster than GPT-4

  3. Database Optimization

    Atomic job claiming with FOR UPDATE SKIP LOCKED, connection pooling, and strategic indexing.

    Query time: 100ms → 5ms

  4. Browser Pooling

    Reusable Puppeteer instances instead of spawning new browsers per job.

    10 concurrent PDFs, same memory

Architecture Highlights

Built for horizontal scaling from day one

Queue System

  • PostgreSQL-backed queue with atomic job claiming
  • Round-robin API key distribution for AI redundancy
  • Multi-tier PDF fallback (Puppeteer → PDFKit → Static)

Worker Design

  • Browser pool with automatic cleanup and monitoring
  • Horizontal scaling ready — just add more workers

Technology Stack

Carefully chosen for maximum throughput

Frontend

  • Next.js 15
  • React 19
  • Tailwind CSS
  • tRPC

Worker

  • Go
  • Goroutines
  • Railway
  • Docker

Database

  • PostgreSQL 14
  • Drizzle ORM
  • Connection Pooling

AI

  • Cerebras GPT-oss-120b
  • OpenRouter
  • 11 API Keys
  • Auto-failover

PDF Generation

  • Puppeteer
  • PDFKit
  • Browser Pool
  • Memory Management

Storage

  • UploadThing v7
  • Signed URLs
  • File Metadata

Cost Efficiency

Enterprise throughput, startup budget

Infrastructure

Single Railway worker
~$20/mo
PostgreSQL database
~$15/mo
No Redis or queues
$0

AI Costs

Cerebras GPT-oss-120b
~$0.002/PDF
At 6,000 PDFs/hour
~$12/hr
vs GPT-4 equivalent
5x savings