Home Blog Spatial Lab Disciplines Agentic Tools
Learn • AI Academy
IP Network Infrastructure About Connect

Continuous Batching & PagedAttention: Production LLM Serving Architecture

The production serving stack: eliminating static padding bubbles via iteration-level scheduling, virtual memory block tables for dynamic KV cache allocation (vLLM), and prefill-decode disaggregation.

Foundational Knowledge & Simpler Primers
Need a simpler explanation or feeling stuck?

To build solid intuition for this module, review these foundational primers:

Unsure of mathematical notation or technical terms on this page? Our 57-term AI Glossary breaks down every concept with plain-English analogies and rigorous engineering specs.
Open AI Glossary (57 Terms)

1. Theoretical Motivation & Foundations

Naïve batching in LLM serving creates severe resource waste: because generated sequence lengths vary widely, static batching must pad all sequences to the length of the longest request, leaving GPUs starved of useful computation (creating massive 'padding bubbles'). Orca and vLLM revolutionized serving by introducing iteration-level scheduling (continuous batching) and PagedAttention. With iteration-level scheduling, the serving engine re-evaluates the batch at every generation step: completed requests are immediately returned to the client, while new pending requests enter the batch on the very next iteration. Complementing this, PagedAttention adapts virtual memory paging principles from operating systems: instead of allocating contiguous memory for the entire maximum sequence length upfront (which wastes 60% to 80% of VRAM through internal and external fragmentation), PagedAttention manages KV caches in small, fixed-size physical memory blocks dynamically mapped via block tables. This eliminates near-all fragmentation, increasing effective serving concurrency by 2x to 4x.

2. Mathematical Formulations & Derivations

The governing analytical formulations and proof frameworks for this module:

Static Batching Padding Waste: W_pad = ∑_{i=1}^B (L_max - L_i) / (B × L_max) ∈ [40%, 75%] Continuous Batching GPU Utilization: U_iter = ∑_{t=1}^T B_active(t) / (T × B_max) ≥ 92% PagedAttention Memory Overhead Derivation: Let block size be B_tokens (e.g. 16 tokens). Memory per token for GQA (H_kv heads, D head dimension, P bytes): Bytes_per_token = 2 × H_kv × D × P Internal Fragmentation per Sequence < (B_tokens - 1) / Sequence_Length For sequence length 2048 and block size 16: Fragmentation < 16 / 2048 = 0.78% (down from 70%+!)

3. From-Scratch Reference Implementation

Executable, production-tested reference code without magic libraries:

# Iteration-Level Continuous Batch Scheduler Simulation class Request: def __init__(self, req_id: int, prompt_len: int, max_gen: int): self.id = req_id self.prompt_len = prompt_len self.max_gen = max_gen self.tokens_generated = 0 self.status = 'PENDING' class ContinuousBatchEngine: def __init__(self, max_batch_size: int): self.max_batch_size = max_batch_size self.running = [] self.completed = [] def step(self, pending_queue): # Fill empty slots immediately while len(self.running) < self.max_batch_size and pending_queue: new_req = pending_queue.pop(0) new_req.status = 'RUNNING' self.running.append(new_req) # Execute one decode iteration for all active requests finished = [] for req in self.running: req.tokens_generated += 1 if req.tokens_generated >= req.max_gen: req.status = 'FINISHED' finished.append(req) for f in finished: self.running.remove(f) self.completed.append(f) return len(self.running), len(finished) engine = ContinuousBatchEngine(max_batch_size=2) queue = [Request(1, 100, 2), Request(2, 50, 4), Request(3, 80, 1)] for i in range(4): active, fin = engine.step(queue) print(f'Step {i+1}: Active={active}, Finished={fin}, Pending={len(queue)}')

4. Systems Complexity & Memory Footprint

Continuous batching and PagedAttention represent the bedrock of modern LLM inference systems (vLLM, TGI, TensorRT-LLM, SGLang). By treating memory dynamically and scheduling iterations rather than requests, production servers maintain near-100% compute density and slash memory waste to under 1%.

5. Canonical Literature & Primary Research

Original research papers and foundational texts recommended for advanced study:

  1. Yu, G. I., et al. (2022). Orca: A Distributed Serving System for Transformer-Based Generative Models. OSDI.
  2. Kwon, W., et al. (2023). Efficient Memory Management for Large Language Model Serving with PagedAttention. SOSP.
  3. Zheng, L., et al. (2024). SGLang: Efficient Execution of Structured Language Model Programs. arXiv:2312.07104.
Next Page for Further Learning
Mastered this concept? Keep advancing

Explore the natural continuations in the curriculum: