Learning path: AI System Design: Reliable, Fast and Affordable LLM Apps · Step 2 of 15
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.
What you will learn
- Explain back pressure and why a bigger buffer only makes the wait longer
- Bound a queue and reply 429 with Retry-After when it is full
- Separate chat and bulk work into lanes, and say who pays for it
- Count tokens in flight and cancel work when the caller leaves
Before you start
- Latency Math: p50, p95, p99
- LLM Tokens, Context Windows and Cost
Back pressure means a slow part of your system tells the part before it to slow down, instead of piling up work that it can never finish. It matters a lot for LLM traffic, because one request can cost far more than another. And it's a favourite interview question.
The load and servers in this lesson are simulated. No provider was called.
Four moves
There are four moves, and that's also the shape of your answer:
- Bound the queue, so it can't grow forever.
- Give chat its own lane.
- Count tokens, not requests.
- Stop working when the caller leaves.
A bulk job buries the chat
A customer bulk uploads 50,000 documents at once, and chat latency triples. Design back pressure.
Latency is just how long you wait. Let's watch a smaller flood in a small simulation: 60 chat questions and 200 bulk jobs share one queue. The queue climbs to 199 requests waiting, and chat pays for it. The middle chat answer takes 80.2 seconds. Nothing was rejected, and every job finished. The server didn't break. It just got buried.
Think of a conveyor belt in a factory. At the end, a packer packs boxes at a steady speed: that's your GPU server. The belt is short, so it holds only a few boxes: that's the bounded queue. When the packer is full, the belt stops and the loader waits: that's the back pressure signal. The belt never grows. The loader slows down instead.
Move 1: bound it, and say no
The weak answer is a bigger buffer. It only makes the wait longer, until memory runs out. So give the queue a fixed size. When it's full, reply with a 429 and Retry-After, which tells the sender when to come back.
In the same simulated flood, the line stops at 20, not 199, and 178 bulk jobs get told to try again in 10 seconds. Be honest about this: those jobs didn't run, and the simulation doesn't send them back. We didn't create any capacity. We just said no early, and that beats saying yes and failing late.
Move 2: give chat its own lane
Chat gets one lane and bulk work gets another, so a chat question never stands behind a bulk job. When things get tight, bulk is the one that waits.
With the bound and the lanes together, the middle chat answer drops from 80.2 seconds to 0.35. We switched both on at once, so we can't split that credit. And bulk pays: 22 bulk jobs finished, not 200. Somebody always pays, so say who. The simulator's summary (simulated):
one queue
chat_done 60
chat_p50 80.2
chat_p95 92.75
bulk_done 200
rejected {}
max_depth 199
lanes + bound
chat_done 60
chat_p50 0.35
chat_p95 0.35
bulk_done 22
rejected {'bulk': 178}
retry_after 10.0
max_depth 20
If p50 and p95 are new to you, see Latency Math: p50, p95, p99.
Move 3: count tokens, not requests
So far we've counted requests, but requests aren't equal. On our belt, one giant box takes far longer than a small one. A long request keeps the GPU busy far longer than a short one too. So keep a running count of tokens in flight, meaning the text the GPU is busy with right now. If the next request doesn't fit in the room that's left, it waits at the gate, and chat gets the room first. The request count can look fine while the GPU is full, and that's why you watch tokens. (This move is the idea only; the lesson has no numbers for it.)
Move 4: stop when the caller leaves
People forget this one. In a small simulation, half the callers hang up early, and the GPU keeps writing answers nobody will read.
That wastes 21,000 tokens, and the last job finishes at 14.2 seconds. So when a caller leaves, cancel and give the tokens back. The waste falls to zero, the last job finishes at 7.7 seconds, and the same 16 jobs get done.
Follow-ups and a simple rubric
- Bounded queue, or reject straight away? Usually a small bound to absorb short bursts, then reject.
- How do you tell the uploader to slow down? A 429 with Retry-After says when to come back.
- How do you prove it works? Watch queue depth, rejection rate and time in queue. Then flood it on purpose, and check that chat holds.
| Level | Sounds like |
|---|---|
| Weak | A bigger buffer and more servers; never says what happens when it's full |
| Good | A bound, lanes and a 429 |
| Strong | Also counts tokens, cancels when the caller leaves, and says who pays and how you'd measure it |
The rubric is a guide for practice, not a guaranteed result. Back pressure works with the other side of the call: well-behaved retries (Retries and Backoff) and enough replicas (Sizing and Autoscaling an Inference Service). How work is spread across those replicas is in Load Balancing LLM Traffic.
Recap
| Move | What to remember |
|---|---|
| Bound | Fixed size, 429 + Retry-After when full. Saying no early creates no capacity |
| Lanes | Simulated: middle chat answer 80.2 s to 0.35 s; bulk done 200 to 22 |
| Tokens | Count tokens in flight, not requests |
| Cancel | Simulated: 21,000 wasted tokens to 0, last job 14.2 s to 7.7 s |
Try it yourself
Picture those 178 rejected bulk jobs coming back after 10 seconds. What would you watch to see whether chat holds?
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.
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.
Caching for AI Apps: Four Layers, What Goes in Each Key, and How a Delete Travels
A support bot's cache can hit 97% of requests and still answer only 52% correctly. Four cache layers, the key each one needs, how a document delete has to travel through all of them, and a 200-request simulation that puts hit rate next to correct answers.