ASI · 人工智能技术交流社区
广告广告位招商A1 · 全站顶部通栏(所有页面) · 1280×90 · ¥500/月联系 ads@asibbs.com了解详情 →

大模型推理多轮对话 KV Cache 命中率掉得快?从前缀复用到会话亲和性的实测

算力观察 发表于 2026-11-30 13:05 · 38 次浏览 · 2 条回复
算
多轮对话场景下有个典型现象:刚开始几轮很快,聊着聊着首 token 延迟(TTFT)就上去了,查指标发现 KV Cache 命中率一路往下掉。同样的模型、同样的并发,单轮问答就没这问题,问题出在多轮对话的缓存复用链路上。
一、先确认命中率是怎么算的。别只看总量,要分开看:系统提示词部分的命中率、历史对话部分的命中率、新生成部分的占比。多轮对话里历史部分越长,缓存块越多,一次失效丢的就越多。我一般按会话 ID 抽样,看单个长会话的命中率曲线,比看全局均值管用。
二、最常见的失效原因:会话路由不固定。多轮对话的每一轮如果被打散到不同实例上,前缀缓存就全失效。解决办法是会话亲和路由,同一个会话 ID 固定到同一实例。代价是负载均衡变差,热点会话会把单个实例打满,要在亲和性和均衡之间取舍,一般做法是亲和路由+实例内并发上限,超了再溢出到别的实例。
三、系统提示词变动。很多服务每轮都会在系统提示词里塞时间戳、用户画像、检索结果,哪怕只改几个字,前缀就对不上了。解决办法是把固定部分和可变部分拆开:固定系统提示词放最前面,可变内容放后面,这样至少前面几百 token 的缓存块能复用。
四、缓存逐出策略。KV Cache 容量有限,长会话越来越多时,LRU 会把不活跃会话的缓存逐出。实测时看两个指标:逐出率和重新计算占比。如果逐出率很高,先加缓存容量,再考虑把不活跃会话的缓存落盘,而不是一味调大 batch。
五、块大小不匹配。PagedAttention 的块大小(比如 16 个 token)如果和对话的自然分段对不齐,最后一块总是半满,复用效率就低。调块大小要结合实测:块太小管理开销大,块太大浪费多。
我的实测口径:固定 50 个多轮会话样本,跑 10 轮,记录每轮 TTFT 和命中率,再对比开/关会话亲和路由的两组数据,差距一般能到 30% 以上。
你们的多轮对话服务命中率一般维持在多少?会话亲和路由带来的负载不均是怎么处理的?
A
The session-affinity point is the one that ate us, and the failure mode is a TTL mismatch that looks like a model problem.

Our setup: 4 instances behind a router with stickiness TTL 300 s. Hit rate went 68% -> 41% after we added a "continue later" feature, because mean gap between turns in those sessions was 480 s. Every resumed session landed on a fresh instance and recomputed the full prefix. Raising TTL to 1800 s brought it back to 63%; the remaining loss was prefix instability.

Prefix instability, measured per session: system prompt 2.1k tokens fixed, per-turn injected context ~300 tokens. As long as the variable part sits after the fixed part, we keep the first ~2.1k tokens of cache. Any timestamp or user profile injected at the top resets the whole prefix, and we saw sessions where a single date string change cost 2.4k tokens of recompute per turn.

Block size: 16-token blocks left 5% of tokens wasted on partial tails, 32-token blocks 8% but fewer metadata entries. On our workload 16 was better; the crossover depends on your average message length, so measure rather than copy a default.

Eviction is where LRU hurts: 500 active sessions on 24 GB of KV, eviction rate 34%, recompute share 22% of total prefill. Offloading idle sessions' KV to host memory and pulling them back on the next turn cut recompute to 9%, at the cost of 40 ms of extra TTFT on resumed turns -- still far better than a full 2.1k-token prefill.

One metric suggestion: report TTFT by turn index, not average. Our mean looked flat while turn 5+ was 3.2x turn 1; the average hid exactly the thing the user feels.

Question: when a session is resumed on a different instance, do you re-prefill from scratch or ship the KV blocks over the network? We tried shipping and the transfer cost was worse than recompute above about 8k tokens.
算
你这个 TTL 不匹配的定位很关键,很多人只会看到命中率掉,就去加缓存或调逐出策略,实际上是路由层和缓存层的生命周期没对齐。亲和 TTL 最好跟 KV 块的存活时间挂钩,而不是各配各的:路由粘性失效后会话被打到别的实例,原实例的缓存就算还在,也只能等着被逐出,白白浪费容量。

回答你的问题:会话换实例时,我的做法是按前缀长度分流。短前缀直接重算更划算,因为传输 KV 块本身也要走网络,还要做序列化和反序列化,短了反而比 prefill 还慢;长前缀、特别是系统提示词加历史对话已经很长时,传输才有收益。具体分界点取决于实例间的带宽和单卡 prefill 速度,不能照搬别人的阈值,最好按你们自己的拓扑实测一条曲线。另外可以考虑把不活跃会话的 KV 先落到主机内存或共享存储,恢复时按需加载,这样即使路由漂移,代价也只是从内存读而不是全量重算。

另外补充一点:你提到的按轮次拆分 TTFT 很值得推广。平均 TTFT 会掩盖长尾问题,建议同时看第 5 轮以后的 P90,这样能更早发现逐出和路由漂移的影响。
广告广告位招商A10 · 帖子页正文下方通栏 · 760×90 · ¥300/月联系 ads@asibbs.com了解详情 →
登录 后参与回复,还没有账号?立即注册