Learning path: AI System Design: Reliable, Fast and Affordable LLM Apps · Step 3 of 15
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.
What you will learn
- Explain why round robin fails when one LLM request can cost far more than another
- Compare least outstanding requests with prefix affinity, reading cache hit rate beside p95
- Design a health check that proves a replica can answer, not just reply
- Drain a replica before a deploy so streaming answers aren't cut
Before you start
- Latency Math: p50, p95, p99
- Sizing and Autoscaling an Inference Service
A load balancer picks which server gets each request. That sounds simple, but one LLM request can cost far more than another, so the old habit of taking turns stops working. We'll keep it high level, with a few pictures and a small simulation.
Four questions for the balancer
- Count: what do we count?
- Reuse: can we reuse work a replica has already done?
- Check: can that replica really answer right now?
- Drain: how do we take it away without cutting anyone off?
Two hot, four cool
Six GPU replicas serve one model. Two sit at 95%, and four at 30%. Why, and how do you balance it?
These numbers are for illustration. A replica is one copy of the model. Picture an analyst asking questions over a big manual. A short question is cheap; a question over the whole manual can cost a hundred times more (also for illustration). The balancer uses round robin, which just takes turns. It deals requests out evenly, and the long ones happen to land on the first two. So the count is the same on all six, and the work isn't. That's your short answer to why: round robin counts requests, and requests aren't equal.
Think of a supermarket host pointing shoppers to tills. A weak host counts people: five in each line looks fair, but one of those five has a full trolley. That's round robin. A good host looks at what's still to scan and points you to the till that will free up first. That's least outstanding requests, where outstanding means started but not finished. And if one till already knows your usual order, going back there is quicker. That's prefix affinity. Count the items, not the people.
Count: balance the work
A small simulation: three replicas, 120 requests, and replica C is slower than the other two.
Round robin gives each replica 40. The middle answer takes 1.05 seconds, and the slow end, p95, takes 6.52. Least outstanding sends each request to the replica with the fewest still running. A long request stays in that count for longer, so a busy replica looks busy, and nobody had to measure its size. The share moves on its own: A gets 65, B gets 33, and the slow one, C, gets 22. The middle answer falls to 0.59, and p95 to 3.05.
Reuse: the cache pulls the other way
The start of a prompt is called the prefix, and here that's the manual. A replica that has already read it can skip that long first step. So prefix affinity sends the same manual to the same replica, and cache hits go from 85% to 95%. Here's the price: one replica takes 100 of the 120 requests, and p95 jumps from 3.05 to 11.45 seconds. A mix of the two reached only 87% hits, with p95 at 4.12. The whole comparison (simulated):
rr {'p50': 1.05, 'p95': 6.52, 'hit_rate': 0.85, 'errors': 0, 'cut': 0, 'share': {'a': 40, 'b': 40, 'c': 40}}
lo {'p50': 0.59, 'p95': 3.05, 'hit_rate': 0.85, 'errors': 0, 'cut': 0, 'share': {'a': 65, 'b': 33, 'c': 22}}
prefix {'p50': 3.05, 'p95': 11.45, 'hit_rate': 0.95, 'errors': 0, 'cut': 0, 'share': {'a': 0, 'b': 100, 'c': 20}}
hybrid {'p50': 1.55, 'p95': 4.12, 'hit_rate': 0.87, 'errors': 0, 'cut': 0, 'share': {'a': 13, 'b': 83, 'c': 24}}
To be fair, that's one small simulation, where least outstanding alone was the fastest. Different traffic could give a different answer. So measure hit rate and p95 together, and say which one you're paying with. More on reading the tail in Latency Math: p50, p95, p99.
Check: up is not healthy
Here's the trap. The health check pings a replica and gets back a 200, which means fine. But the GPU is out of memory and restarting in a loop. Worse, a sick replica fails fast, so it looks empty, and least outstanding sends it even more.
In a small simulation, one replica goes bad. With no health check, that's 48 errors. With one, it's 2. Now look at the middle answer: 0.33 seconds without the check, 2.04 with it. It looks faster when it's broken, because failed requests never reach the latency list. A fast error isn't a fast answer.
health=False {'p50': 0.33, 'p95': 3.19, 'hit_rate': 0.46, 'errors': 48, 'cut': 0, 'share': {'a': 57, 'b': 7, 'c': 8}}
health=True {'p50': 2.04, 'p95': 6.29, 'hit_rate': 0.83, 'errors': 2, 'cut': 0, 'share': {'a': 78, 'b': 7, 'c': 33}}
So a strong check sends a tiny test prompt and expects an answer inside a time limit. That test prompt is a design idea; the simulation didn't test it. Healthy means it can answer, not just that it replies.
Drain: deploys without cutting streams
A long LLM answer streams back slowly, maybe for 90 seconds (for illustration). Stop the replica straight away, and the answer is cut off halfway. In a small simulation, that's four answers cut.
So drain it instead. The replica takes no new requests, and its open streams finish. Answers cut goes to zero, and new questions head to the new version. It isn't entirely free: p95 moves from 5.98 to 6.32 seconds. One more thing to check is the idle timeout on the path. A short one can cut a long stream even when your drain is perfect.
Follow-ups and a simple rubric
These answers are reasoning, not results:
- Why not just add more replicas? Round robin still deals blindly, so you pay for more GPUs and the hot ones stay hot.
- How do you prove the balance works? Watch requests in flight on each replica, the wait before a request starts, and the gap between the busiest and the idlest. In our question, 95 minus 30 is 65 points.
- What's your failure test? Make one replica sick on purpose. It should leave the pool, and errors should stay flat on the others.
| Level | Sounds like |
|---|---|
| Weak | Round robin, more replicas, and a ping for health |
| Good | Least outstanding, prefix affinity, and draining |
| Strong | Also names the cost, reads latency beside errors, and has a failure test to prove it |
The rubric is a teaching guide, not a guaranteed result. When every replica is full, the balancer can't help; that's Back Pressure for LLM Traffic and Sizing and Autoscaling.
Recap
| Question | What to remember |
|---|---|
| Count | Balance on requests still running, not on turns. Simulated p95 6.52 s to 3.05 s |
| Reuse | Prefix affinity: 95% hits, p95 11.45 s. Read hit rate beside p95 |
| Check | A test prompt with a time limit. Simulated errors 48 to 2 |
| Drain | Finish open streams before a deploy. Simulated cut answers 4 to 0 |
Try it yourself
Say one manual gets popular, and its questions all head to one replica. Say out loud what you'd watch, and what you'd give up.
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.
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.