How KV Cache Speeds Up LLMs for Faster AI Models on GPUs
LLMs often slow down under heavy traffic due to inefficient GPU memory management during inference. This overview explains how KV cache and Paged Attention, implemented in VLLM, optimize memory usage across prefill and decode phases, significantly boosting LLM throughput, reducing latency, and improving GPU utilization through advanced context handling and specific tuning techniques like prefix caching and speculative decoding.
Large Language Models (LLMs) often face significant performance degradation under high user traffic, with latency climbing and GPU throughput plummeting. This issue typically stems not from the model itself, but from inefficient memory utilization during inference, particularly in how the model stores and retrieves its evolving context. This discussion introduces two key mechanisms—KV Cache and Paged Attention—originating from the open-source inference engine VLLM, which are critical for optimizing LLM inference at scale.
Understanding LLM Inference Phases
LLM inference comprises two distinct phases:
- Prefill Phase: This is the initial processing of the input prompt. It is highly compute-bound, requiring the model to run the input through every transformer layer to construct a mathematical representation before generating any output. This phase accounts for the initial lag before the first token appears.
- Decode Phase: Following prefill, this phase involves generating tokens one by one. It is memory-bound, as the system repeatedly accesses GPU memory to retrieve the context built so far. The efficiency of this phase heavily relies on how the Key-Value (KV) cache is managed. Fragmented or inefficiently utilized memory here directly translates to observable latency.
The Role and Challenges of KV Cache
In autoregressive generation, a transformer model computes a Query (Q), Key (K), and Value (V) for each token. Without caching, generating a response of, for example, 1,000 tokens necessitates recomputing the K and V for all 999 preceding tokens at each step for the 1,000th token.
The KV cache addresses this by storing the Key and Value matrices from previous steps, preventing redundant recomputations. Each new token only needs to compute its own Q, K, and V, then attends over the cached history. While a memory-for-compute trade-off, it’s invaluable for long sequences.
However, traditional KV cache implementations present significant memory bottlenecks:
- Fixed Contiguous Allocation: Naive systems pre-allocate a fixed, contiguous block of GPU memory for each request, based on the maximum possible output length (e.g., 2048 tokens). This leads to substantial waste if users generate shorter responses. Research indicates 60-80% of KV cache memory can be wasted in such setups.
- Internal Fragmentation: Reserving memory for the maximum sequence length even when only a fraction is used.
- External Fragmentation: Varying request lengths create non-contiguous gaps, making it difficult to allocate memory for new requests even if sufficient total memory is available.
- Redundant Duplication: System prompts, which are often shared across many requests, are stored separately for each concurrent request, leading to unnecessary memory consumption.
Paged Attention: Optimizing GPU Memory for KV Cache
Paged Attention leverages an insight from operating systems’ virtual memory paging. Instead of contiguous blocks, it breaks the KV cache into small, fixed-size pages (defaulting to 16 tokens). These pages can be stored non-contiguously anywhere in GPU memory and are allocated on demand. A lightweight block table maps logical page addresses (what the model sees) to their actual physical addresses in VRAM. This approach dramatically improves GPU memory utilization by eliminating internal and external fragmentation, making the 35% of VRAM typically reserved for KV cache much more efficient.
Key Deployment Optimizations from VLLM
To maximize throughput and minimize latency, several VLLM-specific configurations can be tuned:
- Tune GPU Memory Utilization: Controls the fraction of remaining VRAM dedicated to the KV cache (default 0.9). For stable workloads, pushing this to
0.95can accommodate more concurrent requests. Conversely, reducing it to0.8can prevent Out-Of-Memory (OOM) errors during load bursts. Benchmarking with tools likeguideLLMis recommended. - Enable Prefix Caching: Paged Attention allows hashing each KV block by its token sequence. Requests sharing a common system prompt can then point to the same physical memory, with VLLM computing and storing it only once. This significantly improves “Time to First Token” (TTFT) by entirely skipping shared prefill for subsequent requests, commonly achieving 75-95% hit rates in RAG pipelines, multi-turn chats, and coding agents.
- Enable Chunked Prefill: For throughput-heavy scenarios, default VLLM completes prefill before starting decode, which can cause output stuttering with long prompts. Chunked prefill prioritizes batching decode requests first, then fills any remaining compute budget with prefilled chunks. This has shown up to a 50% throughput improvement in production deployments. It is often used in conjunction with setting
max-num-batch-tokensto a value greater than 2048. - Speculative Decoding (Bonus for Latency): For latency-sensitive applications, this feature leverages idle GPU compute during decode. A smaller “draft” model proposes a series of output tokens, which a larger model then verifies in a single forward pass. If correct, tokens are forwarded; if not, corrections are made. The output quality is mathematically identical to running the large model alone. While gains diminish at very high concurrency (where the GPU is already busy), it’s highly beneficial when interactive latency is paramount over raw throughput. VLLM also offers a zero-cost n-gram speculator via
--speculative-model n-gramfor structured or repetitive outputs.