欢迎关注公众号【拿客】,记得星标⭐获取最新技术内容,我们聚焦低延迟架构、AI智能体、高并发系统设计领域,定期分享深度技术干货。
1.1 什么是缓存
缓存(Cache)是一种高速数据存储层,用于临时存储数据,使得后续对该数据的请求能够比访问数据的主存储位置更快得到响应。在计算机体系结构中,缓存无处不在:CPU有L1/L2/L3缓存,操作系统有页缓存,浏览器有HTTP缓存,应用程序有业务缓存。
在Java应用架构中,缓存通常位于应用程序和持久化存储(如数据库)之间,用于存储频繁访问的数据,减少对数据库的访问压力,提升系统响应速度和吞吐量。
#mermaid-svg-e0zoXI7CeJqbAQiS{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-e0zoXI7CeJqbAQiS .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-e0zoXI7CeJqbAQiS .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-e0zoXI7CeJqbAQiS .error-icon{fill:#552222;}#mermaid-svg-e0zoXI7CeJqbAQiS .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-e0zoXI7CeJqbAQiS .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-e0zoXI7CeJqbAQiS .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-e0zoXI7CeJqbAQiS .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-e0zoXI7CeJqbAQiS .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-e0zoXI7CeJqbAQiS .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-e0zoXI7CeJqbAQiS .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-e0zoXI7CeJqbAQiS .marker{fill:#333333;stroke:#333333;}#mermaid-svg-e0zoXI7CeJqbAQiS .marker.cross{stroke:#333333;}#mermaid-svg-e0zoXI7CeJqbAQiS svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-e0zoXI7CeJqbAQiS p{margin:0;}#mermaid-svg-e0zoXI7CeJqbAQiS .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-e0zoXI7CeJqbAQiS .cluster-label text{fill:#333;}#mermaid-svg-e0zoXI7CeJqbAQiS .cluster-label span{color:#333;}#mermaid-svg-e0zoXI7CeJqbAQiS .cluster-label span p{background-color:transparent;}#mermaid-svg-e0zoXI7CeJqbAQiS .label text,#mermaid-svg-e0zoXI7CeJqbAQiS span{fill:#333;color:#333;}#mermaid-svg-e0zoXI7CeJqbAQiS .node rect,#mermaid-svg-e0zoXI7CeJqbAQiS .node circle,#mermaid-svg-e0zoXI7CeJqbAQiS .node ellipse,#mermaid-svg-e0zoXI7CeJqbAQiS .node polygon,#mermaid-svg-e0zoXI7CeJqbAQiS .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-e0zoXI7CeJqbAQiS .rough-node .label text,#mermaid-svg-e0zoXI7CeJqbAQiS .node .label text,#mermaid-svg-e0zoXI7CeJqbAQiS .image-shape .label,#mermaid-svg-e0zoXI7CeJqbAQiS .icon-shape .label{text-anchor:middle;}#mermaid-svg-e0zoXI7CeJqbAQiS .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-e0zoXI7CeJqbAQiS .rough-node .label,#mermaid-svg-e0zoXI7CeJqbAQiS .node .label,#mermaid-svg-e0zoXI7CeJqbAQiS .image-shape .label,#mermaid-svg-e0zoXI7CeJqbAQiS .icon-shape .label{text-align:center;}#mermaid-svg-e0zoXI7CeJqbAQiS .node.clickable{cursor:pointer;}#mermaid-svg-e0zoXI7CeJqbAQiS .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-e0zoXI7CeJqbAQiS .arrowheadPath{fill:#333333;}#mermaid-svg-e0zoXI7CeJqbAQiS .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-e0zoXI7CeJqbAQiS .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-e0zoXI7CeJqbAQiS .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-e0zoXI7CeJqbAQiS .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-e0zoXI7CeJqbAQiS .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-e0zoXI7CeJqbAQiS .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-e0zoXI7CeJqbAQiS .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-e0zoXI7CeJqbAQiS .cluster text{fill:#333;}#mermaid-svg-e0zoXI7CeJqbAQiS .cluster span{color:#333;}#mermaid-svg-e0zoXI7CeJqbAQiS div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-e0zoXI7CeJqbAQiS .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-e0zoXI7CeJqbAQiS rect.text{fill:none;stroke-width:0;}#mermaid-svg-e0zoXI7CeJqbAQiS .icon-shape,#mermaid-svg-e0zoXI7CeJqbAQiS .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-e0zoXI7CeJqbAQiS .icon-shape p,#mermaid-svg-e0zoXI7CeJqbAQiS .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-e0zoXI7CeJqbAQiS .icon-shape rect,#mermaid-svg-e0zoXI7CeJqbAQiS .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-e0zoXI7CeJqbAQiS .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-e0zoXI7CeJqbAQiS .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-e0zoXI7CeJqbAQiS :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
是
否
用户请求
应用服务
缓存是否存在?
返回缓存数据
查询数据库
数据写入缓存
1.2 缓存的核心价值
缓存的核心价值可以总结为以下几点:
1. 性能提升:内存访问速度通常是磁盘访问速度的105~106倍,将热点数据存储在缓存中可以显著降低响应延迟。典型的磁盘访问延迟在10ms左右,而内存访问延迟仅为100ns左右,差距达到5个数量级。
2. 降低后端压力:对于高并发系统,大量请求直接打到数据库会导致数据库连接耗尽、CPU和IO使用率飙升,甚至引发服务雪崩。通过缓存层拦截大部分读请求,可以有效降低数据库的负载。
3. 提升系统吞吐量:缓存层通常具有更好的水平扩展能力,通过增加缓存节点可以线性提升系统的读吞吐量,而数据库的扩展成本和复杂度要高得多。
4. 提高系统可用性:在后端数据库出现故障时,缓存层可以继续提供部分服务,实现优雅降级,提升系统的整体可用性。
1.3 缓存的适用场景与不适用场景
适用场景:
- 读多写少:数据读取频率远高于写入频率,如商品详情、用户信息、配置数据等
- 数据一致性要求不高:允许短暂的数据不一致,如统计数据、排行榜、推荐内容等
- 计算成本高:数据需要复杂计算生成,如报表、数据分析结果等
- 热点数据:少数数据被频繁访问,如秒杀活动商品、热门新闻等
不适用场景:
- 写多读少:数据写入频率高于读取频率,使用缓存会带来额外的同步开销
- 数据一致性要求极高:必须保证强一致性,如金融交易、支付系统等核心数据
- 数据更新频繁且实时性要求高:数据需要实时更新,如库存系统、实时监控数据等
- 数据访问分散:大部分数据访问概率很低,缓存命中率极低,使用缓存反而增加复杂度
1.4 缓存的性能收益模型
缓存的性能收益主要取决于缓存命中率。缓存命中率是指请求中能从缓存中获取到数据的比例,计算公式为:
命中率 = 缓存命中次数 / (缓存命中次数 + 缓存未命中次数)
缓存的整体性能可以通过以下公式计算:
平均响应时间 = 命中率 * 缓存访问时间 + (1 – 命中率) * (数据库访问时间 + 缓存写入时间)
例如,假设缓存访问时间为1ms,数据库访问时间为10ms,缓存写入时间为0.5ms:
- 命中率90%时:平均响应时间 = 0.91 + 0.1(10+0.5) = 0.9 + 1.05 = 1.95ms
- 命中率50%时:平均响应时间 = 0.51 + 0.5(10+0.5) = 0.5 + 5.25 = 5.75ms
- 命中率10%时:平均响应时间 = 0.11 + 0.9(10+0.5) = 0.1 + 9.45 = 9.55ms
可以看到,缓存命中率对系统性能影响巨大。当命中率低于50%时,缓存带来的性能提升已经非常有限,甚至可能因为额外的缓存操作反而降低性能。通常生产系统需要保证缓存命中率在95%以上才能获得理想的收益。
1.4.1 命中率提升方法论
提升缓存命中率是缓存优化的核心目标,我们可以从以下几个方面入手:
1. 数据筛选策略
- 只缓存频繁访问的热点数据,避免缓存冷数据占用空间
- 分析业务访问模式,识别热点数据特征,优先缓存这些数据
- 对于访问频率低于一定阈值的数据,不进行缓存
2. 缓存粒度优化
- 缓存粒度越小,命中率越高。例如,缓存单个用户信息比缓存整个用户列表的命中率高
- 避免大而全的缓存对象,尽量拆分为细粒度的缓存单元
- 对于部分字段更新频繁的对象,考虑将静态字段和动态字段分开缓存
3. 过期策略优化
- 合理设置过期时间,避免数据过早过期导致命中率下降
- 对于更新频率低的数据,设置较长的过期时间
- 对于更新频率高的数据,设置较短的过期时间,或者采用主动更新策略
- 避免相同类型的数据设置相同的过期时间,分散过期时间点
4. 缓存键设计优化
- 统一缓存键命名规范,避免重复缓存相同数据
- 包含足够的标识信息,确保不同业务的数据不会互相覆盖
- 避免使用过长的缓存键,减少内存开销和计算哈希的时间
5. 淘汰策略选择
- 选择适合业务访问模式的淘汰策略,如W-TinyLFU比LRU命中率更高
- 对于访问模式相对固定的场景,LFU策略通常能获得更高的命中率
- 对于访问模式变化较快的场景,LRU或W-TinyLFU更适合
1.4.2 缓存投入产出比分析
在引入缓存之前,我们需要评估缓存的投入产出比,确保缓存带来的收益大于投入成本。
成本构成:
- 硬件成本:缓存服务器的采购和运维成本
- 开发成本:缓存逻辑的开发和测试成本
- 运维成本:缓存集群的监控、故障排查、扩容等成本
- 复杂度成本:缓存带来的一致性问题、缓存击穿/穿透/雪崩等问题的处理成本
收益构成:
- 性能提升:用户体验提升,转化率提升带来的业务收益
- 成本节约:减少数据库服务器的数量,降低数据库授权和运维成本
- 可用性提升:减少数据库故障带来的业务损失
- 吞吐量提升:系统能够支持更多的并发用户,带来业务增长
投入产出比计算公式:
ROI = (性能提升收益 + 成本节约 + 可用性提升收益 + 吞吐量提升收益) / (硬件成本 + 开发成本 + 运维成本 + 复杂度成本)
当ROI > 1时,引入缓存是划算的。通常在高并发读场景下,缓存的ROI可以达到5~10倍甚至更高。
2. Java缓存生态全景
2.1 缓存分类体系
Java生态中的缓存可以从多个维度进行分类:
#mermaid-svg-C9KNwUFieZrW4Yni{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-C9KNwUFieZrW4Yni .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-C9KNwUFieZrW4Yni .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-C9KNwUFieZrW4Yni .error-icon{fill:#552222;}#mermaid-svg-C9KNwUFieZrW4Yni .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-C9KNwUFieZrW4Yni .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-C9KNwUFieZrW4Yni .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-C9KNwUFieZrW4Yni .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-C9KNwUFieZrW4Yni .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-C9KNwUFieZrW4Yni .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-C9KNwUFieZrW4Yni .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-C9KNwUFieZrW4Yni .marker{fill:#333333;stroke:#333333;}#mermaid-svg-C9KNwUFieZrW4Yni .marker.cross{stroke:#333333;}#mermaid-svg-C9KNwUFieZrW4Yni svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-C9KNwUFieZrW4Yni p{margin:0;}#mermaid-svg-C9KNwUFieZrW4Yni .edge{stroke-width:3;}#mermaid-svg-C9KNwUFieZrW4Yni .section–1 rect,#mermaid-svg-C9KNwUFieZrW4Yni .section–1 path,#mermaid-svg-C9KNwUFieZrW4Yni .section–1 circle,#mermaid-svg-C9KNwUFieZrW4Yni .section–1 polygon,#mermaid-svg-C9KNwUFieZrW4Yni .section–1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-C9KNwUFieZrW4Yni .section–1 text{fill:#ffffff;}#mermaid-svg-C9KNwUFieZrW4Yni .node-icon–1{font-size:40px;color:#ffffff;}#mermaid-svg-C9KNwUFieZrW4Yni .section-edge–1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-C9KNwUFieZrW4Yni .edge-depth–1{stroke-width:17;}#mermaid-svg-C9KNwUFieZrW4Yni .section–1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-C9KNwUFieZrW4Yni .disabled,#mermaid-svg-C9KNwUFieZrW4Yni .disabled circle,#mermaid-svg-C9KNwUFieZrW4Yni .disabled text{fill:lightgray;}#mermaid-svg-C9KNwUFieZrW4Yni .disabled text{fill:#efefef;}#mermaid-svg-C9KNwUFieZrW4Yni .section-0 rect,#mermaid-svg-C9KNwUFieZrW4Yni .section-0 path,#mermaid-svg-C9KNwUFieZrW4Yni .section-0 circle,#mermaid-svg-C9KNwUFieZrW4Yni .section-0 polygon,#mermaid-svg-C9KNwUFieZrW4Yni .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-C9KNwUFieZrW4Yni .section-0 text{fill:black;}#mermaid-svg-C9KNwUFieZrW4Yni .node-icon-0{font-size:40px;color:black;}#mermaid-svg-C9KNwUFieZrW4Yni .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-C9KNwUFieZrW4Yni .edge-depth-0{stroke-width:14;}#mermaid-svg-C9KNwUFieZrW4Yni .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-C9KNwUFieZrW4Yni .disabled,#mermaid-svg-C9KNwUFieZrW4Yni .disabled circle,#mermaid-svg-C9KNwUFieZrW4Yni .disabled text{fill:lightgray;}#mermaid-svg-C9KNwUFieZrW4Yni .disabled text{fill:#efefef;}#mermaid-svg-C9KNwUFieZrW4Yni .section-1 rect,#mermaid-svg-C9KNwUFieZrW4Yni .section-1 path,#mermaid-svg-C9KNwUFieZrW4Yni .section-1 circle,#mermaid-svg-C9KNwUFieZrW4Yni .section-1 polygon,#mermaid-svg-C9KNwUFieZrW4Yni .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-C9KNwUFieZrW4Yni .section-1 text{fill:black;}#mermaid-svg-C9KNwUFieZrW4Yni .node-icon-1{font-size:40px;color:black;}#mermaid-svg-C9KNwUFieZrW4Yni .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-C9KNwUFieZrW4Yni .edge-depth-1{stroke-width:11;}#mermaid-svg-C9KNwUFieZrW4Yni .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-C9KNwUFieZrW4Yni .disabled,#mermaid-svg-C9KNwUFieZrW4Yni .disabled circle,#mermaid-svg-C9KNwUFieZrW4Yni .disabled text{fill:lightgray;}#mermaid-svg-C9KNwUFieZrW4Yni .disabled text{fill:#efefef;}#mermaid-svg-C9KNwUFieZrW4Yni .section-2 rect,#mermaid-svg-C9KNwUFieZrW4Yni .section-2 path,#mermaid-svg-C9KNwUFieZrW4Yni .section-2 circle,#mermaid-svg-C9KNwUFieZrW4Yni .section-2 polygon,#mermaid-svg-C9KNwUFieZrW4Yni .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-C9KNwUFieZrW4Yni .section-2 text{fill:#ffffff;}#mermaid-svg-C9KNwUFieZrW4Yni .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-C9KNwUFieZrW4Yni .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-C9KNwUFieZrW4Yni .edge-depth-2{stroke-width:8;}#mermaid-svg-C9KNwUFieZrW4Yni .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-C9KNwUFieZrW4Yni .disabled,#mermaid-svg-C9KNwUFieZrW4Yni .disabled circle,#mermaid-svg-C9KNwUFieZrW4Yni .disabled text{fill:lightgray;}#mermaid-svg-C9KNwUFieZrW4Yni .disabled text{fill:#efefef;}#mermaid-svg-C9KNwUFieZrW4Yni .section-3 rect,#mermaid-svg-C9KNwUFieZrW4Yni .section-3 path,#mermaid-svg-C9KNwUFieZrW4Yni .section-3 circle,#mermaid-svg-C9KNwUFieZrW4Yni .section-3 polygon,#mermaid-svg-C9KNwUFieZrW4Yni .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-C9KNwUFieZrW4Yni .section-3 text{fill:black;}#mermaid-svg-C9KNwUFieZrW4Yni .node-icon-3{font-size:40px;color:black;}#mermaid-svg-C9KNwUFieZrW4Yni .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-C9KNwUFieZrW4Yni .edge-depth-3{stroke-width:5;}#mermaid-svg-C9KNwUFieZrW4Yni .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-C9KNwUFieZrW4Yni .disabled,#mermaid-svg-C9KNwUFieZrW4Yni .disabled circle,#mermaid-svg-C9KNwUFieZrW4Yni .disabled text{fill:lightgray;}#mermaid-svg-C9KNwUFieZrW4Yni .disabled text{fill:#efefef;}#mermaid-svg-C9KNwUFieZrW4Yni .section-4 rect,#mermaid-svg-C9KNwUFieZrW4Yni .section-4 path,#mermaid-svg-C9KNwUFieZrW4Yni .section-4 circle,#mermaid-svg-C9KNwUFieZrW4Yni .section-4 polygon,#mermaid-svg-C9KNwUFieZrW4Yni .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-C9KNwUFieZrW4Yni .section-4 text{fill:black;}#mermaid-svg-C9KNwUFieZrW4Yni .node-icon-4{font-size:40px;color:black;}#mermaid-svg-C9KNwUFieZrW4Yni .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-C9KNwUFieZrW4Yni .edge-depth-4{stroke-width:2;}#mermaid-svg-C9KNwUFieZrW4Yni .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-C9KNwUFieZrW4Yni .disabled,#mermaid-svg-C9KNwUFieZrW4Yni .disabled circle,#mermaid-svg-C9KNwUFieZrW4Yni .disabled text{fill:lightgray;}#mermaid-svg-C9KNwUFieZrW4Yni .disabled text{fill:#efefef;}#mermaid-svg-C9KNwUFieZrW4Yni .section-5 rect,#mermaid-svg-C9KNwUFieZrW4Yni .section-5 path,#mermaid-svg-C9KNwUFieZrW4Yni .section-5 circle,#mermaid-svg-C9KNwUFieZrW4Yni .section-5 polygon,#mermaid-svg-C9KNwUFieZrW4Yni .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-C9KNwUFieZrW4Yni .section-5 text{fill:black;}#mermaid-svg-C9KNwUFieZrW4Yni .node-icon-5{font-size:40px;color:black;}#mermaid-svg-C9KNwUFieZrW4Yni .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-C9KNwUFieZrW4Yni .edge-depth-5{stroke-width:-1;}#mermaid-svg-C9KNwUFieZrW4Yni .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-C9KNwUFieZrW4Yni .disabled,#mermaid-svg-C9KNwUFieZrW4Yni .disabled circle,#mermaid-svg-C9KNwUFieZrW4Yni .disabled text{fill:lightgray;}#mermaid-svg-C9KNwUFieZrW4Yni .disabled text{fill:#efefef;}#mermaid-svg-C9KNwUFieZrW4Yni .section-6 rect,#mermaid-svg-C9KNwUFieZrW4Yni .section-6 path,#mermaid-svg-C9KNwUFieZrW4Yni .section-6 circle,#mermaid-svg-C9KNwUFieZrW4Yni .section-6 polygon,#mermaid-svg-C9KNwUFieZrW4Yni .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-C9KNwUFieZrW4Yni .section-6 text{fill:black;}#mermaid-svg-C9KNwUFieZrW4Yni .node-icon-6{font-size:40px;color:black;}#mermaid-svg-C9KNwUFieZrW4Yni .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-C9KNwUFieZrW4Yni .edge-depth-6{stroke-width:-4;}#mermaid-svg-C9KNwUFieZrW4Yni .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-C9KNwUFieZrW4Yni .disabled,#mermaid-svg-C9KNwUFieZrW4Yni .disabled circle,#mermaid-svg-C9KNwUFieZrW4Yni .disabled text{fill:lightgray;}#mermaid-svg-C9KNwUFieZrW4Yni .disabled text{fill:#efefef;}#mermaid-svg-C9KNwUFieZrW4Yni .section-7 rect,#mermaid-svg-C9KNwUFieZrW4Yni .section-7 path,#mermaid-svg-C9KNwUFieZrW4Yni .section-7 circle,#mermaid-svg-C9KNwUFieZrW4Yni .section-7 polygon,#mermaid-svg-C9KNwUFieZrW4Yni .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-C9KNwUFieZrW4Yni .section-7 text{fill:black;}#mermaid-svg-C9KNwUFieZrW4Yni .node-icon-7{font-size:40px;color:black;}#mermaid-svg-C9KNwUFieZrW4Yni .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-C9KNwUFieZrW4Yni .edge-depth-7{stroke-width:-7;}#mermaid-svg-C9KNwUFieZrW4Yni .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-C9KNwUFieZrW4Yni .disabled,#mermaid-svg-C9KNwUFieZrW4Yni .disabled circle,#mermaid-svg-C9KNwUFieZrW4Yni .disabled text{fill:lightgray;}#mermaid-svg-C9KNwUFieZrW4Yni .disabled text{fill:#efefef;}#mermaid-svg-C9KNwUFieZrW4Yni .section-8 rect,#mermaid-svg-C9KNwUFieZrW4Yni .section-8 path,#mermaid-svg-C9KNwUFieZrW4Yni .section-8 circle,#mermaid-svg-C9KNwUFieZrW4Yni .section-8 polygon,#mermaid-svg-C9KNwUFieZrW4Yni .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-C9KNwUFieZrW4Yni .section-8 text{fill:black;}#mermaid-svg-C9KNwUFieZrW4Yni .node-icon-8{font-size:40px;color:black;}#mermaid-svg-C9KNwUFieZrW4Yni .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-C9KNwUFieZrW4Yni .edge-depth-8{stroke-width:-10;}#mermaid-svg-C9KNwUFieZrW4Yni .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-C9KNwUFieZrW4Yni .disabled,#mermaid-svg-C9KNwUFieZrW4Yni .disabled circle,#mermaid-svg-C9KNwUFieZrW4Yni .disabled text{fill:lightgray;}#mermaid-svg-C9KNwUFieZrW4Yni .disabled text{fill:#efefef;}#mermaid-svg-C9KNwUFieZrW4Yni .section-9 rect,#mermaid-svg-C9KNwUFieZrW4Yni .section-9 path,#mermaid-svg-C9KNwUFieZrW4Yni .section-9 circle,#mermaid-svg-C9KNwUFieZrW4Yni .section-9 polygon,#mermaid-svg-C9KNwUFieZrW4Yni .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-C9KNwUFieZrW4Yni .section-9 text{fill:black;}#mermaid-svg-C9KNwUFieZrW4Yni .node-icon-9{font-size:40px;color:black;}#mermaid-svg-C9KNwUFieZrW4Yni .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-C9KNwUFieZrW4Yni .edge-depth-9{stroke-width:-13;}#mermaid-svg-C9KNwUFieZrW4Yni .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-C9KNwUFieZrW4Yni .disabled,#mermaid-svg-C9KNwUFieZrW4Yni .disabled circle,#mermaid-svg-C9KNwUFieZrW4Yni .disabled text{fill:lightgray;}#mermaid-svg-C9KNwUFieZrW4Yni .disabled text{fill:#efefef;}#mermaid-svg-C9KNwUFieZrW4Yni .section-10 rect,#mermaid-svg-C9KNwUFieZrW4Yni .section-10 path,#mermaid-svg-C9KNwUFieZrW4Yni .section-10 circle,#mermaid-svg-C9KNwUFieZrW4Yni .section-10 polygon,#mermaid-svg-C9KNwUFieZrW4Yni .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-C9KNwUFieZrW4Yni .section-10 text{fill:black;}#mermaid-svg-C9KNwUFieZrW4Yni .node-icon-10{font-size:40px;color:black;}#mermaid-svg-C9KNwUFieZrW4Yni .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-C9KNwUFieZrW4Yni .edge-depth-10{stroke-width:-16;}#mermaid-svg-C9KNwUFieZrW4Yni .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-C9KNwUFieZrW4Yni .disabled,#mermaid-svg-C9KNwUFieZrW4Yni .disabled circle,#mermaid-svg-C9KNwUFieZrW4Yni .disabled text{fill:lightgray;}#mermaid-svg-C9KNwUFieZrW4Yni .disabled text{fill:#efefef;}#mermaid-svg-C9KNwUFieZrW4Yni .section-root rect,#mermaid-svg-C9KNwUFieZrW4Yni .section-root path,#mermaid-svg-C9KNwUFieZrW4Yni .section-root circle,#mermaid-svg-C9KNwUFieZrW4Yni .section-root polygon{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-C9KNwUFieZrW4Yni .section-root text{fill:#ffffff;}#mermaid-svg-C9KNwUFieZrW4Yni .section-root span{color:#ffffff;}#mermaid-svg-C9KNwUFieZrW4Yni .section-2 span{color:#ffffff;}#mermaid-svg-C9KNwUFieZrW4Yni .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-C9KNwUFieZrW4Yni .edge{fill:none;}#mermaid-svg-C9KNwUFieZrW4Yni .mindmap-node-label{dy:1em;alignment-baseline:middle;text-anchor:middle;dominant-baseline:middle;text-align:center;}#mermaid-svg-C9KNwUFieZrW4Yni :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
Java缓存分类
按部署位置
本地缓存
分布式缓存
边缘缓存
按存储介质
堆内缓存
堆外缓存
磁盘缓存
按功能特性
通用缓存
计算缓存
会话缓存
按一致性模型
最终一致性缓存
强一致性缓存
事务型缓存
2.1.1 本地缓存 vs 分布式缓存
本地缓存:
- 定义:与应用程序在同一个进程内的缓存组件
- 优点:访问速度极快(进程内调用,无网络开销)、部署简单、运维成本低
- 缺点:数据无法在多节点间共享、内存容量受限于JVM堆大小、应用重启数据丢失
- 适用场景:单节点应用、不需要共享的热点数据、作为分布式缓存的二级缓存
分布式缓存:
- 定义:独立部署的缓存服务集群,多个应用节点可以共享访问
- 优点:数据可以跨节点共享、支持水平扩展、容量大、高可用、数据持久化
- 缺点:访问有网络开销、部署运维复杂、一致性问题处理难度大
- 适用场景:分布式系统、需要多节点共享的数据、大规模应用集群
2.1.2 堆内缓存 vs 堆外缓存
堆内缓存:
- 定义:数据存储在JVM堆内存中
- 优点:访问速度最快、不需要序列化/反序列化
- 缺点:受GC影响、内存容量有限、大对象会导致频繁GC
- 代表:Guava Cache、Caffeine、HashMap
堆外缓存:
- 定义:数据存储在JVM堆外内存中(直接内存)
- 优点:不受GC影响、可以使用更大内存空间、减少GC停顿
- 缺点:数据需要序列化/反序列化、访问速度比堆内慢
- 代表:Ehcache 3.x、Ignite、Redis(进程外堆外)
2.1.3 不同层级缓存的性能对比
| CPU L1缓存 | 10亿+ | 0.5-1ns | 32-64KB | 极高 |
| CPU L2缓存 | 5亿 | 3-5ns | 256-512KB | 高 |
| CPU L3缓存 | 2亿 | 10-15ns | 16-32MB | 中 |
| JVM堆内存 | 5000万 | 50-100ns | 1-16GB | 中 |
| 堆外内存 | 1000万 | 100-200ns | 1-32GB | 低 |
| Redis单机 | 10万 | 0.1-1ms | 1-100GB | 低 |
| MySQL数据库 | 1000 | 1-10ms | TB级 | 低 |
性能对比说明:
从性能对比可以看到,不同层级的缓存性能差异巨大:
- CPU缓存速度是内存的几十到上百倍
- 堆内缓存速度是分布式缓存的几百倍
- 分布式缓存速度是数据库的几百倍
这就是为什么我们需要多级缓存架构,尽量让请求在更上层的缓存得到处理。
2.2 主流本地缓存框架对比
2.2.1 Guava Cache
Guava Cache是Google Guava库中的缓存实现,是Java生态中使用最广泛的本地缓存组件之一。
核心特性:
- 基于LRU淘汰策略
- 支持基于时间的过期(写入后过期、访问后过期)
- 支持软引用/弱引用存储
- 提供缓存统计功能
- 支持自动加载缓存(LoadingCache)
- 线程安全,使用ConcurrentHashMap实现
适用场景:
- 中小规模的本地缓存需求
- 简单的缓存场景,不需要复杂特性
- 已经使用Guava库的项目
示例代码:
LoadingCache<String, User> userCache = CacheBuilder.newBuilder()
.maximumSize(1000) // 最大容量
.expireAfterWrite(10, TimeUnit.MINUTES) // 写入后10分钟过期
.expireAfterAccess(5, TimeUnit.MINUTES) // 访问后5分钟过期
.recordStats() // 开启统计
.build(new CacheLoader<String, User>() {
@Override
public User load(String userId) throws Exception {
// 从数据库加载数据
return userRepository.findById(userId);
}
});
Guava Cache的实现原理: Guava Cache内部使用Segment分段锁实现高并发,每个Segment相当于一个小的ConcurrentHashMap,不同Segment之间的操作互不影响。这种设计在JDK 1.7时代性能很好,但在JDK 1.8+ ConcurrentHashMap优化后,分段锁的优势已经不明显。
2.2.2 Caffeine
Caffeine是基于Java 8的高性能缓存库,是Guava Cache的改进版,目前是Spring Boot 2.x+默认的本地缓存实现。
核心特性:
- 基于W-TinyLFU淘汰策略,命中率远高于LRU
- 支持多种过期策略:基于时间、基于大小、基于引用
- 支持异步加载、刷新机制
- 高性能:读写性能是Guava Cache的2~5倍
- 完全兼容Guava Cache的API
- 支持事件监听、统计监控
性能对比:
| Guava Cache | 1,200 | 低 | 中(LRU) | 简单场景,已使用Guava的项目 |
| Caffeine | 3,500 | 低 | 极高(W-TinyLFU) | 高性能要求,Spring Boot默认 |
| Ehcache 3 | 800 | 高 | 中 | 需要堆外存储、磁盘持久化 |
| HashMap(无缓存功能) | 5,000 | 低 | – | 简单KV存储,无淘汰策略 |
性能对比说明:
适用场景:
- 高性能要求的本地缓存场景
- 需要高命中率的缓存场景
- 替代Guava Cache的首选方案
示例代码:
Cache<String, User> userCache = Caffeine.newBuilder()
.maximumSize(10_000)
.expireAfterWrite(Duration.ofMinutes(10))
.refreshAfterWrite(Duration.ofMinutes(5)) // 写入5分钟后自动刷新
.recordStats()
.build(key -> userRepository.findById(key));
Caffeine的高性能实现原理:
2.2.3 Ehcache
Ehcache是一个成熟的开源Java缓存框架,支持多层缓存架构,是Hibernate默认的缓存实现。
核心特性:
- 支持堆内、堆外、磁盘三级存储
- 支持JCache(JSR-107)标准
- 支持分布式缓存部署
- 提供事务支持
- 支持持久化到磁盘
- 支持集群模式
适用场景:
- 需要多级存储的缓存场景
- 需要持久化的本地缓存
- 企业级应用,需要JCache标准支持
Ehcache 3.x的三级存储架构:
#mermaid-svg-Wf89HbBKhkFWqY4O{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-Wf89HbBKhkFWqY4O .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-Wf89HbBKhkFWqY4O .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-Wf89HbBKhkFWqY4O .error-icon{fill:#552222;}#mermaid-svg-Wf89HbBKhkFWqY4O .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Wf89HbBKhkFWqY4O .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-Wf89HbBKhkFWqY4O .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Wf89HbBKhkFWqY4O .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Wf89HbBKhkFWqY4O .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-Wf89HbBKhkFWqY4O .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Wf89HbBKhkFWqY4O .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Wf89HbBKhkFWqY4O .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Wf89HbBKhkFWqY4O .marker.cross{stroke:#333333;}#mermaid-svg-Wf89HbBKhkFWqY4O svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Wf89HbBKhkFWqY4O p{margin:0;}#mermaid-svg-Wf89HbBKhkFWqY4O .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-Wf89HbBKhkFWqY4O .cluster-label text{fill:#333;}#mermaid-svg-Wf89HbBKhkFWqY4O .cluster-label span{color:#333;}#mermaid-svg-Wf89HbBKhkFWqY4O .cluster-label span p{background-color:transparent;}#mermaid-svg-Wf89HbBKhkFWqY4O .label text,#mermaid-svg-Wf89HbBKhkFWqY4O span{fill:#333;color:#333;}#mermaid-svg-Wf89HbBKhkFWqY4O .node rect,#mermaid-svg-Wf89HbBKhkFWqY4O .node circle,#mermaid-svg-Wf89HbBKhkFWqY4O .node ellipse,#mermaid-svg-Wf89HbBKhkFWqY4O .node polygon,#mermaid-svg-Wf89HbBKhkFWqY4O .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-Wf89HbBKhkFWqY4O .rough-node .label text,#mermaid-svg-Wf89HbBKhkFWqY4O .node .label text,#mermaid-svg-Wf89HbBKhkFWqY4O .image-shape .label,#mermaid-svg-Wf89HbBKhkFWqY4O .icon-shape .label{text-anchor:middle;}#mermaid-svg-Wf89HbBKhkFWqY4O .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-Wf89HbBKhkFWqY4O .rough-node .label,#mermaid-svg-Wf89HbBKhkFWqY4O .node .label,#mermaid-svg-Wf89HbBKhkFWqY4O .image-shape .label,#mermaid-svg-Wf89HbBKhkFWqY4O .icon-shape .label{text-align:center;}#mermaid-svg-Wf89HbBKhkFWqY4O .node.clickable{cursor:pointer;}#mermaid-svg-Wf89HbBKhkFWqY4O .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-Wf89HbBKhkFWqY4O .arrowheadPath{fill:#333333;}#mermaid-svg-Wf89HbBKhkFWqY4O .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-Wf89HbBKhkFWqY4O .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-Wf89HbBKhkFWqY4O .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Wf89HbBKhkFWqY4O .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-Wf89HbBKhkFWqY4O .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Wf89HbBKhkFWqY4O .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-Wf89HbBKhkFWqY4O .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-Wf89HbBKhkFWqY4O .cluster text{fill:#333;}#mermaid-svg-Wf89HbBKhkFWqY4O .cluster span{color:#333;}#mermaid-svg-Wf89HbBKhkFWqY4O div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-Wf89HbBKhkFWqY4O .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-Wf89HbBKhkFWqY4O rect.text{fill:none;stroke-width:0;}#mermaid-svg-Wf89HbBKhkFWqY4O .icon-shape,#mermaid-svg-Wf89HbBKhkFWqY4O .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Wf89HbBKhkFWqY4O .icon-shape p,#mermaid-svg-Wf89HbBKhkFWqY4O .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-Wf89HbBKhkFWqY4O .icon-shape rect,#mermaid-svg-Wf89HbBKhkFWqY4O .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Wf89HbBKhkFWqY4O .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-Wf89HbBKhkFWqY4O .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-Wf89HbBKhkFWqY4O :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
是
否
是
否
是
否
应用请求
堆内缓存Heap Tier
是否命中?
返回数据
堆外缓存Off-Heap Tier
是否命中?
返回数据并写入堆内缓存
磁盘缓存Disk Tier
是否命中?
返回数据并写入堆外和堆内缓存
从数据源加载
写入各级缓存
这种三级存储架构结合了各级存储的优势:堆内缓存速度最快,容量最小;堆外缓存速度次之,容量较大;磁盘缓存速度最慢,但容量最大,支持持久化。
2.2.4 本地缓存框架选型建议
| 性能 | 中 | 极高 | 中 |
| 命中率 | 中(LRU) | 极高(W-TinyLFU) | 中 |
| 内存占用 | 低 | 低 | 高 |
| 堆外存储 | 不支持 | 不支持 | 支持 |
| 磁盘持久化 | 不支持 | 不支持 | 支持 |
| JCache标准 | 不支持 | 不支持 | 支持 |
| 学习成本 | 低 | 低 | 高 |
| 活跃度 | 低 | 高 | 中 |
选型建议:
- 绝大多数场景优先选择Caffeine,性能最好,API简单
- 如果需要堆外存储、磁盘持久化或JCache标准,选择Ehcache 3
- 老项目如果已经使用Guava Cache且没有性能问题,可以继续使用
2.3 主流分布式缓存对比
2.3.1 Redis
Redis(Remote Dictionary Server)是目前最流行的开源分布式缓存系统,以高性能、丰富的数据结构和灵活的特性著称。
核心特性:
- 支持丰富的数据结构:String、Hash、List、Set、Sorted Set、Bitmap、HyperLogLog、Geo、Stream等
- 单线程模型,高性能:QPS可达10万+
- 支持持久化:RDB快照、AOF日志
- 支持多种集群模式:主从复制、哨兵模式、Redis Cluster
- 支持事务、Lua脚本、发布订阅
- 丰富的生态系统:客户端库完善,周边工具丰富
架构图:
#mermaid-svg-XLc2CJdKGi5ZLNXy{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-XLc2CJdKGi5ZLNXy .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-XLc2CJdKGi5ZLNXy .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-XLc2CJdKGi5ZLNXy .error-icon{fill:#552222;}#mermaid-svg-XLc2CJdKGi5ZLNXy .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-XLc2CJdKGi5ZLNXy .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-XLc2CJdKGi5ZLNXy .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-XLc2CJdKGi5ZLNXy .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-XLc2CJdKGi5ZLNXy .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-XLc2CJdKGi5ZLNXy .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-XLc2CJdKGi5ZLNXy .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-XLc2CJdKGi5ZLNXy .marker{fill:#333333;stroke:#333333;}#mermaid-svg-XLc2CJdKGi5ZLNXy .marker.cross{stroke:#333333;}#mermaid-svg-XLc2CJdKGi5ZLNXy svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-XLc2CJdKGi5ZLNXy p{margin:0;}#mermaid-svg-XLc2CJdKGi5ZLNXy .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-XLc2CJdKGi5ZLNXy .cluster-label text{fill:#333;}#mermaid-svg-XLc2CJdKGi5ZLNXy .cluster-label span{color:#333;}#mermaid-svg-XLc2CJdKGi5ZLNXy .cluster-label span p{background-color:transparent;}#mermaid-svg-XLc2CJdKGi5ZLNXy .label text,#mermaid-svg-XLc2CJdKGi5ZLNXy span{fill:#333;color:#333;}#mermaid-svg-XLc2CJdKGi5ZLNXy .node rect,#mermaid-svg-XLc2CJdKGi5ZLNXy .node circle,#mermaid-svg-XLc2CJdKGi5ZLNXy .node ellipse,#mermaid-svg-XLc2CJdKGi5ZLNXy .node polygon,#mermaid-svg-XLc2CJdKGi5ZLNXy .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-XLc2CJdKGi5ZLNXy .rough-node .label text,#mermaid-svg-XLc2CJdKGi5ZLNXy .node .label text,#mermaid-svg-XLc2CJdKGi5ZLNXy .image-shape .label,#mermaid-svg-XLc2CJdKGi5ZLNXy .icon-shape .label{text-anchor:middle;}#mermaid-svg-XLc2CJdKGi5ZLNXy .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-XLc2CJdKGi5ZLNXy .rough-node .label,#mermaid-svg-XLc2CJdKGi5ZLNXy .node .label,#mermaid-svg-XLc2CJdKGi5ZLNXy .image-shape .label,#mermaid-svg-XLc2CJdKGi5ZLNXy .icon-shape .label{text-align:center;}#mermaid-svg-XLc2CJdKGi5ZLNXy .node.clickable{cursor:pointer;}#mermaid-svg-XLc2CJdKGi5ZLNXy .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-XLc2CJdKGi5ZLNXy .arrowheadPath{fill:#333333;}#mermaid-svg-XLc2CJdKGi5ZLNXy .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-XLc2CJdKGi5ZLNXy .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-XLc2CJdKGi5ZLNXy .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-XLc2CJdKGi5ZLNXy .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-XLc2CJdKGi5ZLNXy .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-XLc2CJdKGi5ZLNXy .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-XLc2CJdKGi5ZLNXy .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-XLc2CJdKGi5ZLNXy .cluster text{fill:#333;}#mermaid-svg-XLc2CJdKGi5ZLNXy .cluster span{color:#333;}#mermaid-svg-XLc2CJdKGi5ZLNXy div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-XLc2CJdKGi5ZLNXy .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-XLc2CJdKGi5ZLNXy rect.text{fill:none;stroke-width:0;}#mermaid-svg-XLc2CJdKGi5ZLNXy .icon-shape,#mermaid-svg-XLc2CJdKGi5ZLNXy .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-XLc2CJdKGi5ZLNXy .icon-shape p,#mermaid-svg-XLc2CJdKGi5ZLNXy .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-XLc2CJdKGi5ZLNXy .icon-shape rect,#mermaid-svg-XLc2CJdKGi5ZLNXy .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-XLc2CJdKGi5ZLNXy .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-XLc2CJdKGi5ZLNXy .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-XLc2CJdKGi5ZLNXy :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
Redis Cluster
Master Node 1
Slave Node 1
Master Node 2
Slave Node 2
Master Node 3
Slave Node 3
应用客户端
适用场景:
- 绝大多数分布式缓存场景
- 需要复杂数据结构支持的场景
- 高并发、高性能要求的系统
- 分布式锁、消息队列、限流、排行榜等高级功能场景
Redis的单线程模型: Redis使用单线程模型处理所有命令,这是其高性能的核心原因之一:
- 避免了多线程的锁竞争和上下文切换开销
- 代码实现更简单,没有多线程的并发问题
- IO多路复用技术,可以同时处理大量并发连接
- 瓶颈主要在内存和网络,而不是CPU
需要注意的是,Redis 6.0+引入了多线程IO,将网络IO操作改为多线程处理,但命令执行仍然是单线程的,这样在保持兼容性的同时,进一步提升了性能。
2.3.2 Memcached
Memcached是一个高性能的分布式内存对象缓存系统,设计简单,专注于高性能的KV缓存。
核心特性:
- 简单的KV存储模型
- 多线程模型,多核性能优秀
- 内存管理效率高,碎片化少
- 协议简单,客户端丰富
- 不支持持久化,不支持复杂数据结构
适用场景:
- 简单的KV缓存场景
- 对性能要求极高,不需要复杂特性
- 作为会话缓存、页面缓存等
Memcached的Slab内存分配机制: Memcached使用Slab Allocation机制管理内存,将内存划分为不同大小的Slab,每个Slab存储相同大小的对象,这种设计可以有效减少内存碎片,但可能会造成一定的内存浪费。
#mermaid-svg-t74AojPBLxwnZFvl{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-t74AojPBLxwnZFvl .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-t74AojPBLxwnZFvl .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-t74AojPBLxwnZFvl .error-icon{fill:#552222;}#mermaid-svg-t74AojPBLxwnZFvl .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-t74AojPBLxwnZFvl .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-t74AojPBLxwnZFvl .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-t74AojPBLxwnZFvl .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-t74AojPBLxwnZFvl .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-t74AojPBLxwnZFvl .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-t74AojPBLxwnZFvl .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-t74AojPBLxwnZFvl .marker{fill:#333333;stroke:#333333;}#mermaid-svg-t74AojPBLxwnZFvl .marker.cross{stroke:#333333;}#mermaid-svg-t74AojPBLxwnZFvl svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-t74AojPBLxwnZFvl p{margin:0;}#mermaid-svg-t74AojPBLxwnZFvl .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-t74AojPBLxwnZFvl .cluster-label text{fill:#333;}#mermaid-svg-t74AojPBLxwnZFvl .cluster-label span{color:#333;}#mermaid-svg-t74AojPBLxwnZFvl .cluster-label span p{background-color:transparent;}#mermaid-svg-t74AojPBLxwnZFvl .label text,#mermaid-svg-t74AojPBLxwnZFvl span{fill:#333;color:#333;}#mermaid-svg-t74AojPBLxwnZFvl .node rect,#mermaid-svg-t74AojPBLxwnZFvl .node circle,#mermaid-svg-t74AojPBLxwnZFvl .node ellipse,#mermaid-svg-t74AojPBLxwnZFvl .node polygon,#mermaid-svg-t74AojPBLxwnZFvl .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-t74AojPBLxwnZFvl .rough-node .label text,#mermaid-svg-t74AojPBLxwnZFvl .node .label text,#mermaid-svg-t74AojPBLxwnZFvl .image-shape .label,#mermaid-svg-t74AojPBLxwnZFvl .icon-shape .label{text-anchor:middle;}#mermaid-svg-t74AojPBLxwnZFvl .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-t74AojPBLxwnZFvl .rough-node .label,#mermaid-svg-t74AojPBLxwnZFvl .node .label,#mermaid-svg-t74AojPBLxwnZFvl .image-shape .label,#mermaid-svg-t74AojPBLxwnZFvl .icon-shape .label{text-align:center;}#mermaid-svg-t74AojPBLxwnZFvl .node.clickable{cursor:pointer;}#mermaid-svg-t74AojPBLxwnZFvl .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-t74AojPBLxwnZFvl .arrowheadPath{fill:#333333;}#mermaid-svg-t74AojPBLxwnZFvl .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-t74AojPBLxwnZFvl .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-t74AojPBLxwnZFvl .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-t74AojPBLxwnZFvl .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-t74AojPBLxwnZFvl .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-t74AojPBLxwnZFvl .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-t74AojPBLxwnZFvl .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-t74AojPBLxwnZFvl .cluster text{fill:#333;}#mermaid-svg-t74AojPBLxwnZFvl .cluster span{color:#333;}#mermaid-svg-t74AojPBLxwnZFvl div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-t74AojPBLxwnZFvl .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-t74AojPBLxwnZFvl rect.text{fill:none;stroke-width:0;}#mermaid-svg-t74AojPBLxwnZFvl .icon-shape,#mermaid-svg-t74AojPBLxwnZFvl .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-t74AojPBLxwnZFvl .icon-shape p,#mermaid-svg-t74AojPBLxwnZFvl .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-t74AojPBLxwnZFvl .icon-shape rect,#mermaid-svg-t74AojPBLxwnZFvl .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-t74AojPBLxwnZFvl .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-t74AojPBLxwnZFvl .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-t74AojPBLxwnZFvl :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}



