# 缓存替换策略

缓存替换策略是缓存在已满且必须存入新条目时，用来决定逐出哪个现有条目的规则。该策略决定命中率，即由缓存而非较慢的后备存储服务的请求所占比例；它适用于所有出现缓存的地方：[CPU 缓存](https://www.edgepedia.org/zh/cpu-cache)行、操作系统的页缓存和缓冲区缓存、数据库缓冲池，以及 Redis 之类的分布式内存缓存。

| 关键事实 | 细节 |
|---|---|
| 理论最优 | 贝拉迪算法（Belady's algorithm）逐出未来下一次引用最远的块，对任意请求模式都能达到最大缓存命中率，但需要知道未来请求。<sup>[1](https://www.sciencedirect.com/science/article/pii/S1389128623000282)</sup> |
| 经典策略 | FIFO、LRU、LFU、随机（Random）、时钟（Clock）、分段FIFO、2Q 和 LRU-K 是文献和真实操作系统中确认的标准策略。<sup>[2](https://static.usenix.org/publications/library/proceedings/usenix02/full_papers/burnett/burnett_html/index.html)</sup> |
| 新近性与频率 | LRU 假设可能变化的工作集以突发形式出现且能装入缓存，因此只有新近性重要；LFU 假设访问频率统计独立且恒定，因此只有长期频率重要。<sup>[3](https://www.vldb.org/pvldb/vol16/p3323-vohringer.pdf)</sup> |
| CLOCK 成本 | CLOCK 对每个缓存页只维护一个位，表示最近是否被访问；LRU 则通过在列表中排列缓存页来维护新近性排名。<sup>[3](https://www.vldb.org/pvldb/vol16/p3323-vohringer.pdf)</sup> |
| ARC 性能 | 在缓存为 4 GB 的类似 SPC1 合成基准上，LRU 的命中率为 9.19%，而 ARC 达到 20%。<sup>[4](https://research.ibm.com/publications/arc-a-self-tuning-low-overhead-replacement-cache)</sup> |
| 自适应边界 | CAR 的竞争比下界为 N + 1，上界为 18N，其中 N 是缓存大小；ARC 的上界为 4N。<sup>[5](https://arxiv.org/abs/1503.07624)</sup> |
| 硬件实践 | CPU 缓存通常使用伪 LRU 而非真正的 LRU；在没有行被锁定时，英特尔（Intel）的数据缓存行替换算法是伪 LRU。<sup>[6](https://download.intel.com/design/intelxscale/31628302.pdf)</sup> |

## 经典策略

最简单的策略按单一可观察属性对条目排序。**FIFO** 逐出驻留时间最长的条目，而不管它最近是否被使用。**LRU**（最近最少使用）逐出最近一次访问距今最远的条目，利用时间局部性：最近用过的数据很可能很快再次被使用。**LFU**（最不经常使用）逐出累计访问计数最低的条目，适合流行度稳定的工作负载，但当热点集合变化时适应缓慢。针对特殊模式存在 **MRU** 和 **LIFO** 变体，而**随机逐出**完全避免记账。<sup>[2](https://static.usenix.org/publications/library/proceedings/usenix02/full_papers/burnett/burnett_html/index.html)</sup><sup> • </sup><sup>[7](https://arxiv.org/abs/2107.14646)</sup>

新近性与频率之间的区别是核心设计轴线。LFU 隐含假设页访问频率统计独立且恒定，因此只有长期频率重要；LRU 假设可能变化的工作集以突发形式出现且能装入缓存，因此只有新近性重要。<sup>[3](https://www.vldb.org/pvldb/vol16/p3323-vohringer.pdf)</sup> 针对一种假设调优的策略在另一种假设下表现很差，这促使了下文描述的自适应和混合策略。

## 低成本近似 LRU

真正的 LRU 在硬件和非常大的软件缓存中实现成本很高，因为每次访问都必须更新新近性排序。**CLOCK** 用每页一个引用位来近似 LRU：一根指针扫过缓存，清除引用位，并逐出遇到的第一个引用位已经清零的页。<sup>[3](https://www.vldb.org/pvldb/vol16/p3323-vohringer.pdf)</sup> 因此，CLOCK 在虚拟内存页替换中主导了数十年，因为纯 LRU 在虚拟内存管理中的成本难以承受。<sup>[8](https://static.usenix.org/publications/library/proceedings/usenix05/tech/general/full_papers/jiang/jiang_html/html.html)</sup>

CPU 缓存在更小规模上面临同样的约束。组相联硬件缓存通常使用**伪 LRU**，这是一种树状近似，所需状态远少于完整的新近性排序；英特尔（Intel）将其数据缓存在没有行被锁定时的行替换算法记录为伪 LRU。<sup>[6](https://download.intel.com/design/intelxscale/31628302.pdf)</sup> 厂商通常不公布处理器的确切替换策略，因此必须通过微基准测试推断其行为。<sup>[9](https://www.researchgate.net/publication/269302058_Reverse_engineering_of_cache_replacement_policies_in_Intel_microprocessors_and_their_evaluation)</sup>

## 自适应与抗扫描策略

LRU 的主要弱点是极少利用历史：一次性的顺序扫描就能把整个工作集冲出缓存，这个问题称为缓存污染或扫描脆弱性。<sup>[8](https://static.usenix.org/publications/library/proceedings/usenix05/tech/general/full_papers/jiang/jiang_html/html.html)</sup> 由 IBM 研究院（IBM Research）提出的 **ARC**（自适应替换缓存）通过把只访问过一次的条目与访问更频繁的条目分开、使用四个 LRU 型列表并动态调整它们之间的分区大小来解决这个问题。它可自调优，不需要用户定义参数，每次请求的时间复杂度与 LRU 一样是常数，并且抗扫描，允许一次性顺序请求穿过而不污染缓存。<sup>[4](https://research.ibm.com/publications/arc-a-self-tuning-low-overhead-replacement-cache)</sup> 相比之下，LRU-2、2Q、LRFU 和 LIRS 等策略需要用户定义参数，而且没有单一选择能在不同工作负载和缓存大小下普遍表现良好；LRU-2 和 LRFU 甚至需要与缓存大小成对数的时间复杂度。<sup>[4](https://research.ibm.com/publications/arc-a-self-tuning-low-overhead-replacement-cache)</sup>

**CAR**（带自适应替换的 CLOCK）把同样的思想应用于低开销的 CLOCK 结构，ARC 和 CAR 在实践中都被证明优于各自的经典对应物。<sup>[5](https://arxiv.org/abs/1503.07624)</sup> 理论分析最终表明，没有病态的最坏情况请求序列能使 ARC 或 CAR 相对于 LRU 退化超过常数因子：CAR（以及 ARC）的竞争比下界为 N + 1，上界为 18N，其中 ARC 为 4N，N 是缓存大小。<sup>[5](https://arxiv.org/abs/1503.07624)</sup>

**CLOCK-Pro** 走另一条路，把 LIRS 缓冲区缓存算法的原理整合进 CLOCK 式机制。它的工作方式与 CLOCK 类似，成本对虚拟内存而言可承受，不使用预定参数，并能适应变化的访问模式；在局部性强的程序上，它保留 LRU 的优势，并略优于 CLOCK。<sup>[8](https://static.usenix.org/publications/library/proceedings/usenix05/tech/general/full_papers/jiang/jiang_html/html.html)</sup>

## 依赖工作负载的行为

没有一种策略能在所有情况下胜出，实测比较让工作负载依赖性变得具体。在一项基准测试中，比较七种策略（LRU、LFU、FIFO、MRU、LIFO、CLOCK 和 ARC）在带每条记录 TTL 的容忍过期数据上的表现，用 Python 实现，以 [PostgreSQL](https://www.edgepedia.org/zh/postgresql) 为真值来源、Redis 为缓存；在所有测试的缓存大小下，ARC 的平均命中率最高，LRU 和 CLOCK 紧随其后。当热点集合轮换时，LFU 表现很差，而 MRU 始终最弱。通过对整个缓存统一施加短暂的 TTL 覆盖，作者得以完全消除返回过期条目的情况，尽管这伴随着命中率的大幅下降——他们不把这描述为存在唯一正确设置，而是描述为操作者可根据工作负载调整的旋钮。<sup>[10](https://zenodo.org/records/20576370)</sup>

在分布式环境中，将 LRU、LFU、ARC 和 TLRU（时间感知 LRU）在命中率、延迟降低、内存开销和可扩展性方面进行比较，发现 LRU 等传统算法仍然普遍，而结合机器学习技术的混合方法在动态环境中表现出更优性能。<sup>[11](https://arxiv.org/abs/2504.02220)</sup> 近期研究还利用完整访问历史而非单一统计量：WATT 策略通过结合不同时间尺度上的子频率，从访问日志计算页价值，在 LRU 只看新近性和 LFU 只看频率这两个极端之间取得折中。<sup>[3](https://www.vldb.org/pvldb/vol16/p3323-vohringer.pdf)</sup>

## 真实系统中的实现

操作系统和缓存服务器把替换策略作为配置选项暴露出来。当缓存超过设定的内存限制时，Redis 会自动逐出键，其可配置的逐出策略包括通过 maxmemory-policy 设置选择的 LRU 和 LFU 变体。<sup>[12](https://redis.io/docs/latest/develop/reference/eviction/)</sup> 亚马逊 ElastiCache for Redis（Amazon ElastiCache for Redis）默认使用 volatile-lru 策略，也支持 LFU 逐出。<sup>[13](https://docs.aws.amazon.com/whitepapers/latest/database-caching-strategies-using-redis/database-caching-strategies-using-redis.pdf)</sup>

[Linux 内核](https://www.edgepedia.org/zh/linux-kernel)（Linux kernel）持续演进其页回收策略。多代 LRU（MGLRU）是一种替代 LRU 实现，它通过把访问新近性相近的页归入同一代，来优化内存压力下的页回收。<sup>[14](https://kernel.org/doc/html/v6.1/mm/multigen_lru.html)</sup> 在设备映射器的缓存目标中，较旧的 mq 策略在 64 位机器上每个缓存块使用 88 字节元数据，而其后继 smq 通过存储 28 位索引而非指针并避免显式的每块命中计数，将每块元数据降至约 25 字节。<sup>[15](https://cdn.kernel.org/doc/html/latest/admin-guide/device-mapper/cache-policies.html)</sup>

由于策略常常没有文档，研究人员构建了从外部推断策略的工具。Dust 指纹识别工具能识别缓冲区缓存使用的是初始访问顺序、新近性、频率还是长期历史，并成功区分了 NetBSD 1.5、Linux 2.2.19 和 2.4.14 以及 Solaris 2.7 中的策略；一个 Web 服务器根据这些知识对操作系统缓存策略建模，同时改善了平均响应时间和吞吐量。<sup>[2](https://static.usenix.org/publications/library/proceedings/usenix02/full_papers/burnett/burnett_html/index.html)</sup>

## 分析模型

命中率也可以通过分析预测。TTL（生存时间）近似用条目会留在缓存中的特征时间来建模替换算法，从而无需模拟逐出就能计算命中概率。此类近似在独立引用模型下适用于 LRU，并已扩展到一般马尔可夫到达过程下的 h-LRU 和 LRU(m) 算法，数值和理论支持表明它们是渐近精确的。<sup>[16](https://www.sciencedirect.com/science/article/pii/S0166531617300688)</sup> 独立请求下固定大小缓存命中率的精确乘积形式解适用于 FIFO、RANDOM 和基于时钟的策略。<sup>[1](https://www.sciencedirect.com/science/article/pii/S1389128623000282)</sup>

## References

1. [an overview of analysis methods and evaluation results for caching strategies - sciencedirect](https://www.sciencedirect.com/science/article/pii/S1389128623000282)
2. [Exploiting Gray-Box Knowledge of Buffer-Cache Management, USENIX 2002](https://static.usenix.org/publications/library/proceedings/usenix02/full_papers/burnett/burnett_html/index.html)
3. [Comparison of replacement algorithms (WATT), PVLDB Vol. 16](https://www.vldb.org/pvldb/vol16/p3323-vohringer.pdf)
4. [ARC: A self-tuning, low overhead replacement cache, IBM Research](https://research.ibm.com/publications/arc-a-self-tuning-low-overhead-replacement-cache)
5. [Analyzing Adaptive Cache Replacement Strategies, arXiv](https://arxiv.org/abs/1503.07624)
6. [Intel XScale core developer's manual, section 6.2.4 Rep](https://download.intel.com/design/intelxscale/31628302.pdf)
7. [Cache Replacement Algorithm, arXiv](https://arxiv.org/abs/2107.14646)
8. [CLOCK-Pro: An Effective Improvement of the CLOCK Replacement, USENIX 2005](https://static.usenix.org/publications/library/proceedings/usenix05/tech/general/full_papers/jiang/jiang_html/html.html)
9. [Reverse engineering of cache replacement policies in Intel microprocessors and their evaluation, ResearchGate](https://www.researchgate.net/publication/269302058_Reverse_engineering_of_cache_replacement_policies_in_Intel_microprocessors_and_their_evaluation)
10. [Comparative Evaluation of Cache Replacement Algorithms for Time-Bounded Stale-Tolerant Data Workloads, Zenodo](https://zenodo.org/records/20576370)
11. [Comparative Analysis of Distributed Caching Algorithms, arXiv](https://arxiv.org/abs/2504.02220)
12. [Key eviction, Redis documentation](https://redis.io/docs/latest/develop/reference/eviction/)
13. [Database Caching Strategies Using Redis, AWS whitepaper](https://docs.aws.amazon.com/whitepapers/latest/database-caching-strategies-using-redis/database-caching-strategies-using-redis.pdf)
14. [Multi-Gen LRU, The Linux Kernel documentation](https://kernel.org/doc/html/v6.1/mm/multigen_lru.html)
15. [Guidance for writing policies, The Linux Kernel documentation](https://cdn.kernel.org/doc/html/latest/admin-guide/device-mapper/cache-policies.html)
16. [TTL approximations of the cache replacement algorithms LRU(m) and h-LRU, Performance Evaluation](https://www.sciencedirect.com/science/article/pii/S0166531617300688)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods › Algorithms overview

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/cache-replacement-policies

English original: https://www.edgepedia.org/cache-replacement-policies
