← Back to AI Infrastructure Mind Map
🖥️ AI InfrastructureID: continuous-batching

Continuous Batching

连续批处理
🎯Core Definition
Continuous Batching (iteration-level scheduling, introduced by Orca) does not wait for an entire batch to finish before serving the next one; at the end of every decoding iteration (one token step) the batch membership is reshuffled: requests that finished (naturally or by max-length truncation) are evicted, waiting requests that completed prefill are inserted, and each in-flight request keeps its own KV-cache progress. Contrast static (request-level) batching, which pins a batch until all requests complete — the slowest request (e.g. 40 tokens) blocks the fastest, whose slot idles the rest of the time → low GPU utilization. With continuous batching every step tries to keep the batch full, raising throughput roughly 2-3× (Orca reports about 2.5×; the win is biggest for mixed long/short workloads, since short requests finish fast, free slots, and new ones fill in). Key implementation points: ① a new request must prefill first (compute the whole prompt's KV in one pass) before joining decode iterations; how prefill and decode are co-scheduled decides TTFT; ② preemption: under memory pressure the engine can preempt some requests' decode slots (common policies: new-prefill admission or priority-based eviction); evicted requests' KV is swapped to CPU or discarded to be restarted/recomputed later; ③ sequences of unequal length in the batch need padding or chunked attention to avoid wasted compute; ④ it composes naturally with PagedAttention, whose paged KV allows slots to be allocated/freed block-by-block as requests come and go, fragmentation-free.
💡Use Cases
the default scheduler in every mainstream engine (vLLM/SGLang/TensorRT-LLM); biggest gains for online chat/API traffic (random arrival, mixed lengths); interview staples: "static vs continuous batching", "preemption and swap", "why GPU utilization goes up".
⚡Key Problems Solved
static batching wastes GPU cycles from two sources — the slowest request in a batch blocks everyone (bubble), and waits between batch arrivals. Continuous batching refines the scheduling granularity from request to iteration, so every step has finished requests freeing slots and new requests filling them, driving compute density toward 100%; on the same hardware this is roughly 2-3× more throughput, with more controllable TTFT since new requests no longer wait for a whole batch to drain.
🎯Core Interview Exam Points
Fundamentals · Production Scenarios · Deep Dive
Core Def
Explain the core definition, underlying mechanism/math, and key principles of Continuous Batching.
Use Cases
In production engineering, what are the primary use cases for Continuous Batching and what core pain points does it solve?
🔥 High-Frequency Deep-Dive Questions
1
Static vs continuous batching: draw a timeline with two requests (10 tokens vs 40 tokens) showing static batching's idle time vs continuous batching's refill, and why throughput rises 2-3×.
2
Iteration-level scheduling: what the engine does at the end of each decode iteration (evict finished, insert prefilled newcomers, dynamic membership); why a request must prefill before joining decode.
3
Preemption and swap: who gets preempted under memory pressure (new prefill vs running decode), where evicted KV goes (CPU swap / discard-restart), and each option's latency cost.
4
Why workloads with many short requests benefit most: fast completion, fast slot turnover; relate to the padding/chunked-attention cost of unequal-length sequences in a batch.
5
How continuous batching composes with PagedAttention and PD disaggregation: paged KV makes dynamic membership fragmentation-free; how scheduling works in prefill nodes vs decode nodes.
Updated 2026-08-12
🎯
Verify Mastery: Practice Exam Questions for "Continuous Batching"
Take pitfall multiple-choice questions with derivation options; mistakes are automatically logged.
🚀 Start Card Practice ➔
← Previous CardInference Engines vLLM/SGLang/TensorRT-LLMNext Card →Prompt Caching & RadixAttention

🔗 More AI Infrastructure Knowledge Cards

引用出处: TalentMe 450+ 工业级 AI 架构考点知识库 (Tech Vault) · 遵循 CC-BY-4.0 知识共享协议
URL: https://talentme.airsota.com/resources/tech/roadmap/mindmap/AI_Infra/card/continuous-batching
中文速览与面试要点

连续批处理 (Continuous Batching)

定义: Continuous Batching(迭代级调度,Orca 提出)= 不是等整批请求全部生成完再换下一批,而是在每个解码迭代(token 步)结束时就动态调整批成员: 已生成完(含最大长度截断)的请求移出批、等待中的新请求完成 Prefill 后插入批、批内各请求各自维护 KV 缓存进度。对比静态批 (request-level batching): 静态批把一批请求绑死到全部完成,最慢请求(如 40 个 token)拖住最快请求,快请求的 slot 在其余时间空转 → GPU 利用率低;连续批下每步 batch 都尽量装满,吞吐提升可达 2-3×(Orca 论文报告约 2.5×;混合长短请求、短请求占比高的负载提升最大——短请求完成快,slot 释放再插入,填充更密)。关键实现点: ① 新请求必须先做 Prefill(一次性算出整个 prompt 的 KV)才能加入 decode 迭代,prefill 与 decode 的混合调度决定了 TTFT;② 抢占 (preemption): 显存不足时,引擎可抢占部分请求的 decode slot(常见策略: 新 prefill 插队或按优先级驱逐),被抢占请求的 KV 缓存 swap 到 CPU 显存或直接丢弃重启,恢复时重新 prefill/续算;③ 批内序列长度参差 → 需要 padding 或分块注意力,避免 GPU 空算;④ 与 PagedAttention 天然配合: 分页 KV 让动态进出批的请求按需分配/释放块,无碎片。 使用场景: 所有主流推理引擎(vLLM/SGLang/TensorRT-LLM)的默认调度;在线对话/API 服务(请求到达无规律、长短混合)收益最大;面试高频“静态批 vs 连续批”“抢占与 swap”“为什么能提升 GPU 利用率”。 解决的问题: 静态批的 GPU 空转来自两点——批内最慢请求阻塞整体(bubble),以及请求间到达间隙的批量等待。连续批把调度粒度从「请求」细化到「迭代」,每步都能有完成的请求腾位、有新请求补充,GPU 计算密度趋近 100%;数值上同等硬件吞吐提升约 2-3×,同时 TTFT 更可控(新请求不用等整批结束)。

核心要点

  • 静态批 vs 连续批的差异: 用两个具体请求(一个 10 token、一个 40 token)画时间线,说明静态批的空转与连续批的填充逻辑,以及吞吐为何可提升 2-3×。
  • 迭代级调度机制: 每个解码迭代结束时引擎做什么(完成移出/新请求 prefill 后插入/批成员动态变化);为什么新请求必须先 prefill 才能 decode。
  • 抢占 (preemption) 与 swap: 显存不足时抢占谁(新 prefill vs 旧 decode)、被抢占请求的 KV 放哪里(CPU swap / 丢弃重启),各自延迟代价是什么。
  • 为什么短请求占比高的负载收益最大: 短请求完成快、slot 周转快;结合批内序列长度参差时的 padding/分块注意力代价说明取舍。
  • 连续批如何与 PagedAttention、PD 分离配合: 分页 KV 使动态进出批无碎片;prefill/decode 分离后连续批分别在两阶段如何调度。