<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mohamed Elsayed Ali</title>
    <description>The latest articles on DEV Community by Mohamed Elsayed Ali (@mohamedelsayed002).</description>
    <link>https://dev.to/mohamedelsayed002</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2295780%2F49ae4bf8-5baf-485a-9360-23c8aa9213b2.jpeg</url>
      <title>DEV Community: Mohamed Elsayed Ali</title>
      <link>https://dev.to/mohamedelsayed002</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mohamedelsayed002"/>
    <language>en</language>
    <item>
      <title># How I Built a PDF Processing Pipeline Designed to Scale Toward 10k Users — FastAPI, Inngest &amp; Smart Async Design</title>
      <dc:creator>Mohamed Elsayed Ali</dc:creator>
      <pubDate>Thu, 07 May 2026 11:10:03 +0000</pubDate>
      <link>https://dev.to/mohamedelsayed002/-how-i-built-a-pdf-processing-pipeline-designed-to-scale-toward-10k-users-fastapi-inngest--46k8</link>
      <guid>https://dev.to/mohamedelsayed002/-how-i-built-a-pdf-processing-pipeline-designed-to-scale-toward-10k-users-fastapi-inngest--46k8</guid>
      <description>&lt;p&gt;I recently built a PDF processing pipeline inside my project &lt;strong&gt;Playground&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The goal was simple:&lt;/p&gt;

&lt;p&gt;Users upload PDFs, the API responds immediately, and heavy processing happens in the background without blocking request latency.&lt;/p&gt;

&lt;p&gt;That sounds straightforward until traffic increases.&lt;/p&gt;

&lt;p&gt;A naive implementation works fine for a few users — but starts falling apart when many uploads arrive at once.&lt;/p&gt;

&lt;p&gt;In this article, I’ll walk through the architecture, the async design decisions, and a few lessons I learned while building it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Repository:&lt;/strong&gt; &lt;a href="https://github.com/MohamedElsayed002/playground?utm_source=chatgpt.com" rel="noopener noreferrer"&gt;Playground on GitHub&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;A common first implementation looks something like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;receive upload&lt;/li&gt;
&lt;li&gt;parse PDF inside the request&lt;/li&gt;
&lt;li&gt;extract text&lt;/li&gt;
&lt;li&gt;store results&lt;/li&gt;
&lt;li&gt;return response&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It works — until multiple users upload files concurrently.&lt;/p&gt;

&lt;p&gt;The biggest issue is that &lt;strong&gt;PDF parsing is not purely async work&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Libraries like &lt;code&gt;pdfplumber&lt;/code&gt; perform CPU-heavy parsing. If that happens directly inside an async route, the event loop gets blocked.&lt;/p&gt;

&lt;p&gt;That creates a chain reaction:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;slower response times&lt;/li&gt;
&lt;li&gt;reduced concurrency&lt;/li&gt;
&lt;li&gt;request pileups under load&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In short:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Async helps with I/O. It does not automatically solve CPU-bound work.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  My Solution
&lt;/h2&gt;

&lt;p&gt;Instead of processing inside the request lifecycle, I split the pipeline into two stages.&lt;/p&gt;

&lt;h3&gt;
  
  
  Request path
&lt;/h3&gt;

&lt;p&gt;The API only handles lightweight operations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;validate file type&lt;/li&gt;
&lt;li&gt;perform idempotency checks&lt;/li&gt;
&lt;li&gt;upload raw file to S3&lt;/li&gt;
&lt;li&gt;create database record&lt;/li&gt;
&lt;li&gt;trigger background job&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then it returns immediately.&lt;/p&gt;

&lt;h3&gt;
  
  
  Background worker
&lt;/h3&gt;

&lt;p&gt;The worker performs heavy tasks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;download from S3&lt;/li&gt;
&lt;li&gt;scan file&lt;/li&gt;
&lt;li&gt;parse PDF&lt;/li&gt;
&lt;li&gt;extract structured data&lt;/li&gt;
&lt;li&gt;save final result&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Architecture
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client
  ↓
FastAPI upload endpoint
  ↓
S3 object storage
  ↓
Inngest event
  ↓
Background worker
  ↓
Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fty9o4r5woao2i542gduf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fty9o4r5woao2i542gduf.png" alt="PDF pipeline" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That separation keeps the API responsive even when parsing becomes expensive.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Idempotency Matters
&lt;/h2&gt;

&lt;p&gt;One subtle issue in async pipelines is duplicate execution.&lt;/p&gt;

&lt;p&gt;A user may retry the upload.&lt;br&gt;
A network timeout may happen.&lt;br&gt;
A background worker may retry after failure.&lt;/p&gt;

&lt;p&gt;Without idempotency, the same file can be processed multiple times.&lt;/p&gt;

&lt;p&gt;My upload endpoint checks for an existing idempotency key before doing any heavy work.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IdempotencyKey&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IdempotencyKey&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;idempotency_key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;existing_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;scalars&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;first&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;existing_key&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;existing_key&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;existing_key&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That small check prevents duplicate parsing and duplicate storage.&lt;/p&gt;

&lt;p&gt;In production systems, this becomes surprisingly important.&lt;/p&gt;




&lt;h2&gt;
  
  
  Upload Endpoint: Return Fast
&lt;/h2&gt;

&lt;p&gt;The upload route stores the file and immediately schedules background processing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;inngest_client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;inngest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Event&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;pdf/upload.requested&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;event_payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;file_id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;file_record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;status&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;FileStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PROCESSING&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;message&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;PDF uploaded. Processing in background.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The client receives a response immediately and can poll later for status updates.&lt;/p&gt;

&lt;p&gt;That means the API is optimized for &lt;strong&gt;fast acknowledgement&lt;/strong&gt;, not heavy computation.&lt;/p&gt;




&lt;h2&gt;
  
  
  CPU-bound Parsing Needs a Thread Boundary
&lt;/h2&gt;

&lt;p&gt;The worker downloads the file and parses it in the background.&lt;/p&gt;

&lt;p&gt;The important part is this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;file_bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_thread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blocking_work&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;pdf_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_thread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_parse_pdf_bytes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;raw_bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;Because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;S3 SDK calls are blocking&lt;/li&gt;
&lt;li&gt;PDF parsing is CPU-heavy&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If those ran directly inside async functions, they would block the event loop.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;asyncio.to_thread()&lt;/code&gt; moves that work off the main async execution path.&lt;/p&gt;

&lt;p&gt;That was one of the most useful lessons from building this pipeline.&lt;/p&gt;




&lt;h2&gt;
  
  
  Background Workflow
&lt;/h2&gt;

&lt;p&gt;The worker itself follows a predictable sequence:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Download file from S3&lt;/li&gt;
&lt;li&gt;Scan file&lt;/li&gt;
&lt;li&gt;Parse PDF pages&lt;/li&gt;
&lt;li&gt;Extract structured metadata&lt;/li&gt;
&lt;li&gt;Save results to database&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The parsing step returns:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;page-by-page text&lt;/li&gt;
&lt;li&gt;extracted tables&lt;/li&gt;
&lt;li&gt;full combined text&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That makes the output usable for both search and downstream LLM enrichment.&lt;/p&gt;




&lt;h2&gt;
  
  
  Error Handling and Audit Logging
&lt;/h2&gt;

&lt;p&gt;One thing I cared about early was observability.&lt;/p&gt;

&lt;p&gt;Every major step writes audit events:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;upload received&lt;/li&gt;
&lt;li&gt;validation failed&lt;/li&gt;
&lt;li&gt;S3 stored&lt;/li&gt;
&lt;li&gt;processing started&lt;/li&gt;
&lt;li&gt;worker completed&lt;/li&gt;
&lt;li&gt;worker failed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That made debugging much easier than relying only on exception traces.&lt;/p&gt;

&lt;p&gt;When async systems grow, visibility becomes just as important as the code itself.&lt;/p&gt;




&lt;h2&gt;
  
  
  Lessons Learned
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Async is not magic
&lt;/h3&gt;

&lt;p&gt;Async improves I/O concurrency.&lt;/p&gt;

&lt;p&gt;It does not automatically make CPU-heavy workloads scalable.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. Keep request handlers thin
&lt;/h3&gt;

&lt;p&gt;Request handlers should do only what is necessary to accept work.&lt;/p&gt;

&lt;p&gt;Heavy processing belongs in workers.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. Idempotency is underrated
&lt;/h3&gt;

&lt;p&gt;Retries are normal in distributed systems.&lt;/p&gt;

&lt;p&gt;Designing for safe retries makes the whole pipeline more reliable.&lt;/p&gt;




&lt;h3&gt;
  
  
  4. Background jobs improve system stability
&lt;/h3&gt;

&lt;p&gt;Moving parsing out of request-response flow improves:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;latency&lt;/li&gt;
&lt;li&gt;resilience&lt;/li&gt;
&lt;li&gt;operational predictability&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  What I’d Improve Next
&lt;/h2&gt;

&lt;p&gt;A few things I’d like to add next:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;dedicated worker concurrency limits&lt;/li&gt;
&lt;li&gt;chunked parsing for very large PDFs&lt;/li&gt;
&lt;li&gt;metrics around parse duration and queue latency&lt;/li&gt;
&lt;li&gt;smarter retry classification for transient failures&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;This project was a useful reminder that scaling is often less about “making code faster” and more about &lt;strong&gt;putting work in the right place&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The biggest architectural decision was simple:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;return fast, process later.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That one decision changed the entire behavior of the system.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Full repository:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://github.com/MohamedElsayed002/playground?utm_source=chatgpt.com" rel="noopener noreferrer"&gt;Playground GitHub repository&lt;/a&gt;&lt;/p&gt;

</description>
      <category>fastapi</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
