Single logo
  • Courses
  • Blog
  • About
Join free on SkoolJoin free on Skool

Learning path · 15 steps

AI System Design: Reliable, Fast and Affordable LLM Apps
  1. 1
    Fallback Models for an LLM App: A Chain That Checks Region and Fit, Flags Degraded Answers and Gets Rehearsed
  2. 2
    Back Pressure for LLM Traffic: Bound the Queue, Give Chat Its Own Lane, Count Tokens, Cancel When the Caller Leaves
  3. 3
    Load Balancing LLM Traffic: Count the Work, Weigh the Prefix Cache, Check Real Health, Drain Before Deploys
  4. 4
    A Message Queue for Document Ingestion: At Least Once, Leases, an Idempotent Consumer and a Dead Letter Queue
  5. 5
    Caching for AI Apps: Four Layers, What Goes in Each Key, and How a Delete Travels
  6. 6
    Semantic Caching: Why a Similarity Threshold Can't Stop Confident Wrong Answers
  7. 7
    LLM Model Routing: Rules, a Classifier or a Cascade, Judged on Cost per Win and Every Topic
  8. 8
    Observability and Tracing for LLM Apps: A Span per Stage, Redaction First, Bad Traces Become Tests
  9. 9
    Cutting p95 and Cost in Half for an LLM App: Read the Bill, Match the Lever, Guard Success
  10. 10
    Guardrails for an LLM Agent: Check What Comes In, What It Wants to Do, and What Goes Out
  11. 11
    Embedding Migration With Zero Downtime: Two Indexes, a Dual-Write Backfill, a Shadow Check and a Flag
  12. 12
    Design a Multi-Tenant RAG Assistant, Part 1: Requirements, Rough Numbers and a Contract
  13. 13
    Design a Multi-Tenant RAG Assistant, Part 2: Tenant Isolation, a Scoped Cache and Walking an Outage
  14. 14
    A 500 MB PDF Takes 50 s to Answer, Get It to 2 s: Measure First, Then Promise Per Path
  15. 15
    A 500 MB PDF From 50 s to 2 s, the Fast Path: Queue, Status, Streaming and an Acceptance Test That Fails
  1. Blog
  2. /
  3. AI Engineering
  4. /
  5. AI System Design

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.

Chaand Sheikh

·27 November 2026AI System Design

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

  1. Count: what do we count?
  2. Reuse: can we reuse work a replica has already done?
  3. Check: can that replica really answer right now?
  4. 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 against least outstanding Round robin Least outstanding 40 40 40 65 33 22 A B C slow A B C slow p50 1.05 s p95 6.52 s p50 0.59 s p95 3.05 s Requests per replica, simulated, 120 in total
Nobody added a server. We stopped dealing the work out blindly.

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.

Draining before a deploy 1 Stop new requests to the old replica 2 Open streams finish nobody is cut off 3 New version takes new questions Simulated: answers cut 4 to 0; p95 5.98 s to 6.32 s Check the idle timeout on the path too
Draining costs a little p95, and that's a fair trade.

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.
LevelSounds like
WeakRound robin, more replicas, and a ping for health
GoodLeast outstanding, prefix affinity, and draining
StrongAlso 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

QuestionWhat to remember
CountBalance on requests still running, not on turns. Simulated p95 6.52 s to 3.05 s
ReusePrefix affinity: 95% hits, p95 11.45 s. Read hit rate beside p95
CheckA test prompt with a time limit. Simulated errors 48 to 2
DrainFinish 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.

Tried the exercise? Share your answer or ask a question.

The StudyEasy AI Community is free to join, with the lesson files, notebooks and code.

Join free on Skool

Keep going in the learning path AI System Design: Reliable, Fast and Affordable LLM Apps · step 3 of 15

Previous
Back Pressure for LLM Traffic: Bound the Queue, Give Chat Its Own Lane, Count Tokens, Cancel When the Caller Leaves
Next post
A Message Queue for Document Ingestion: At Least Once, Leases, an Idempotent Consumer and a Dead Letter Queue

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.

On this page

  • Four questions for the balancer
  • Two hot, four cool
  • Count: balance the work
  • Reuse: the cache pulls the other way
  • Check: up is not healthy
  • Drain: deploys without cutting streams
  • Follow-ups and a simple rubric
  • Recap
  • Try it yourself
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool

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)

Single logo

Empowering Education, One Course at a Time

About
About usContact usFAQs
Legal
Terms of ServicePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.