# Static vs Continuous Batching

> What static and continuous batching are, why static batching leaves the GPU idle, and the trade-offs, explained simply for your AI interview.

**In short:** Static batching sends a group of requests to the GPU and waits for the slowest one before taking new work. Continuous batching swaps in a new request as soon as one finishes, so the GPU stays busy and short replies don't wait.

AI and LLM interview question Q314, Inference, performance and cost. Intermediate, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/static-vs-continuous-batching

## Say it in 30 seconds

Batching means running several requests on the GPU at once, because one at a time leaves it mostly idle. With static batching, the server waits until every request in the group finishes. One long reply holds up the short ones, and the slots sit empty. With continuous batching, the server checks after every token. A finished request leaves, and a waiting one takes its place straight away. In a made-up run I did, the four slots were in use 83 percent of the time instead of 55. The trade-off is that a long prompt can slow the replies already running, so I'd measure latency as well as throughput.

## Say it in 2 minutes

A GPU is very fast at doing many small jobs at once, so an AI server groups requests together. This is called batching. Requests don't arrive together, and they don't all need the same length of reply. A short status check might need 12 tokens, and a claim summary might need 90. A token is a small piece of text, often a word or part of one, and the model writes one token at a time.

With static batching, the server fills a batch, runs it step by step, and starts the next batch only when every request in the current one has finished. A short reply finishes early, but its slot stays reserved, so its slot does no useful work until the long one ends. The vLLM paper describes the same problem: padding requests to equal length wastes computation and memory.

Continuous batching checks after every step. A request that has finished leaves, and a waiting request takes its slot at once. In a made-up run with 8 requests and 4 slots, static batching kept the slots busy 55 percent of the time over 160 steps. Continuous batching kept them busy 83 percent of the time over 106 steps, with the same 351 tokens of work.

The price is latency. A new long prompt needs a burst of prefill work, which can stall replies already streaming. So I'd check both throughput and latency, and I'd ask whether chunked prefill, which reads long prompts in slices, is needed. I'd start with continuous batching and measure.

## What is batching, in plain words?

Batching means giving the GPU several requests at once. A GPU is built to do many small jobs side by side, so when it handles one request at a time, most of its capacity sits idle.

A GPU is the chip that runs the model. Throughput is how much work it finishes per second. Latency is how long one user waits for a reply.

Think of a clinic that books patients in groups of four. In a static clinic, the group is called in together, and nobody new sits down until the slowest of the four has left, so chairs stand empty. In a continuous clinic, a new patient sits down the moment a chair frees up. An AI claims assistant has the same problem. One customer types "Where is my refund for claim 88412 and when will it land?" and another asks for a full policy summary. Both may share one batch, and the short question should not wait for the summary.

A model writes its reply one token at a time. Each request goes through two phases. Prefill reads the whole prompt in one go. Decode writes the reply, one token per step.

- **Batch:** Several requests the GPU works on in the same step.
- **Token:** A small piece of text, often a word or part of one.
- **Prefill:** Reading the whole prompt in one go, before the first reply token.
- **Decode:** Writing the reply, one token at a time.

## How does static batching work, and where does it waste time?

The server fills a batch with a fixed set of requests and sends it to the GPU. Every request in the batch moves forward together, one token per step. The batch only ends when its longest reply ends, and the next batch waits until then.

The waste comes from different lengths. A short reply is finished early, but its slot stays reserved, so its slot does no useful work. Engineers also pad short requests with filler so they line up with the longest one, and that filler still costs work.

*Diagram, One batch of four, two ways: Top: static batching keeps a finished slot empty until the whole batch ends. Bottom: continuous batching refills it at the next step. The percentages come from the made-up run later in this answer.*
1. The short requests finish early but keep their slots.
2. The GPU slots sit empty until the whole batch ends.
3. Each step, finished requests leave the batch.
4. A waiting request fills the free slot at once.

## What does continuous batching change?

Continuous batching re-forms the batch at every step. The scheduler, the part of the server that decides who runs next, does three jobs each step.

1. **Drop the finished:** A request that has written its last token leaves, and its memory is freed.
2. **Refill the free slot:** A waiting request takes the empty place, so no slot sits idle.
3. **Run one more token:** Every running request writes its next token in the same GPU step.

> "Continuous batching maximizes GPU utilization by dynamically rescheduling the batch at every generation step." (Hugging Face's transformers docs)
>
> "A straightforward batching technique would pad the inputs and outputs of the requests to equalize their lengths, wasting GPU computation and memory." (The vLLM paper, 2023)

The idea has several names. The vLLM paper uses the term iteration-level scheduling, from Orca (Yu et al., 2022), for the same per-step idea. NVIDIA's TensorRT-LLM lists in-flight batching as a feature. vLLM itself lists continuous batching among its speed features.

Triton, NVIDIA's inference server, has a dynamic batcher that can hold requests for a set time so others can join the batch. That timed wait is a different mechanism from refilling a slot at every step. Interviewers may mix the two up, so say which one you mean.

## Does continuous batching really help?

Here is a made-up run. A server has 4 slots, and 8 requests arrive with replies of 12, 90, 15, 80, 10, 60, 14 and 70 tokens. The script below counts the steps and how many slots do useful work.

- **Static: 55% of slots busy.** 160 steps, because each batch of four waits for its longest reply.
- **Continuous: 83% of slots busy.** 106 steps, because a finished slot is refilled at once.

Both runs do the same 351 tokens of work. The difference is the empty slots.

```python
# A made-up GPU server with 4 slots in the batch. Each number is how many tokens a
# request still has to write. Real servers also juggle memory; this shows only the scheduling.
REQUESTS = [12, 90, 15, 80, 10, 60, 14, 70]
SLOTS = 4

def static(queue):
    steps = busy = 0
    for i in range(0, len(queue), SLOTS):
        batch = queue[i:i + SLOTS]
        steps += max(batch)  # the whole batch waits for its longest request
        busy += sum(batch)
    return steps, busy

def continuous(queue):
    steps = busy = 0
    todo, running = list(queue), []
    while todo or running:
        while todo and len(running) < SLOTS:  # a free slot is refilled at once
            running.append(todo.pop(0))
        steps += 1
        busy += len(running)
        running = [left - 1 for left in running if left > 1]
    return steps, busy

for name, run in [("static", static), ("continuous", continuous)]:
    steps, busy = run(REQUESTS)
    total = steps * SLOTS
    print(f"{name:<11} steps={steps:<4} slots busy={busy}/{total} ({busy / total:.0%})")
```

Output:

```text
static      steps=160  slots busy=351/640 (55%)
continuous  steps=106  slots busy=351/424 (83%)
```

## What are the trade-offs?

Continuous batching has a cost. Each running request keeps a KV cache, the stored memory of its tokens so far. That cache grows and shrinks as requests come and go, so the server has to track it closely.

The bigger cost is latency. A new long prompt needs a big prefill burst, and that can stall replies already streaming. Prefill is slow per step but saturates the GPU's compute (its math units), while decode is fast per step but uses little of it. The first token is the first piece of the reply the user sees. The Sarathi-Serve paper, from 2024, puts the clash this way:

> "batching multiple requests leads to an interleaving of prefill and decode iterations which makes it challenging to achieve both high throughput and low latency." (The Sarathi-Serve paper, 2024)

*Diagram, A long prompt, and the fix: A long prompt can stall replies already running. Chunked prefill reads it in slices, so the running replies keep moving. The chart shows the order of the fix, not a measured result.*
1. A big read takes a burst of GPU work, so replies slow down.
2. The fix: the long prompt is read in slices, not in one go.
3. Each slice lets the running replies keep moving at every step.

Sarathi-Serve introduces chunked prefills, which splits a prefill request into near equal sized chunks. Those chunks let new requests join a batch without pausing ongoing decodes. The cost is plain: chunking spreads the long prompt over several steps, so its own first token can come later.

> **Tip:** Asked how to keep a chat fast under load? Name the trade-off first: throughput is how much work the GPU finishes, and latency is how long one user waits. Then say you would measure both.

### How would you serve a chat app and an overnight report job?

Two workloads share one GPU fleet. The chat app needs replies to start quickly, so it gets continuous batching with chunked prefill. The overnight job only cares about total work finished by morning, so a big batch with long waits is fine.

- **Live chat: Latency first.** Continuous batching, with long prompts read in chunks so replies keep moving.
- **Overnight job: Throughput first.** Large batches that keep the GPU full, and nobody is waiting on one reply.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] Batching runs several requests on the GPU together.
- [ ] Static batching waits for the longest request, so slots sit empty.
- [ ] Continuous batching refills a slot at every step.
- [ ] The trade-off: memory for each running request, and slower first tokens when long prompts arrive.
- [ ] Throughput versus latency, and how chunked prefill helps.
- [ ] Names the tools: vLLM, TensorRT-LLM's in-flight batching, Triton's dynamic batcher (a timed wait, not per-step refill).

**Common traps**

- Trap: "Static batching is faster, because the whole group finishes together." Better: Finishing together only helps the slowest request. Continuous batching keeps idle slots filled, so more work is done per second.
- Trap: "Continuous batching has no downside." Better: It adds memory for each running request, and it can slow replies when a long prompt arrives.
- Trap: "Just buy a bigger GPU." Better: A bigger GPU still wastes slots when short and long requests share a static batch.

## Can you spot the waste?

**Spot the waste:** A batch holds three 12-token tag requests and one 900-token summary. Which setup leaves the most GPU slots empty?

A. Continuous batching, which refills each slot as it frees up
B. Static batching, which holds three free slots until the summary ends
C. Continuous batching with a bigger batch size
D. A shorter system prompt on every request

**Answer:** B. The three short requests finish early, but static batching keeps their slots empty until the 900-token summary is done. Refilling each slot as it frees up is the fix.

**Quick check:** A chat app gets new long prompts all day, and users say replies freeze. What is the best first change?

A. Switch to static batching with bigger batches
B. Keep continuous batching and read long prompts in chunks
C. Remove the system prompt
D. Turn off streaming so users wait for the whole reply

**Answer:** B. Chunked prefill spreads a long prompt's reading across steps, so replies already streaming keep moving. Bigger static batches make waits longer, and removing the system prompt or turning off streaming does not fix the freeze.

## Can you answer these from memory?

**Flash cards**

- **What is batching?** Running several requests on the GPU together, so it stays busy.
- **Static batching in one line?** Start a batch, and wait for its longest request before taking new work.
- **Continuous batching in one line?** Check every step, and refill each slot the moment a request finishes.
- **Where does static batching waste time?** Finished requests hold their slots, and short ones are padded to match long ones.
- **What is the main trade-off?** Memory for each running request, and slower replies when long prompts arrive.
- **What is chunked prefill?** Reading a long prompt in slices, so running replies keep moving.

## Likely follow-up questions

- Q315 Why stream tokens? https://designervenkat.online/interview/ai/why-stream-tokens
- Q316 TTFT vs tokens/sec vs total latency? https://designervenkat.online/interview/ai/ttft-vs-tokens-per-second-vs-latency
- Q317 The latency vs throughput trade-off? (full answer coming)
- Q319 What is PagedAttention / vLLM? (full answer coming)
- Q320 Serving engines (vLLM, SGLang, TensorRT-LLM, llama.cpp)? (full answer coming)

## Learn it in depth

- [Inference Engineering](https://designervenkat.online/learn/ai-engineering/inference-engineering)
