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 requestsWaiting requestsGPUGPU1 in the batch
  • Waiting requests — healthy
  • GPU — healthy — 1 in the batch
Time for one step (the serving lab's numbers)
  • 1 in the batch (highlighted)28 ms
  • 2 in the batch31 ms
  • 3 in the batch34 ms
  • 4 in the batch37 ms
  • 5 in the batch40 ms
  • 6 in the batch43 ms
  • 7 in the batch46 ms
  • 8 in the batch49 ms
  1. One request alone. With one request, each step takes 28 ms and writes one token for it.
  2. 8 at once. With 8, a step takes 49 ms, far less than 8 separate steps of 28 ms.
  3. More work done. Together the 8 get about 163 tokens a second, against 36 for one alone.
  4. Each user slower. Each user's own speed drops, here from 36 to 20 tokens a second, since each step is longer.
  5. 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

  1. Each step writes one token for every request in the batch.
  2. A bigger batch makes each step a little longer.
  3. A bigger batch makes more tokens a second in total.
  4. But each user gets their own tokens more slowly.

Go deeper in the Serving a language model lab →

A 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 for every request still running.

Example. Illustrative: the lab's default GPU step takes 25 ms plus 3 ms for each request in the batch. A full batch of 8 takes about 49 ms a step, since 25 + 3 × 8 is 49.

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 .

In an interview

  • Explain the trade, bigger batches raise total output but slow each user, then pick a size from your targets.

Check yourself

A server raises its batch size from 1 to 8. What happens to each single user's tokens per second?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

A GPU step takes 25 ms plus 3 ms for each request in the batch. With 4 requests, how long is a step, and how fast is each user's stream?

Judge a design

A team runs every request in a batch of 1, to give each user the fastest stream. What does that choice cost?

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.

Open this lesson on its own page

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.

QueueQueueGPUGPU0 of 8,000 used
  • Queue — healthy
  • GPU — healthy — 0 of 8,000 used
GPU memory for saved work (8,000 tokens)
  • Used0 tokens
  • Free (highlighted)8,000 tokens
Running
not shown yet
Waiting
not shown yet
  1. Two limits. The serving lab's small example GPU holds 8,000 tokens of saved work, and its batch has 8 seats.
  2. Short requests. 12 requests arrive; each grows to 800 tokens: a 700-token prompt plus a 100-token answer.
  3. Seats run out first. Memory could hold 10 of them, but the batch has 8 seats, so 4 wait in the queue.
  4. Long prompts. At 4,000 tokens each, memory holds only 2, so 10 of the 12 requests wait.
  5. Or one is paused. Preemption: if running answers outgrow memory, the server pauses one and later redoes its prompt and answer so far.
  6. 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

  1. Saved work grows by one token at every step of the answer.
  2. When memory or the batch's seats run out, new requests wait.
  3. Long prompts mean far fewer requests run together.
  4. If answers outgrow memory, a server may pause one and redo its work later.

Go deeper in the Serving a language model lab →

Each request's saved work, the (see How a model writes), takes up memory on the GPU. A longer 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 until enough memory frees up.

Sometimes a running request is paused instead, called a . Its saved work is dropped to free memory for others. It must redo that work, its , once it starts again (see How a model writes).

Try it: the lab's long-prompts experiment shows requests getting paused this way.

Example. Illustrative: the lab's small example GPU has room for 8,000 tokens. A request with a 700-token prompt and a 100-token answer saves 800 tokens of work, so 10 of them fit. With a 3,500-token prompt and a 500-token answer, each saves 4,000, so only 2 fit.

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

Prompts grow from 700 to 3,500 tokens. What happens to how many requests fit in GPU memory?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

A GPU has room for 16,000 tokens of saved work. Each request has a 1,500-token prompt and a 500-token answer. How many fit?

Judge a design

With long prompts, requests queue while the batch still has empty seats. A team raises the batch size. Will that help?

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.

Open this lesson on its own page

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 requestsWaiting requestsGPU, 24 GBGPU, 24 GB16-bit parameters
  • Waiting requests — healthy
  • GPU, 24 GB — healthy — 16-bit parameters
How the 24 GB is shared (example model and GPU)
  • Model parameters (highlighted)14 GB
  • Left for saved work10 GB
  1. 16 bits each. At 16 bits, the 7 billion parameters fill 14 GB of the 24 GB GPU.
  2. Room for saved work. The 10 GB left holds 19,073 tokens of saved work: 9 requests of 2,000 tokens.
  3. Down to 8 bits. In 8 bits the parameters need 7 GB, so 16 requests fit at once.
  4. Down to 4 bits. In 4 bits they need 3.5 GB, and 19 requests fit.
  5. Check the answers. Rounding every number can change some answers, so the smaller model must pass your test set first.
  6. 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

  1. Parameters stored in 16 bits take 2 bytes each.
  2. Halving the bits halves the memory the parameters need.
  3. Freed memory holds more saved work, so more requests fit.
  4. Fewer bits can change answers, so test before switching.

A model's 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.

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 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.

Example. Example: a 24 GB GPU and a 7-billion-parameter model like Llama 2 7B, whose saved work takes 0.5 MB a token. At 16 bits there is room for 9 requests of 2,000 tokens. At 8 bits 16 fit, and at 4 bits 19 fit, before the server's own working memory. Newer models need several times less per token.

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

Moving parameters from 16 bits to 8 bits frees memory. What must you check before switching?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

A 13-billion-parameter model is stored in 16 bits. How much memory do its parameters need, and how much at 4 bits?

Judge a design

A team moves to 4 bits, checks that general chat still passes, and ships. Its app also does sums. What did it miss?

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.

Open this lesson on its own page

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.

QueueQueueGPU batchGPU batch4 of 4 seats used
  • Queue — healthy
  • GPU batch — healthy — 4 of 4 seats used
The 4 seats of the batch, step by step

Static batching

Seat 1
  1. Seat 1: A, running, steps 1 to 12.
Seat 2
  1. Seat 2: B, running, steps 1 to 3.
Seat 3
  1. Seat 3: C, running, steps 1 to 4.
Seat 4
  1. Seat 4: D, running, steps 1 to 2.
Queue

    Continuous batching

    Seat 1
    1. Seat 1: A, running, steps 1 to 12.
    Seat 2
    1. Seat 2: B, running, steps 1 to 3.
    Seat 3
    1. Seat 3: C, running, steps 1 to 4.
    Seat 4
    1. Seat 4: D, running, steps 1 to 2.
    Queue
      E waits, static
      not shown yet
      E waits, continuous
      not shown yet
      1. A batch of 4. 4 requests start together; the longest, A, needs 12 steps, and D needs only 2.
      2. A short question. Request E arrives after step 2, and its answer needs only 2 steps.
      3. Static: wait for all. With static batching, E waits until step 12, while the seats of B, C and D sit idle.
      4. Continuous: join early. With continuous batching, D leaves after step 2, and E takes its seat on the very next step.
      5. The difference. E waits 10 steps, about 370 ms, with static batching, and none with continuous.
      6. 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

      1. Static batching starts a new batch only when every request in it is done.
      2. A short request can wait behind one long answer.
      3. Continuous batching lets requests leave and join at every step.
      4. So a short request starts as soon as any seat frees up.

      Go deeper in the Serving a language model lab →

      A model server can batch requests two ways. Static 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.

      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.

      Example. A short question arrives right after a batch starts. Under static batching, it waits until the long answer already in that batch finishes. Under continuous batching, it joins on the very next step instead.

      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 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

      A short request arrives right after a long batch starts. With continuous batching, when does it start?

      Two harder questions

      The first uses the idea on a new case. The second asks you to judge a design.

      Apply it

      Under static batching, one batch holds answers of 20, 50 and 400 tokens. After how many steps can a new batch start?

      Judge a design

      A static batching server reserves memory for the longest possible answer for every request, but most answers are short. What does that waste?

      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.

      Open this lesson on its own page

      5. Two numbers to watch

      The picture

      Lab · AI engineering

      Serving a language model

      Requests fill a GPU batch and its memory; watch batching, long prompts and batch size decide how fast each user gets an answer.

      Try it

      Lab time 3.0 s
      Users, 1/sUsers1/sQueue, waiting for a batch slotQueuewaiting for a batch slot0 waitingGPU, 7 / 8 in the batchGPU7 / 8 in the batch82% of memory used
      • Users — 1/s — healthy
      • Queue — waiting for a batch slot — healthy — 0 waiting
      • GPU — 7 / 8 in the batch — healthy — 82% of memory used
      • Request
      • Response

      A token is a small piece of text the model reads or writes.

      A GPU is the chip that runs the model's calculations.

      Time to first token,
      247 ms
      Time to first token,
      296 ms
      Healthy:
      Tokens a second
      79/s
      Each user's tokens a second
      25.5/s
      Queue length
      0
      Paused to free memory (preemptions)
      0
      Wasted slot steps
      0
      Seats held by finished requests, static batching only

      Numbers add up:9 arrived = 1 completed + 7 running + 0 waiting + 1 in flightThe lab checks this sum as it runs, so a counting mistake would show here.

      Controls

      Traffic
      1/s

      New requests bring a 2,000–4,000-token prompt instead of 200–1,200.

      Batching
      Batching mode

      Requests join and leave the batch at every step. Memory grows one token at a time.

      8 requests
      8,000 tokens

      In the batch

      • r170 of 83 tokens
      • r334 of 103 tokens
      • r434 of 41 tokens
      • r533 of 104 tokens
      • r619 of 25 tokens
      • r75 of 119 tokens
      • r80 of 43 tokens

      Memory (KV cache)

      The KV cache is the memory where the model keeps its saved work on every token so far.

      • Used6,556 of 8,000 tokens (82%)
      Time to first token (, recent)
      0 ms125 ms250 ms375 ms500 ms1s2s3s
      Show data table
      Timep95
      1s189 ms
      1s212 ms
      2s296 ms
      2s296 ms
      3s296 ms
      3s296 ms
      Tokens a second
      0501001502001s2s3s
      Show data table
      TimeTokens/s
      1s16
      1s54
      2s56
      2s110
      3s126
      3s110
      Queue and batch
      WaitingIn the batch
      02.557.5101s2s3s
      Show data table
      TimeWaitingIn the batch
      1s01
      1s02
      2s05
      2s05
      3s05
      3s07
      What this model simplifies
      • The step times, prefill speed and memory size are made up to show the shape. Real numbers vary by model and hardware by ten times or more.
      • Every request's answer length is fixed when it arrives. A real model stops when it writes a special end token.
      • Paused requests go back to the queue and redo their work. Real servers can also move saved work to slower memory instead.
      • One GPU and one model. Real services spread a model over several GPUs and run many copies behind a load balancer.

      Open the full lab →

      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.

      Example. Illustrative: a chat product might aim for the first token within about 1 second, and at least 15 tokens a second after that. These are example targets, not fixed rules.

      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

      Why should a chat product set targets for both time to first token and tokens per second?

      Two harder questions

      The first uses the idea on a new case. The second asks you to judge a design.

      Apply it

      Targets are a first token within 1 s and at least 15 tokens a second. One batch size gives 0.6 s and 12 tokens a second. Does it meet both?

      Judge a design

      A team tunes its server only for total tokens per second across the GPU. What can go wrong?

      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.

      Open this lesson on its own page