Continuous Batching vs Static Batching: LLM Serving Trade-offs
In this article
Continuous batching and static batching are two strategies for grouping inference requests in LLM serving systems. Continuous batching dynamically adds and removes requests from a batch as they finish, while static batching processes a fixed group from start to finish. This article explains how each affects throughput, latency, and memory, and helps you decide which to use.
in t | Total tokens | tokens | 1000 | 1 in b | Batch size | requests | 8 | 1 in m | Model memory (GB) | GB | 16 | 1 out mem_per_req | Memory per request (GB) | GB | m / b | 2 out mem_active | Active memory with static batching (GB) | GB | m | 2
Formula
The calculator estimates memory per request in a static batch. It divides the model's memory footprint by the batch size. The formula is:
Memory per request = Model memory / Batch size
Where Model memory is the GPU memory needed to hold the model weights and KV cache (in GB), and Batch size is the number of requests processed together. The second output shows that with static batching, all requests share the same memory allocation for the entire duration.
Worked Example
Suppose you have a model that uses 16 GB of GPU memory and you set a static batch size of 8. Using the formula:
Memory per request = 16 GB / 8 = 2 GB
So each request effectively uses 2 GB of the model's memory. The active memory (the total allocated) remains 16 GB because the whole batch is held in memory until all requests finish.
Background: Why Batching Matters in LLM Serving
Large language models (LLMs) generate tokens one at a time, and each token requires a forward pass through the model. Without batching, the GPU is idle during the memory-bound parts of generation, wasting compute. Batching groups multiple requests so the GPU processes them together, improving utilization and throughput. Static batching is the traditional approach, where a fixed set of requests is processed until all are complete. Continuous batching, also called iteration-level scheduling, is a newer technique that treats each decoding step as a scheduling unit.
How Static Batching Works
In static batching, the server collects requests until it reaches a batch size (or a timeout), then runs the entire batch through the model. The batch is fixed: no new requests are added, and no request leaves until every sequence in the batch finishes. This is simple to implement and predictable, but it has a major inefficiency: the batch is only as fast as its slowest member. If one request generates 500 tokens and another generates 50, the shorter request waits for the longer one, wasting GPU cycles and increasing latency for the shorter request.
How Continuous Batching Works
Continuous batching (also known as dynamic batching or iteration-level scheduling) checks after every decoding step whether any sequence has finished. When a request completes, it is removed from the batch, and a new request can be added immediately. This keeps the GPU busy with a full batch of active sequences, rather than waiting for stragglers. The scheduler maintains a queue of pending requests and selects which ones to add based on memory availability and fairness policies.
Throughput, Latency, and Memory Trade-offs
The key trade-offs between the two approaches are:
- Throughput: Continuous batching typically increases throughput by 2-3x compared to static batching, because it eliminates the idle time caused by stragglers. The GPU is always processing a full batch of active requests.
- Latency: Static batching can have higher tail latency because a short request may wait for a long one. Continuous batching reduces this by letting short requests finish early. However, continuous batching can introduce slight overhead per step due to scheduling logic, but the overall latency is usually lower.
- Memory: Static batching allocates memory for the maximum batch size upfront. Continuous batching allocates memory dynamically as requests are added, but it must reserve memory for the KV cache of each active sequence. If the model has a large memory footprint, continuous batching can support larger effective batch sizes by only keeping active sequences in memory.
How to Choose Between the Two
Choosing depends on your workload and requirements:
- Use static batching if: you have a small number of requests with similar lengths, or you need a simple implementation with predictable memory usage.
- Use continuous batching if: you have variable request lengths, high traffic, or you want to maximize GPU utilization and throughput.
- Consider hybrid approaches: Some systems use static batching for prefill (processing the input prompt) and continuous batching for decoding (generating tokens). This balances latency and throughput.
Common Problems and Misconceptions
One common misconception is that continuous batching always reduces memory usage. In fact, it can use more memory per request because it keeps the KV cache for all active sequences, whereas static batching might reuse memory more efficiently if the batch is homogeneous. Another issue is that continuous batching requires careful scheduling to avoid priority inversion, where a long-running request starves shorter ones. Static batching avoids this by treating all requests equally, but at the cost of efficiency.
Cost and Hardware Considerations
The choice also affects cost. Continuous batching can reduce the number of GPUs needed for a given throughput, lowering hardware costs. However, it may require more engineering effort to implement and tune. Static batching is easier to deploy on simple inference servers but may require more GPUs to meet the same throughput. For memory-bound models, continuous batching can help fit larger batches into a single GPU, but you must monitor memory fragmentation.
Frequently Asked Questions
Q: Does continuous batching always improve latency?
Not always. For a single request, there is no batching, so both methods have similar latency. For multiple requests, continuous batching generally reduces tail latency because short requests don't wait for long ones. However, the scheduling overhead can add a few milliseconds per step, which is negligible compared to the time saved.
Q: Can I use continuous batching with any LLM?
Most modern serving frameworks like vLLM and TensorRT-LLM support continuous batching. It works with any transformer-based LLM, but you need to ensure your framework implements it correctly. Some models with unusual attention mechanisms may not be compatible.
Q: How does batching affect the KV cache?
The KV cache stores the key and value vectors for each token in the sequence. In static batching, the cache is allocated for the maximum sequence length for all requests. In continuous batching, the cache is allocated per active sequence and grows as the sequence grows, which can be more memory-efficient if sequences vary in length.
Q: What is prefill and decode in this context?
Prefill is the initial processing of the input prompt, which is compute-heavy. Decode is the token-by-token generation, which is memory-bound. Static batching processes prefill and decode together for the whole batch. Continuous batching can treat them separately, allowing prefill to be batched efficiently and decode to be scheduled dynamically.
Q: Is there a risk of out-of-memory errors with continuous batching?
Yes, because the scheduler adds new requests as others finish, it can run out of memory if the total KV cache exceeds GPU memory. Frameworks use memory management techniques like paging (as in vLLM) to reduce fragmentation, but you must still set a maximum batch size or memory limit.
Q: Can I mix static and continuous batching?
Yes, some systems use static batching for the prefill phase and continuous batching for the decode phase. This is common in production systems to balance throughput and latency. The prefill is compute-bound, so a fixed batch is fine, while decode benefits from dynamic scheduling.