Back to home

Compare

Comparing: How LRU Cache Became LLM Infrastructure's Secret Backbone & LRU 缓存这个 1960 年代的算法,正在被大模型公司重新重视

AEN
LRU CacheKV CachePaged Attention·

How LRU Cache Became LLM Infrastructure's Secret Backbone

Andrew Ng dropped a number this week: 90% of Agent projects get stuck on deployment—not for technical reasons. We buy half of that, but what's really tripping teams up is often the "old-school" infrastructure details, like LRU cache.

What This Is

LRU (Least Recently Used) is an eviction policy: when capacity fills up, discard the data least recently accessed. It's powered by two cooperating data structures: a hash table handles "find the node by key," while a doubly linked list handles "arrange nodes by access order." Together, they make lookup, insertion, and eviction all O(1) (constant time—fixed cost regardless of data volume). A LeetCode #146 solution recently went viral on Juejin, explaining this clearly.

But here's what matters: this 1960s algorithm now underpins AI inference infrastructure. The LLM KV Cache (storing computed attention results for reuse, avoiding recomputation), vector database query caches, RAG (Retrieval-Augmented Generation) system retrieval caches, and Agent short-term conversation memory all run on LRU or its variants.

Industry View

Classic LRU hits two new problems in the AI era. First, "long context" pressure: models now routinely support 128K, 200K, even million-token contexts (token = the model's minimum text unit), and time-based eviction doesn't guarantee keeping the most useful content. Second, "cost structure" has changed: traditional LRU assumes all nodes are equal size, but AI cache entries vary wildly—a vector is a few KB, a conversation history is several MB. Pure count-based eviction wastes GPU memory.

So big model companies have been "reinventing the wheel" in recent years: Anthropic's Prompt Caching prices based on prefix hit rate; vLLM and DeepSeek's Paged Attention (slicing KV Cache into small OS-style pages to manage) solves memory fragmentation; Meta's StreamingLLM uses an attention-sink strategy, letting models "forget the middle, remember the start and end." None of these are LRU, but they're all extensions of—or rebellions against—LRU thinking.

A word of caution: the more complex the caching strategy, the more engineering landmines. Paged Attention still has memory leak issues in some inference frameworks; over-reliance on prefix caching makes cold-start user experience worse. This is an engineering battle with no standard answer.

Impact on Regular People

For enterprise IT: When procuring AI inference services, "cache hit rate" matters more than comparing token unit prices. For the same vendor and same model, a 10-percentage-point hit-rate gap can roughly double your bill.

For individual careers: When using AI tools, building habits of "fixed openings, fixed terminology" actively helps the system improve cache hit rates—and you'll see response times noticeably faster.

For the consumer market: The "speed" of future AI applications will increasingly depend on underlying cache strategies, not raw compute. This means the same GPU can deliver wildly different product experiences depending on the company behind it.

Source: juejin.cn
BZH
LRU缓存KV CachePaged Attention·

LRU 缓存这个 1960 年代的算法,正在被大模型公司重新重视

Andrew Ng 这周抛出一个数:90% 的 Agent 项目卡在落地不是技术——这话我们信一半,但真正卡住的,往往是基础设施里那些「老掉牙」的细节,比如 LRU 缓存。

这是什么

LRU(Least Recently Used,最近最少使用)是一种淘汰策略:容量满了,先扔掉最久没被访问的那条数据。它由两个数据结构协作:哈希表负责「按 key 找到节点」,双向链表负责「按访问顺序排列节点」。两者结合,才能让查找、插入、淘汰全部 O(1)(即常数时间,与数据量无关的固定耗时)完成。掘金上最近一篇 LeetCode 第 146 题的题解被反复转载,把这件事讲得很透。

但值得关心的是:这个 1960 年代就被讨论过的算法,今天正在撑起 AI 推理的底层基础设施——大模型的 KV Cache(把已经算过的注意力结果存起来复用,避免每次重算)、向量数据库的查询缓存、RAG(Retrieval-Augmented Generation,给模型外挂知识库检索)系统的检索缓存、Agent 的短期对话记忆,背后都在用 LRU 或它的变种。

行业怎么看

经典 LRU 在 AI 时代遇到两个新问题。一是「长上下文」压力:模型动不动支持 128K、200K 甚至百万级 token(模型处理文字的最小单元),按时间淘汰并不能保证留下最有用的内容。二是「成本结构」变了:传统 LRU 假设所有节点大小一致,但 AI 系统的缓存项差异巨大(一个向量几 KB,一段对话历史几 MB),纯按数量淘汰会导致显存(GPU 内存)浪费。

因此大模型公司近几年在「重新发明轮子」:Anthropic 的 Prompt Caching(提示缓存)按前缀命中率定价;vLLM 和 DeepSeek 推动的 Paged Attention(把 KV Cache 像操作系统分页一样切成小块管理)解决显存碎片;Meta 的 StreamingLLM 用注意力汇聚策略,让模型可以「忘掉中间、记住首尾」。这些都不是 LRU,但都是 LRU 思想的延伸或反叛。

需要警惕的是:缓存策略讲得越复杂,工程坑也越多。Paged Attention 在部分推理框架中仍有内存泄漏问题;过度依赖前缀缓存,会让冷启动用户体验变差。这是一场没有标准答案的工程仗。

对普通人的影响

对企业 IT:采购 AI 推理服务时,「缓存命中率」比单纯比 token 单价更值得关注——同一厂商同一模型,命中率差 10%,账单可能差一倍。

对个人职场:使用 AI 工具时养成「固定开头、固定术语」的习惯,就是在主动帮系统提高缓存命中率,响应速度会肉眼可见地变快。

对消费市场:未来 AI 应用的「快」越来越取决于底层缓存策略,而非单纯堆算力。这意味着同样一张显卡,不同公司的产品体验差距会越拉越大。

Source: juejin.cn