Module 11 · Specialisation: running model servers · 5 lessons
Serving a model: batches, memory and queues
Run a model server, fill it with requests, and watch batches, memory and queues decide how fast each user gets an answer.
Before you start
From this track: Modules 0 to 10. Each module uses only ideas from the ones before it.
Builds on: Load balancing and health checks, Vertical and horizontal scaling
Already know these? Start with the first lesson below.
1. A GPU works on a batch
The picture
One step, many requests
A GPU writes one token for every request in its batch at each step, so more requests share each step.
Scene 1 of 4 · One request alone
With one request, each step takes 28 ms and writes one token for it.
- Waiting requests — healthy
- GPU — healthy — 1 in the batch
- One request alone. With one request, each step takes 28 ms and writes one token for it.
- 8 at once. With 8, a step takes 49 ms, far less than 8 separate steps of 28 ms.
- More work done. Together the 8 get about 163 tokens a second, against 36 for one alone.
- Each user slower. Each user's own speed drops, here from 36 to 20 tokens a second, since each step is longer.
- Now you try. Set how many requests share the batch. For example: 4 in the batch: 37 ms a step, 108 tokens a second in all, 27 for each user. More requests share each step, so the total rises while each user's own speed drops.
Scene 1 of 4: One request alone. With one request, each step takes 28 ms and writes one token for it.
Key points
- Each step writes one token for every request in the batch.
- A bigger batch makes each step a little longer.
- A bigger batch makes more tokens a second in total.
- But each user gets their own tokens more slowly.
A (A graphics processing unit: a chip that does many simple sums at the same time. Language models run on GPUs because they need huge numbers of sums.) is the chip that runs a model's sums (see How a model writes). It does not have to work on one request at a time. A model server gives it a whole group of requests to work on together, in one step. This group is called a batch.
A bigger batch uses the GPU better, since more requests share the same step instead of each waiting for its own. But each step still takes a little longer, because the GPU does a little more work for every extra request in it.
This happens at every step of the batch, each time it writes one more (A piece of text that a language model reads or writes, often part of a word. Prices and limits are counted in tokens.) for every request still running.
In system design: A server batches requests to use the GPU well. Bigger batches raise the work done each step, but each request waits slightly longer for its own step to finish.
Where it goes wrong
- A batch that is too big makes each user's tokens slow. A batch that is too small wastes the GPU.
- One very long request holds its seat for a long time, which hurts most with (Running a fixed group of requests together until the slowest one finishes, before any new request can start.).
In an interview
- Explain the trade, bigger batches raise total output but slow each user, then pick a size from your targets.
Check yourself
Two harder questions
The first uses the idea on a new case. The second asks you to judge a design.
Apply it
Judge a design
Sources and further reading (2)
First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- Research paperOrca: A Distributed Serving System for Transformer-Based Generative Models (2022)Yu et al., USENIX OSDIWhy batching requests keeps a GPU busy, and how batches were built before and after this paper.
- Official docsvLLM documentationvLLM projectAn open-source model server that batches requests and manages GPU memory.
2. Memory decides how many fit
The picture
Memory sets the batch size
Every running request keeps its saved work in GPU memory, so request size decides how many run at once.
Scene 1 of 5 · Two limits
The serving lab's small example GPU holds 8,000 tokens of saved work, and its batch has 8 seats.
- Queue — healthy
- GPU — healthy — 0 of 8,000 used
- Running
- not shown yet
- Waiting
- not shown yet
- Two limits. The serving lab's small example GPU holds 8,000 tokens of saved work, and its batch has 8 seats.
- Short requests. 12 requests arrive; each grows to 800 tokens: a 700-token prompt plus a 100-token answer.
- Seats run out first. Memory could hold 10 of them, but the batch has 8 seats, so 4 wait in the queue.
- Long prompts. At 4,000 tokens each, memory holds only 2, so 10 of the 12 requests wait.
- Or one is paused. Preemption: if running answers outgrow memory, the server pauses one and later redoes its prompt and answer so far.
- Now you try. Set the saved tokens per request, and see how many of 12 can run. For example: 5 run at once, and 7 of 12 wait in the queue. Memory is the limit here: bigger requests leave room for fewer at once.
Scene 1 of 5: Two limits. The serving lab's small example GPU holds 8,000 tokens of saved work, and its batch has 8 seats.
Key points
- Saved work grows by one token at every step of the answer.
- When memory or the batch's seats run out, new requests wait.
- Long prompts mean far fewer requests run together.
- If answers outgrow memory, a server may pause one and redo its work later.
Each request's saved work, the (The memory where a model keeps its work on every token so far, so each new token does not redo it. It grows by one token per step.) (see How a model writes), takes up memory on the GPU. A longer (Everything sent to a language model in one request: instructions, context and the question.) or a longer answer so far means more saved work, so it takes more memory.
The GPU has a fixed amount of this memory. Once it is full, a new request cannot start: it waits in the (A waiting line for work. One part adds messages, and another takes them off and handles them later, at its own pace.) until enough memory frees up.
Sometimes a running request is paused instead, called a (Pausing a running request and freeing its memory so others can continue. It must redo its saved work when it returns.). Its saved work is dropped to free memory for others. It must redo that work, its (The first stage of an answer, where the model reads the whole prompt in one go and saves its work for later steps.), once it starts again (see How a model writes).
Try it: the lab's long-prompts experiment shows requests getting paused this way.
In system design: Memory sets how many requests run together. Long prompts and answers use more of it, so fewer requests fit, and the rest wait or get paused.
Where it goes wrong
- With long prompts, memory fills before the batch does, so requests queue while seats sit empty.
- Frequent pauses to free memory waste work, since each paused request reads its prompt again.
In an interview
- Work out how many requests fit from the memory each token needs and how long the requests are.
Check yourself
Two harder questions
The first uses the idea on a new case. The second asks you to judge a design.
Apply it
Judge a design
Sources and further reading (2)
First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- Research paperEfficient Memory Management for Large Language Model Serving with PagedAttention (2023)Kwon et al., SOSPHow the KV cache limits how many requests fit on one GPU, and how paging fits more.
- Official docsCachingHugging Face Transformers docsHow the KV cache grows with every token in the prompt and the answer.
3. Quantization: smaller numbers, more room
The picture
Fewer bits, more requests
Storing each parameter in fewer bits shrinks the model in GPU memory, leaving more room for saved work and so more requests at once.
Scene 1 of 5 · 16 bits each
At 16 bits, the 7 billion parameters fill 14 GB of the 24 GB GPU.
- Waiting requests — healthy
- GPU, 24 GB — healthy — 16-bit parameters
- 16 bits each. At 16 bits, the 7 billion parameters fill 14 GB of the 24 GB GPU.
- Room for saved work. The 10 GB left holds 19,073 tokens of saved work: 9 requests of 2,000 tokens.
- Down to 8 bits. In 8 bits the parameters need 7 GB, so 16 requests fit at once.
- Down to 4 bits. In 4 bits they need 3.5 GB, and 19 requests fit.
- Check the answers. Rounding every number can change some answers, so the smaller model must pass your test set first.
- Now you try. Pick how many bits each parameter gets, and see how many requests fit. For example: Parameters: 7 GB. Left: 17 GB, enough for 16 requests at once. Smaller numbers free memory for saved work, as long as answers stay good enough.
Scene 1 of 5: 16 bits each. At 16 bits, the 7 billion parameters fill 14 GB of the 24 GB GPU.
Key points
- Parameters stored in 16 bits take 2 bytes each.
- Halving the bits halves the memory the parameters need.
- Freed memory holds more saved work, so more requests fit.
- Fewer bits can change answers, so test before switching.
A model's (One of the numbers inside a model that training adjusts. Large language models have billions of them.) are numbers, often stored in 16 bits, which is 2 bytes each. A model with 7 billion parameters then needs 14 GB of GPU memory before it serves anyone.
(Storing a model's numbers in fewer bits, such as 8 or 4 instead of 16, so it needs less memory.) stores each number in fewer bits, such as 8 or 4. The parameters then take a half or a quarter of the memory, and the freed memory holds more saved work.
Each (The second stage of an answer, where the model writes one new token per step, using the work it saved.) step reads the parameters from memory, so smaller numbers can make steps faster too. How much depends on the hardware and the software.
Rounding the numbers loses some detail, and answers can get worse. How much depends on the model and the method, so test the smaller model on your test set before switching.
In system design: Quantization is a capacity choice: more requests per GPU, traded against answer quality that you measure.
Where it goes wrong
- Fewer bits can hurt some tasks, such as sums or less common languages, more than others. Test the tasks you serve.
- The speed gain depends on the hardware and the software, so measure it on your own set-up.
In an interview
- Say what quantization frees, how many more requests fit, and how you would check answer quality.
Check yourself
Two harder questions
The first uses the idea on a new case. The second asks you to judge a design.
Apply it
Judge a design
Sources and further reading (3)
First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- Research paperLLM.int8(): 8-bit Matrix Multiplication for Transformers at Scale (2022)Dettmers et al., NeurIPSRunning large models in 8 bits with little loss in quality.
- Research paperGPTQ: Accurate Post-Training Quantization for Generative Pre-trained Transformers (2022)Frantar et al., ICLR 2023Shrinking a trained model to 4 or 3 bits per number and measuring the quality cost.
- Official docsQuantizationHugging Face Transformers docsThe common methods side by side, with what each needs to run.
4. Static and continuous batching
The picture
Wait for all, or join at once
Static batching makes a new request wait for the whole batch; continuous batching gives it the first free seat.
Scene 1 of 5 · A batch of 4
4 requests start together; the longest, A, needs 12 steps, and D needs only 2.
- Queue — healthy
- GPU batch — healthy — 4 of 4 seats used
Static batching
- Seat 1: A, running, steps 1 to 12.
- Seat 2: B, running, steps 1 to 3.
- Seat 3: C, running, steps 1 to 4.
- Seat 4: D, running, steps 1 to 2.
Continuous batching
- Seat 1: A, running, steps 1 to 12.
- Seat 2: B, running, steps 1 to 3.
- Seat 3: C, running, steps 1 to 4.
- Seat 4: D, running, steps 1 to 2.
- E waits, static
- not shown yet
- E waits, continuous
- not shown yet
- A batch of 4. 4 requests start together; the longest, A, needs 12 steps, and D needs only 2.
- A short question. Request E arrives after step 2, and its answer needs only 2 steps.
- Static: wait for all. With static batching, E waits until step 12, while the seats of B, C and D sit idle.
- Continuous: join early. With continuous batching, D leaves after step 2, and E takes its seat on the very next step.
- The difference. E waits 10 steps, about 370 ms, with static batching, and none with continuous.
- Now you try. Change how many steps the long answer A needs. For example: Static: E waits 18 steps. Continuous: E starts on the very next step. Static batching ties E to the slowest answer in the batch; continuous batching needs only one free seat.
Scene 1 of 5: A batch of 4. 4 requests start together; the longest, A, needs 12 steps, and D needs only 2.
Key points
- Static batching starts a new batch only when every request in it is done.
- A short request can wait behind one long answer.
- Continuous batching lets requests leave and join at every step.
- So a short request starts as soon as any seat frees up.
A model server can batch requests two ways. Static (Grouping several items, such as messages or requests, and handling them together instead of one at a time.) runs a fixed group together. A new batch starts only once every request in that group has finished, even one that finished long ago.
Static batching also reserves memory for the longest answer any request might give, since it cannot see the real length in advance. That memory sits reserved even for a request that finishes early.
(Letting requests join and leave a batch at every step, instead of waiting for the whole batch to finish.) lets requests join and leave the batch at every step instead, not only at the end. A finished request leaves right away, and a new one can join on the very next step.
Try it: switch batching modes in the lab to see the difference.
In system design: Static batching is simpler but slower for a short request stuck behind a long one. Continuous batching adds complexity but keeps the batch full and responsive.
Where it goes wrong
- With static batching, short requests wait behind long ones, so (How long a user waits from sending a request until the first piece of the answer appears.) jumps.
- Continuous batching still runs out of memory when requests are long. Then requests wait or get paused.
In an interview
- Explain why continuous batching keeps the GPU busy and short requests fast.
Check yourself
Two harder questions
The first uses the idea on a new case. The second asks you to judge a design.
Apply it
Judge a design
Sources and further reading (2)
First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- Research paperOrca: A Distributed Serving System for Transformer-Based Generative Models (2022)Yu et al., USENIX OSDIThe paper behind continuous batching: requests join and leave the batch at every step.
- Research paperEfficient Memory Management for Large Language Model Serving with PagedAttention (2023)Kwon et al., SOSPWhy reserving memory for the longest possible answer wastes space, and how vLLM avoids it.
5. Two numbers to watch
The picture
Two numbers describe how a model server feels to use. Time to first token (see How a model writes) is the wait for the first piece of the answer: the queue plus reading the prompt.
Tokens per second is the speed of every token after that first one. It says how fast the rest of the answer streams in, once the model has started writing.
A bigger batch raises the GPU's total tokens produced every second, across every request together. But since each step takes a little longer with more requests in it, each single user's own tokens per second falls.
Because of this trade-off, a design sets a target for both numbers, not for one alone. A fast start with a slow stream can still feel disappointing once the answer begins.
In system design: Set a target for both numbers, and watch them together. A larger batch trades one for the other, so pick a batch size that meets both targets.
Where it goes wrong
- Tuning only for total output can make each user's stream too slow.
- Tuning only for a fast first token, with small batches, can need many more GPUs for the same traffic.
In an interview
- Give a target for both numbers, such as a first token within 1 second and 15 tokens a second. Then pick a batch size that meets both.
Check yourself
Two harder questions
The first uses the idea on a new case. The second asks you to judge a design.
Apply it
Judge a design
Sources and further reading (2)
First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- Research paperDistServe: Disaggregating Prefill and Decoding for Goodput-optimized LLM Serving (2024)Zhong et al., USENIX OSDITime to first token and time per output token as two separate targets a server must meet.
- Official docsMetricsNVIDIA NIM LLM benchmarking guideExact definitions of time to first token, inter-token latency and throughput.