KV Cache, Batching and Little's Law
The memory equation that decides how many sequences fit, and why static batching wastes a logarithmically growing fraction of the GPU.
The memory equation
For a model with layers, key/value width and bytes per stored element, the cache holds
so a batch of sequences of length requires bytes.
Assumptions
- One key vector and one value vector are stored per layer per token — hence the leading 2.
- , where is the number of key/value heads and the head dimension. Multi-head attention has and so ; grouped-query attention shrinks ; multi-query attention takes .
- No cross-sequence sharing. Prefix sharing changes this and is discussed below.
With total device memory , weights and overhead , the number of concurrent sequences at context length is bounded by
Throughput and concurrency
For a system in which requests arrive, spend time inside, and depart, let be the long-run arrival rate, the long-run mean time a request spends in the system, and the long-run mean number of requests in the system. If these limits exist and are finite, then
Assumptions
Stationarity and finiteness of the three limits. No assumption on the arrival process, the service-time distribution, the number of servers, or the scheduling discipline. This is what makes the law usable: it holds for a system as messy as an LLM server.
Key idea
For a server, read Little's law as
Concurrency is not a free parameter you tune independently of latency and throughput — fixing any two fixes the third. Since is capped by memory, so is the product : you may serve many slow requests or few fast ones, and the trade is arithmetic, not engineering skill.
Why static batching wastes the machine
Static batching groups requests, runs them together, and returns the batch when all have finished. Since sequences stop at different lengths, the short ones keep occupying a slot — and consuming decode steps — long after they are done.
Let output lengths be i.i.d. exponential with mean . Define utilisation as the expected fraction of computed token-slots that carry useful work,
Then
At this is ; at , ; at , . Utilisation gets worse as you batch harder — the opposite of what batching is supposed to buy.
Key idea
Continuous (in-flight) batching removes this loss by construction: a finished sequence is evicted and a queued one admitted at the next step, so slots never idle. The gain is not an optimisation of a constant factor — it is the removal of an penalty that grows precisely as you scale up.
What this does not say
The exponential assumption is doing real work. It is a reasonable first model for "stop tokens arrive at a roughly constant hazard rate", and empirical length distributions are heavier-tailed than exponential, which makes the gap worse, not better. But the exact constant is a property of the exponential model, not a measured quantity. The qualitative claim — utilisation decays with under any non-degenerate length distribution — follows from and needs no distributional assumption.
Paging the cache
Allocating a contiguous buffer sized for the maximum possible context wastes everything between the actual length and that maximum. Block allocation fixes this.
If the cache is allocated in blocks holding tokens each, then a sequence of length wastes
independent of and of any maximum context length.
So total waste across sequences is under slots regardless of context length, versus for contiguous preallocation — a quantity that grows with the declared maximum rather than the used length. Choosing trades fragmentation (small ) against per-block indirection overhead in the attention kernel (large ); values in the tens are typical.
References
- PaperJ. D. C. Little (1961). A Proof for the Queuing Formula L = λWThe original distribution-free proof.
- PaperW. Kwon et al. (2023). Efficient Memory Management for Large Language Model Serving with PagedAttentionIntroduces block-paged KV allocation; the fragmentation bound below is stated independently.
- PaperJ. Ainslie et al. (2023). GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints
These works are cited as sources to read, not reproduced. All exposition, examples and problems on this site are written independently; where an argument is standard, the citation points to a canonical treatment.
Results stated on this page
- PropositionKV cache sizeproof includedThe cache costs 2·L·d_kv·b bytes per token per sequence, linear in context length and independent of batch-wide sharing.
- CorollaryMaximum concurrent sequencesproof includedAvailable memory after weights, divided by per-sequence cache, bounds the batch size — usually well below the batch needed to be compute-bound.
- TheoremLittle's lawproof includedIn any stable queueing system the mean number in system equals arrival rate times mean time in system, with no distributional assumptions.
- PropositionStatic batching utilisation decays like 1/log Bproof includedWith exponentially distributed output lengths, a static batch of size B wastes a fraction 1 − 1/H_B of its decode steps.
- PropositionBlock paging bounds internal fragmentationproof includedAllocating the cache in blocks of k tokens wastes fewer than k tokens per sequence, independent of context length.