Ch.55: LLM System Design: Why Streaming Answers Stall

Transcript

0:00 Type a question into an app, and an LLM serving system runs a trained language model to send your answer back in little pieces. That's inference: using the model, not training it. And those first words make the app feel quick. I'm less impressed when the next word apparently needs a lunch break. Let's design the backend behind that stream and see how bounded memory and smaller chunks of work keep answers moving. Picture Sam getting an answer while Lee submits an 8,000-token document. Tokens are pieces of text, not necessarily whole words.

0:32 Suddenly Sam's answer pauses. Sam didn't submit that document, and the server hasn't crashed. How does Lee's work get to interrupt a different person's answer? So follow both requests on this map. A gateway bounds what arrives, a queue holds permitted waiting requests, and the shared engine budgets memory and computation before sending text back. That engine uses a GPU, a processor doing the model's numerical work. Both callers need it, even while Sam's answer is already appearing. Yep. We'll unpack the queue, scheduler and saved state, then return to this complete map.

1:07 We're designing our own service, not claiming access to a company's hidden infrastructure. And the fix has to serve the next word without pretending Lee's document disappears. They both still need that processor. Before choosing a schedule, test our functional requirements. Suppose Sam closes the app halfway through his answer. Has closing his socket actually canceled the work? No. Cancellation must reach Sam's request inside the engine, not just close a socket. Otherwise the GPU can finish an answer nobody will read.

1:38 And when Lee arrives, does accepting his text mean he enters the engine immediately? Not necessarily. Admit him, hold him under a bounded waiting policy, or reject him. Once admitted, his generated pieces come back in order. With limits on both prompt and answer lengths. The service can't promise useful work by accepting more than its memory and processor can handle. Now the non-functional requirements put bounds on memory, waiting work and stream delays. Starting an answer quickly and continuing it smoothly are separate promises.

2:10 We'll use one prepared text model that fits on one GPU. For our exercise, let's target a first generated piece within 2 seconds for 95% of admitted requests. What happens to Sam after that first piece? We separately target server-side gaps below 200 milliseconds for 99% of output intervals. Those are proposed load-test targets, not measured results. So a fast first piece can pass one target while a later pause breaks the other. I mean, averaging the whole response would bury that distinction. Exactly. Declare the hardware and mixture of input and output lengths, too. A target without a workload is a remarkably optimistic sentence.

2:48 And these clocks stop at our server's output. A congested phone network can still make a healthy engine look slow to its user. So the two clocks meet at that shared GPU. Processing incoming prompt text is prefill; producing a later piece using already-saved calculations is decode. Then walk me through Lee's arrival. Does the engine process that entire document before Sam gets another word? It can, under a schedule that allows a big prompt-processing step. While that step occupies the processor, Sam's next output has to wait.

3:22 Oh, that's why the opening words looked fine. Sam had already started before Lee's expensive work arrived; the initial response-time graph misses the interruption. Yep. And the scheduler can't just yank an already-executing GPU operation out halfway through because Sam's next word became urgent. We need smaller decision boundaries. And those execution boundaries let us change who's doing work. Suppose Sam finishes while Lee still has work to do, and another caller is waiting. With fixed membership, Sam's place can sit unused until Lee finishes.

3:54 The next caller waits even though part of that group has emptied out. Continuous batching lets us reconsider membership after each model step. Finished requests leave and eligible waiting work can join. The ORCA paper studied that iteration-level decision, linked in the description. So the group isn't locked for the entire answer. Once a step ends, can the waiting request use Sam's place? Yes. We can fill that empty place at the next step. But execution isn't the only limit; every active answer also occupies memory.

4:25 So that memory budget starts with the model's learned numbers, its weights, staying loaded. Growing request state gets a separate budget after weights and execution overhead. We've said decode reuses saved calculations. I used to picture only the text itself sitting in memory. What exactly gets kept alongside that text? The key-value cache keeps numbers already calculated for earlier tokens. Think of reusable working notes, not a saved finished sentence. Later steps use those numbers while deciding which earlier text matters next.

4:57 For example, Sam's prompt already needs those working notes. Each new token adds more. Lee's longer input needs room before he gets a word back. Ah, so two open requests needn't mean two equal-sized jobs. Counting connections tells us very little about how full that state memory is. But memory admission doesn't shorten a long prefill step. To keep Sam's output moving, break Lee's prompt processing into chunks and schedule ongoing answers between them. Let's count a toy schedule. Sam gets one position for output; Lee uses what's left.

5:30 Suppose a step permits 256 new token positions. Sam consumes one, leaving 255 for the prompt. So chunking gives us more places to make a scheduling decision. Sam advances again without waiting for Lee's whole document to finish. Right. Sarathi-Serve studied that interference. vLLM, an inference engine, also schedules ongoing decodes alongside smaller prompt chunks. Current systems aren't all stuck with the naive version. Hold on. I'm not sure that counting picture proves the timing promise. Can a token budget tell us how long each step takes?

6:04 No. They measure different things. The count limits admitted work in a step; the elapsed time still depends on the model, how much earlier text each request carries, the batch and the hardware. Then choosing the chunk size needs measurements, not a token-to-millisecond conversion. We'd test the mixtures that actually made Sam's stream stall. Exactly. Smaller chunks create more chances to schedule output, but also add overhead and can delay the first output for a new long request. A product engineer might say, “Just prioritize every answer that's already started.” That sounds fair to Sam, but how does Lee ever join?

6:44 We cap admitted concurrency and leave a measured share of execution for incoming work. Waiting requests also have an age limit, so they aren't silently promised service forever. Both clocks constrain the policy. Protecting current streams by leaving every new caller waiting would make one dashboard prettier without fixing the product. But scheduling time doesn't settle memory admission. PagedAttention stores those saved per-request calculations in blocks, so one answer needn't claim a single large uninterrupted region.

7:12 Like giving Sam's earlier calculations several small storage lockers, with a map saying which are his, instead of demanding one huge empty room. Right. It reduces wasted space, but it can't manufacture memory. Consider a room with six storage lockers: existing requests reserve five and the next needs two. Seven won't fit into six. That request waits or gets rejected under policy, even if the next execution step has spare time. If completed work releases two reservations, three remain and the new two fit.

7:45 Those are diagram units, not real GPU block sizes. Before Sam enters, we'd reserve state for his prompt and his maximum allowed answer. We may leave space unused, but we won't admit Lee by spending Sam's reservation twice. So Lee may wait outside the engine. But the gateway can't hold an unlimited stack of documents while he waits. Yeah. Lee is asking us to hold 8,000 tokens, far more text than a short question. Bound bytes, estimated token work and waiting age, not just the number of connections.

8:17 And cap each customer's share, so one customer's documents can't fill the whole waiting room before anyone else can join. Capacity doesn't expand because the API is polite. A replica can have fewer connections and still have no room for Lee's saved calculations. I'd try another compatible, ready replica with enough space and a bounded backlog. What if that other replica is running a different model version? Then Lee doesn't go there. The model and tokenizer, its text-to-token rules, must match the request, and the bundle has to be loaded before we call it ready.

8:52 But routing can't help when a caller abandons an admitted answer. The gateway sends cancellation to the engine, rather than simply forgetting the socket. I'd want to free its state right away. Otherwise canceled work keeps reducing the capacity everyone else can use. At a safe execution boundary, yes. But an operation already running may still refer to those blocks. The engine confirms it has stopped using them before we reuse them. Oh, giving the same memory to Lee too early could corrupt requests, not merely waste a few extra calculations.

9:24 The gateway can't declare the GPU finished. Exactly. We also cap buffered output when a client reads slowly. Eventually we cancel or terminate under policy instead of storing an unlimited answer behind that connection. And suppose the engine itself disappears halfway through Sam's reply. Can another replica just carry on from the next word? Not under our contract. The gateway reports an interrupted generation. Starting over may produce different text, so we don't secretly splice that answer onto Sam's old stream.

9:56 That explicit failure boundary keeps this about model serving, not durable conversation replay. But a serving engineer could still challenge our shared engine: “Why let long inputs near ongoing output at all?” Process Lee's input in one ready pool and generate his answer in another. Before the second pool can continue, Lee's key-value cache, those saved working notes, has to cross between them. That sounds cleaner than adjusting chunk sizes forever. I mean, what are we buying besides an extra box on the architecture picture?

10:27 We keep another model copy ready and spend resources moving that per-request data. Current vLLM documentation calls the split experimental and explicitly says it isn't a throughput improvement. So we should compare measured tail delays and resource costs before adopting it. It isn't a free upgrade just because the diagram has fewer crossing arrows. Yep. Start with the simpler shared design if it meets the workload's targets; split when the isolation benefit earns its transfer and capacity costs.

10:56 So for production debugging, consider a concrete case: Lee arrives while Sam's text is moving. Measure queue delay, startup and each later output gap. Yeah. Track free space for saved calculations, too, plus queued requests. Otherwise a scheduling delay looks like admission waiting. Keep prompt text out of ordinary telemetry; IDs and timings can carry the trace. A busy GPU can still be delivering a miserable stream. “100% utilized” is quite a trophy when nobody can finish reading a sentence. We should put the trophy next to the queue that's technically full of satisfied future customers.

11:31 Right. Report rejection and waiting outcomes too, or we could meet the latency target by serving almost nobody. Count useful completed service within the promised limits. Now follow Lee on that complete map again. The gateway and bounded queue can hold his document without handing it straight to the GPU. And free compute doesn't tell us whether he'll fit. Sam's saved calculations are already taking space. Exactly. The router needs compatible, ready capacity and a reservation for Lee. Once he fits, chunked prompt work leaves Sam opportunities to decode.

12:04 Then the GPU executes the chosen work and the dispatcher preserves output order. Completion or cancellation frees state only after the engine stops using it. Lee still costs time. But we can now trace Sam's pause to competing work and test both clocks, instead of treating streaming itself as the explanation. So in an interview I'd ask whether the caller waited to enter, waited for anything back, or lost progress after output began. Because those symptoms lead to different capacity and scheduling decisions.

12:35 I wouldn't start by drawing a bigger GPU fleet and hoping the distinction goes away. Yeah. First identify the scarce space and the long execution step, then show how your admission and scheduling choices address both. That's a design we can explain and test. Thanks for listening to Learning Podcasts.