LLM Inference: KV Cache, Batching & Scheduling

KV cache mechanics, memory management, and paging (PagedAttention). Continuous batching (Orca), chunked prefill, prefix caching, and latency vs. throughput tradeoffs in LLM serving.

Overview

Inference is a different problem from training

Training is compute-bound: large batches, weight reuse, a backward pass. Production inference balances two competing goals, throughput (tokens per second across all users) and latency (time to first token and inter-token delay per user). The KV cache is the central data structure, and every serving optimization revolves around managing it.

A serving system is built around that cache. Incoming requests reach a scheduler that runs continuous batching. The scheduler moves each request through two phases. Prefill processes the prompt; it is compute-heavy, and it can be chunked. Decode then generates one token per step, and it is bandwidth-bound. Prefill writes the KV cache, which lives as paged blocks in HBM, and every decode step both reads and extends it. A prefix cache holds the KV of shared prompts and feeds it into the same cache.

The rest of this chapter takes the techniques one at a time, and each solves one problem.

  • The KV cache stops the model from recomputing keys and values for every prior token.
  • PagedAttention removes the fragmentation that pre-allocated KV memory causes.
  • Continuous batching removes idle slots and head-of-line blocking in static batches.
  • Chunked prefill keeps long prompts from stalling other requests' decode.
  • Prefix caching avoids recomputing a shared prompt for every request.

Interview questions

  • Explain PagedAttention in detail. How does it manage KV cache memory and what are its limits?
  • How do you architect an LLM serving system to handle both latency-sensitive and throughput-sensitive workloads simultaneously?

Sign up free to read the full topic

More AI Infrastructure topics

Start free on Cariara · Coding problems · System design studio