← Back to AI Infrastructure Mind Map
🖥️ AI InfrastructureID: prompt-caching-radix

Prompt Caching & RadixAttention

Prompt Caching/RadixAttention
🎯Core Definition
Prompt caching reuses the KV computed for identical prompt prefixes, skipping repeated prefill computation. Typical reusable prefixes: system prompts, few-shot exemplars, early turns of multi-turn conversations, and shared instruction blocks across tenants — often 50-90% of a request's tokens, needlessly recomputed every time. RadixAttention (SGLang's core): organizes the KV blocks of all live requests into a radix tree where each node is a shared token prefix (root = empty; a path from root to node is a token sequence). A new request walks the tree with longest-prefix matching; matched nodes reuse their KV directly, and only the unmatched suffix is prefilled (compute ∝ unmatched tokens). The tree splits/merges dynamically as requests start and end: shared prefixes stay shared, divergent branches split. Effects: with a cache hit, TTFT drops 80%+ (if 90% of tokens hit and prefill dominates TTFT, TTFT ≈ 1/10 of the original); multi-turn chats only compute the incremental turns; shared system prompts slash per-request cost across tenants. Eviction: tree nodes carry reference counts and last-access timestamps; when memory is tight, least-recently-used branches are evicted (like OS page replacement), and a later hit requires recomputing prefill. vs vLLM's automatic prefix caching: vLLM hashes fixed-size token blocks (n-gram block hashing) — simple and effective; RadixAttention reuses arbitrary-length longest common prefixes precisely, with finer granularity and bigger wins for long shared prefixes.
💡Use Cases
conversational services with long system prompts, agentic multi-turn tool calls (each turn resends the full context), RAG with shared instruction templates, multi-tenant SaaS; interview favorites: "why does TTFT drop 80%+", "how the radix tree matches prefixes", "eviction policies".
⚡Key Problems Solved
prefill is compute-bound, and repeated prefill of long prompts wastes GPU compute and inflates TTFT; prefix KV reuse zeroes out the repeated work — at 80-90% hit rates the prefill compute demand drops by an order of magnitude and TTFT by 80%+, sharply reducing per-token cost in multi-turn/multi-tenant settings. It is one of the most direct cost levers of the long-context era.
🎯Core Interview Exam Points
Fundamentals · Production Scenarios · Deep Dive
Core Def
Explain the core definition, underlying mechanism/math, and key principles of Prompt Caching & RadixAttention.
Use Cases
In production engineering, what are the primary use cases for Prompt Caching & RadixAttention and what core pain points does it solve?
🔥 High-Frequency Deep-Dive Questions
1
Radix-tree prefix reuse: how a new request does longest-prefix matching; how hit prefixes vs unmatched suffixes are handled; how the tree splits/merges as requests end.
2
Why TTFT drops 80%+: prefill compute is proportional to unmatched tokens; convert hit rate and prefill share into numbers (prefill is 90% of TTFT, 90% hits → TTFT down ~81%).
3
Eviction policies: how LRU / reference counting work on the radix tree (branch-wise eviction); the cost of evicting hot prefixes on hit rate and TTFT.
4
Which workloads gain most: system prompts, multi-turn, multi-tenant sharing; estimate hit rate and token savings, and how caching composes with continuous batching and PD disaggregation.
5
vLLM prefix caching vs SGLang RadixAttention: block-hash matching vs radix-tree longest-prefix matching; hit granularity, implementation complexity, and the gap on long shared prefixes.
Updated 2026-08-12
🎯
Verify Mastery: Practice Exam Questions for "Prompt Caching & RadixAttention"
Take pitfall multiple-choice questions with derivation options; mistakes are automatically logged.
🚀 Start Card Practice ➔
← Previous CardContinuous BatchingNext Card →High-Concurrency Serving

🔗 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/prompt-caching-radix
中文速览与面试要点

Prompt Caching/RadixAttention (Prompt Caching & RadixAttention)

定义: Prompt Caching = 复用相同前缀 prompt 已计算好的 KV 缓存,跳过重复的 Prefill 计算。典型可复用前缀: 系统提示 (system prompt)、Few-shot 示例、多轮对话的早期轮次、多租户共享的公共指令块 —— 这些前缀占一次请求 token 的 50-90%,却每请求重算一遍纯属浪费。RadixAttention (SGLang 核心): 把所有活跃请求的 KV 块组织进一棵基数树 (RadixTree),树上每个节点 = 一段公共前缀,根为空,路径 = 从根到节点的 token 序列;新请求沿树做最长前缀匹配 (longest prefix match),命中的节点直接复用其 KV,只对「未命中的后缀」做 Prefill(计算量正比于未命中 token 数)。请求结束或变化时树动态分裂/合并: 公共前缀继续共享、差异分支分裂。效果: 命中前缀的请求 TTFT 可降 80%+(若 90% token 命中且 prefill 占 TTFT 的 90%,TTFT ≈ 原来的 1/10);多轮对话每轮只增量计算新增 token;多租户共享系统提示时单请求成本大幅下降。缓存淘汰: 树节点带引用计数与最后访问时间,显存容量不足时按 LRU 淘汰最久未访问的分支(类似操作系统页面置换),淘汰后再次命中需重算 prefill。对比 vLLM 的 automatic prefix caching: vLLM 按固定长度 block 做哈希前缀匹配(基于 token 的 n-gram 块哈希),实现简单;RadixAttention 用树精确复用任意长度的最长公共前缀,命中粒度更细、长共享前缀场景收益更高。 使用场景: 长系统提示的对话服务、Agent 多轮工具调用(每轮重发完整上下文)、RAG 检索 + 共享指令模板、多租户 SaaS;面试高频“TTFT 为什么降 80%”“RadixTree 怎么匹配前缀”“缓存淘汰策略”。 解决的问题: Prefill 是算力密集阶段,长 prompt 的重复 prefill 浪费 GPU 算力并拉高 TTFT;前缀 KV 复用让重复计算归零 —— 数值上命中率 80-90% 时 prefill 算力需求降一个数量级、TTFT 降 80%+,多轮/多租户场景单位 token 成本显著下降,是长上下文时代推理成本优化最直接的杠杆之一。

核心要点

  • RadixTree 前缀复用原理: 新请求如何做最长前缀匹配;命中的前缀与未命中的后缀分别怎么处理;请求结束时树如何分裂/合并。
  • 为什么 TTFT 能降 80%+: prefill 计算量正比于未命中 token 数;给出命中率与 prefill 占比的数值换算(prefill 占 TTFT 90% 时,90% 命中 → TTFT 约降 81%)。
  • 缓存淘汰策略: LRU / 引用计数在基数树上怎么实现(逐分支淘汰);淘汰错误(驱逐热点前缀)对 hit rate 和 TTFT 的代价。
  • 哪些场景收益最大: 系统提示、多轮对话、多租户共享;hint: 命中率与 token 节省如何估算,与连续批/PD 分离如何叠加。
  • vLLM prefix caching vs SGLang RadixAttention: block 哈希匹配 vs 基数树最长前缀匹配;命中粒度、实现复杂度、长共享前缀场景差异。