KV cache interview questions
KV cache interview questions with model answers: why the cache exists, the size formula with worked numbers, GQA and MLA, KV cache quantisation, eviction and offloading, and how it limits batch size.
The KV cache is where GPU memory goes once the weights are loaded, so it decides how many requests you can batch and how long a context you can serve. Interviewers want to see you derive its size from the model config, not quote a number.
Question 1
What is the KV cache and why do we need it?
Model answer
In self-attention each new token's query attends to the keys and values of every earlier token. Without a cache, generating token would recompute the keys and values for all previous tokens in every layer, so generating tokens would cost work proportional to forward passes over the prefix.
The KV cache stores each token's key and value vectors for every layer the first time they are computed. A decode step then only computes the key, value and query for the one new token, appends the new key and value to the cache, and attends over the cached ones. That turns per-step work from "reprocess the whole sequence" into "process one token plus read the cache".
The trade is memory for compute: the cache grows linearly with sequence length and with the number of concurrent sequences, and reading it adds to the bytes each decode step must move.
Likely follow-up questions
- Why do we cache keys and values but not queries?
- Is the KV cache needed during training? Why or why not?
- What exactly has to be recomputed if a request is preempted and its cache is thrown away?
Question 2
How do you calculate the KV cache size for a model? Work through an example.
Model answer
Per token, each layer stores one key and one value vector per KV head:
Multiply by the sequence length and the number of concurrent sequences for the total.
Llama 3 8B has 32 layers, 8 KV heads (grouped-query attention) and a head dimension of 128. In BF16:
An 8,192-token sequence therefore needs 1 GiB, and 32 such sequences need 32 GiB, twice the 16 GB of weights.
Llama 3 70B has 80 layers, 8 KV heads and head dimension 128, so 320 KiB per token, or 40 GiB for a single 128K-token context.
A good answer finishes with the consequence: on an 80 GB GPU, after weights and activation workspace, what is left for KV cache divided by bytes per token is the total number of tokens you can hold across all requests, and that caps concurrency.
Likely follow-up questions
- How would the 8B number change without grouped-query attention, with 32 KV heads?
- How much does FP8 KV cache buy you, and what does it cost?
- With tensor parallelism across 4 GPUs, how is the KV cache split?
Question 3
How do GQA, MQA and MLA reduce KV cache size?
Model answer
- Multi-head attention (MHA) keeps one key and value head per query head.
- Multi-query attention (MQA) shares a single key and value head across all query heads. The cache shrinks by the number of heads, but quality can drop.
- Grouped-query attention (GQA) is the middle ground: query heads are split into groups and each group shares one KV head. Llama 3 uses 8 KV heads for 32 or 64 query heads, a 4 to 8 times saving with little quality loss.
- Multi-head latent attention (MLA), used by DeepSeek-V2 and V3, caches a small compressed latent vector per token per layer (512 dimensions plus a 64-dimensional rotary part in DeepSeek-V3) instead of full keys and values. The keys and values are reconstructed from it, and the up-projection can be absorbed into the query and output projections, so the cache is many times smaller than MHA at similar quality.
All of these cut the bytes read per decode step as well as the memory held, which raises the batch size you can fit and speeds up long-context decode.
Likely follow-up questions
- Why does GQA hurt quality less than MQA?
- What is the catch with MLA for tensor parallelism, given there is only one latent per token?
- Can you convert an MHA checkpoint to GQA after training? How?
Question 4
Should you quantise the KV cache? What are the trade-offs?
Model answer
Quantising the KV cache to FP8 or INT8 halves its memory compared with BF16, and INT4 variants quarter it. Benefits:
- Roughly twice as many tokens fit, so you can run larger batches or longer contexts.
- Each decode step reads fewer bytes, which helps most at long context where cache reads rival weight reads.
Risks:
- Keys tend to have outlier channels, so naive per-tensor scaling hurts accuracy more for keys than for values. Per-channel or per-head scales help.
- Errors are small per step but the model attends over thousands of quantised entries, so long-context retrieval tasks are the place to test, not short benchmarks.
- You need kernels that read the quantised format directly; otherwise you pay to dequantise.
In practice FP8 KV cache with calibrated scales is close to lossless for most models and is a common default on Hopper-class GPUs. Lower bit widths need evaluation on your own long-context workload.
Likely follow-up questions
- How would you evaluate whether KV cache quantisation hurt your product?
- Why might you quantise values more aggressively than keys?
- Does KV cache quantisation help prefill speed?
Question 5
What do you do when the KV cache runs out of memory under load?
Model answer
When the cache is full the scheduler cannot admit new requests and may not have room for running requests to grow. The options, roughly in the order engines apply them:
- Stop admitting new requests and queue them, which raises TTFT but protects running ones.
- Preempt a running request (usually the most recent), freeing its blocks. Later it is either recomputed (re-run prefill over its prompt plus generated tokens, cheap thanks to prefill's efficiency) or swapped back from CPU memory.
- Offload cold cache blocks to CPU RAM or SSD and bring them back on demand, which is useful for prefix caches shared across turns.
- Reduce demand: cap the maximum context or output length, quantise the cache, or use sliding-window attention where the model supports it.
At the capacity planning level, the answer is more memory per replica (tensor parallelism, bigger GPUs) or more replicas, sized from the bytes-per-token formula times the expected concurrent tokens.
Likely follow-up questions
- When is recomputation cheaper than swapping to CPU memory?
- Which metric in vLLM or SGLang tells you preemption is happening?
- How do eviction policies for a prefix cache differ from preemption of running requests?
The lesson behind these answers · 12 min · free
VRAM math: will this model fit?
Worked numbers, step-through diagrams and a quiz, so you can derive the answer in the interview instead of reciting it.
Read the lesson →- KV cache layout and sampling inside an engine · Engine Core16 min · Pro
- Caching: prefix caching, semantic caching and KV offload · Performance Techniques14 min · Pro
- KV-cache quantization · Models & Quantization20 min · Pro
- Attention kernels for serving: MLA, split-KV decoding and FlashAttention 3 and 4 · Bottlenecks and Hardware18 min · Pro
- Long-context serving: KV memory, prefill cost, context parallelism and cheaper attention · Performance Techniques17 min · Pro
Articles on this topic
- FlashAttention explained simply: same maths, far less memory traffic (8 min read)
- MHA vs GQA vs MQA vs MLA: how attention design decides your KV cache (8 min read)
- PagedAttention explained: virtual memory for the KV cache (7 min read)
- Tensor vs pipeline vs data vs expert parallelism for LLM inference (7 min read)
Related interview questions
- PagedAttention interview questions (5 questions)
- Prefill vs decode interview questions (5 questions)
- FlashAttention interview questions (4 questions)
- Batching and continuous batching interview questions (5 questions)