Skip to main content
How LLM serving worksLesson 3 of 9

How the KV Cache Fills Up

The KV cache holds the computed results for every token the model has already read or written. It lets the model keep writing quickly, but it fills accelerator memory fast as conversations grow and concurrent requests increase. In serving, this cache usually decides "how many users can we handle at once".

KV cache usage0%
New compute per token—
Concurrent requests1
One acceleratorRequest Achat 1Request Bsame promptModel computenext tokenModel weightsfixed sizeKV cache areamost of the restQueuewaitschat 1
RequestResponse / reportAppears in a later step

What you will learn

  • Without a KV cache, everything is recomputed every time
  • The cache grows with conversation length × concurrent requests
  • Splitting it into blocks reduces wasted memory
  • Shared prefixes can be reused (prefix caching)
  • When it is full, new requests wait or get preempted

Running it on Kubernetes​

  • GPU memory usage from nvidia-smi or the DCGM exporter looks high as soon as vLLM starts. vLLM claims the fraction of memory set by --gpu-memory-utilization (long 0.9 by default; recent releases pick it automatically when unset) and uses what remains after the weights for the KV cache. --kv-cache-memory-bytes sets the KV cache size directly.
  • Real headroom is the KV cache usage metric on vLLM’s /metrics (vllm:kv_cache_usage_perc, vllm:gpu_cache_usage_perc in older versions). If waiting requests (vllm:num_requests_waiting) and preemptions (vllm:num_preemptions_total) rise along with it, the cache is too small.
  • Levers when the cache runs short: --max-model-len (maximum length of one request), --max-num-seqs (concurrent requests), replica count, or accelerators with more memory. If an HPA scales replicas, base it on engine metrics such as waiting requests or KV cache usage rather than GPU utilization.
  • Prefix caching pays off most when requests with the same prefix reach the same replica; see Which Replica Gets the Request (Inference Routing). Moving overflow cache to CPU memory or disk is covered in Offloading Overflowing KV Cache.

Try it yourself​

kubectl exec <pod> -- nvidia-smi --query-gpu=memory.used,memory.total --format=csv # e.g. NVIDIA: looks full from the start (step 4)
curl -s http://<engine>:8000/metrics | grep -E 'vllm:(kv|gpu)_cache_usage_perc' # e.g. vLLM: real headroom
curl -s http://<engine>:8000/metrics | grep -E 'vllm:(num_requests_waiting|num_preemptions)' # Cache full: queue and preemptions (step 8)
curl -s http://<engine>:8000/metrics | grep vllm:prefix_cache # Prefix cache queries and hits (step 7)
vllm serve <model> --gpu-memory-utilization 0.9 --max-model-len 8192 --max-num-seqs 64 --enable-prefix-caching # Levers

Check yourself

  1. Right after vLLM starts, nvidia-smi shows GPU memory about 90% used with no traffic. What does this mean?
  2. Requests A and B start with the same system prompt. What does prefix caching let the engine do for B?
  3. KV cache usage stays near full, and waiting requests and preemptions both rise. What does that indicate?

Further reading: vLLM documentation · PagedAttention paper · Gateway API Inference Extension