Learning path: AI System Design: Reliable, Fast and Affordable LLM Apps · Step 15 of 15
A 500 MB PDF From 50 s to 2 s, the Fast Path: Queue, Status, Streaming and an Acceptance Test That Fails
The fast path for a slow PDF assistant, in a small simulation: 20 chunks on a queue finish at 38.3 s with one worker and 7.9 s with eight, a status shows progress, streaming brings the first word to 0.9 s, and an acceptance test at 1.8 and 2.5 requests a second fails every rescue we tried.
What you will learn
- Put the cold path (reading a big PDF) on a queue of chunks, and see why past four workers the gain shrinks
- Show progress with a status, and survive a failed chunk
- Stream the warm path so the first word lands at 0.9 s while the last word stays at 4 s
- Write an acceptance test with two limits and report honestly when every overload policy fails it
Before you start
- Latency Math: p50, p95, p99
- Back Pressure for LLM Traffic
The interview question: a 500 MB PDF takes 50 seconds to answer, get it to 2. Those three numbers are inputs to the scenario, not a promise. Measuring first split the 50 seconds into a cold part (reading the document, once) and a warm part (answering each question). Now we design the fast path, and then we try to break it. Every timing here comes from a small simulation with seconds we chose ourselves, and one result won't be neat.
Here is the starting split from the same simulation:
1. One span or many (simulated)
upload 6.0 s
extract 8.0 s
ocr 14.0 s
embed 16.0 s
index 2.0 s
cold 46.0 s + warm 4.0 s = 50.0 s
The answer, in one busy kitchen
Picture a busy kitchen. Prep jobs go on a rail, and any free cook takes the next ticket: that's a queue for the reading. A board shows how much prep is done: that's the status. Courses go out as they're ready, so you start eating early, though the meal ends when it ends: that's streaming. And the lunch rush tells you whether any of it holds up: that's the load test.
The cold path on a queue
Nobody should wait on one long job that reads the whole PDF. So we cut it into 20 chunks on a queue, and the person gets a reply straight away: we've got it, we're working on it.
2. Queue ingestion, 20 chunks
1 workers: 25% at 9.7 s, all at 38.3 s
4 workers: 25% at 4.2 s, all at 10.5 s
8 workers: 25% at 3.5 s, all at 7.9 s
Doubling from four to eight workers saved only about two and a half seconds, because the upload itself takes six. These workers are also separate from the ones answering questions, so one big upload doesn't slow everybody else down. How the queue itself stays safe under crashes and repeats is covered in Message Queue for Document Ingestion.
Status, index and a failed chunk
Each document carries a status: queued, indexing, ready. Each finished chunk goes into the vector index, and only what's in the index can be searched. With eight workers, the bar moves like this:
t=2 s indexed 0%
t=4 s indexed 35%
t=6 s indexed 65%
t=8 s indexed 100%
one retry: all at 9.07 s
Now break something. One simulated chunk fails and goes back on the queue. Everything still finishes, at about 9.1 seconds instead of 7.9. The failure costs a little over a second, and we don't lose the document.
The warm path streams
3. Warm query
blocking: first text at 4.0 s
streaming: first token at 0.9 s
Finding the passages and getting the first word ready takes 0.9 seconds. If we wait and send the whole answer at once, the screen stays empty until 4 seconds. If we stream, words go out one at a time from 0.9 seconds, inside our 2-second target.
The honest bit: the last word still lands at 4 seconds both ways. Streaming changes what the person feels. It doesn't make the work any smaller, so always say which clock you mean, the first word or the last.
The acceptance test
So far that's one person asking one question. Before choosing any fix for traffic, write the acceptance test. It has two limits:
- Speed: p95 time to the first word of 2 seconds (95 out of 100 people get the first word within 2 s).
- Fairness to the rejected: a cap on how many people we turn away.
4. Acceptance under load (SLO p95 TTFT 2 s)
1.0 req/s none p95 0.90 s rejected 0.0% short 0.0% PASS
1.8 req/s none p95 4.06 s rejected 0.0% short 0.0% FAIL
1.8 req/s shed p95 1.81 s rejected 11.5% short 0.0% FAIL
2.5 req/s none p95 40.46 s rejected 0.0% short 0.0% FAIL
2.5 req/s degrade p95 3.33 s rejected 0.0% short 48.2% FAIL
2.5 req/s shed p95 1.92 s rejected 26.5% short 0.0% FAIL
At 1 request a second, p95 is 0.9 s and it passes. At 1.8 it's about 4 seconds. At 2.5 it's about 40, because requests arrive faster than they clear. Streaming can't help here: the first word is stuck behind the queue.
Two rescues, and both fail
Two overload policies, from Back Pressure for LLM Traffic:
- Shedding: if the wait is too long, reject the request straight away. At 1.8 requests a second p95 drops to 1.81 s, under the limit, but 11.5% of people were turned away, over the cap. At 2.5 it reaches 1.92 s with 26.5% turned away. Both fail.
- Degrading: keep everyone, give some people a shorter answer. At 2.5 about 48% of answers get shortened, and p95 is still 3.33 s. Fail.
Here's the finding, and we didn't tune it away: every overload row we ran fails. At these rates the design itself has to change, with more capacity or a different agreement with the customer. Saying that out loud is a strong move.
After it ships
Say the design changes and the test passes. This part is design guidance, not measured:
- Monitoring: p95 for each stage, the queue length, and how many people get turned away.
- Alarms: tied to the same limits as the test, so you hear before the customer does.
- Rollback: if a new index or model makes things worse, go back to the old one.
- Feedback loop: slow or wrong answers people report go into the test documents, so the test gets harder over time.
Follow-ups and a simple rubric
- Can you answer before indexing finishes? As a design option, yes, if the screen says the answer only covers the pages read so far.
- Shedding got you under the limit, so can you ship it? Not yet. You've moved the pain onto the people you rejected, and the test has to count them.
- One chunk keeps failing? Stop after a few tries, set it aside, and let the document say which pages are missing. One bad chunk shouldn't sink a two-thousand-page file.
| Level | What it sounds like |
|---|---|
| Weak | Adds more workers and promises two seconds |
| Solid | Queue, status and streaming, and says which clock it means |
| Strong | Also writes the acceptance test first, counts the people turned away, and says when the design fails |
The rubric is a suggestion, not any company's official sheet.
Recap
| Piece | Simulated result |
|---|---|
| Cold path on a queue | 38.3 s with 1 worker, 10.5 s with 4, 7.9 s with 8 |
| Status and a failed chunk | 0% at 2 s to 100% at 8 s; one retry costs about 1.2 s |
| Streaming | First word 0.9 s instead of 4.0 s; last word 4.0 s both ways |
| Acceptance under load | Passes at 1.0 req/s; every rescue at 1.8 and 2.5 fails |
Try it yourself
Pick a feature you know and write its acceptance test with two limits: one for speed, and one for the people you turn away. Then guess which limit breaks first when traffic doubles.
Related course on Udemy
Machine Learning Engineer: From First Model to ML Engineer
Learn machine learning in Python, then ship it: data analysis, regression, classification, tuning, deployment and MLOps.View on Udemy ↗: Machine Learning Engineer: From First Model to ML Engineer (paid course, opens in a new tab)Related posts
Fallback Models for an LLM App: A Chain That Checks Region and Fit, Flags Degraded Answers and Gets Rehearsed
When the main model goes down, the backup has to be allowed to take the data, able to read the whole request and tested before the bad day. A simulated four-model chain for an EU customer: a 6,000-token request lands on mid-eu, flagged degraded; a 10,000-token request is served by nobody, on purpose.
Back Pressure for LLM Traffic: Bound the Queue, Give Chat Its Own Lane, Count Tokens, Cancel When the Caller Leaves
A bulk upload buries the chat. In a small simulation, one shared queue grows to 199 waiting and the middle chat answer takes 80.2 s. A bound plus a chat lane brings it to 0.35 s while 178 bulk jobs are told to retry in 10 s, and cancelling abandoned calls saves 21,000 wasted tokens.
Load Balancing LLM Traffic: Count the Work, Weigh the Prefix Cache, Check Real Health, Drain Before Deploys
Six GPU replicas, two at 95% and four at 30%: round robin counts requests, and requests aren't equal. In a small simulation, least outstanding requests cuts p95 from 6.52 s to 3.05 s, prefix affinity lifts cache hits to 95% but pushes p95 to 11.45 s, a real health check takes errors from 48 to 2, and draining cuts 0 answers instead of 4.
A Message Queue for Document Ingestion: At Least Once, Leases, an Idempotent Consumer and a Dead Letter Queue
A queue doesn't lose a document, but it can hand one out twice. In a small simulation, a worker crash makes document two go from 3 to 6 chunks and the index holds 11 when the documents hold 8. An idempotent consumer brings it back to 8 with the same two deliveries, and a limit of three tries moves a poison file to a dead letter queue.