Redis过期删除策略深度剖析:惰性删除与定期删除的协同艺术
1. 引言:为什么需要过期删除
Redis作为内存数据库,内存资源是有限且昂贵的。为了高效管理内存,Redis提供了EXPIRE命令为键设置生存时间(TTL)。然而,如何高效地删除这些过期键,是一个经典的计算机科学问题。
如果采用定时删除(每个键设置定时器),CPU开销太大;如果完全不删除,内存会被过期数据撑爆。Redis采用了一种精妙的组合策略:
#publish-mermaid-1784561275815-0{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;}}#publish-mermaid-1784561275815-0 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561275815-0 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561275815-0 .error-icon{fill:#552222;}#publish-mermaid-1784561275815-0 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1784561275815-0 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1784561275815-0 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1784561275815-0 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1784561275815-0 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1784561275815-0 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1784561275815-0 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1784561275815-0 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1784561275815-0 .marker.cross{stroke:#333333;}#publish-mermaid-1784561275815-0 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1784561275815-0 p{margin:0;}#publish-mermaid-1784561275815-0 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#publish-mermaid-1784561275815-0 .cluster-label text{fill:#333;}#publish-mermaid-1784561275815-0 .cluster-label span{color:#333;}#publish-mermaid-1784561275815-0 .cluster-label span p{background-color:transparent;}#publish-mermaid-1784561275815-0 .label text,#publish-mermaid-1784561275815-0 span{fill:#333;color:#333;}#publish-mermaid-1784561275815-0 .node rect,#publish-mermaid-1784561275815-0 .node circle,#publish-mermaid-1784561275815-0 .node ellipse,#publish-mermaid-1784561275815-0 .node polygon,#publish-mermaid-1784561275815-0 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1784561275815-0 .rough-node .label text,#publish-mermaid-1784561275815-0 .node .label text,#publish-mermaid-1784561275815-0 .image-shape .label,#publish-mermaid-1784561275815-0 .icon-shape .label{text-anchor:middle;}#publish-mermaid-1784561275815-0 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-1784561275815-0 .rough-node .label,#publish-mermaid-1784561275815-0 .node .label,#publish-mermaid-1784561275815-0 .image-shape .label,#publish-mermaid-1784561275815-0 .icon-shape .label{text-align:center;}#publish-mermaid-1784561275815-0 .node.clickable{cursor:pointer;}#publish-mermaid-1784561275815-0 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-1784561275815-0 .arrowheadPath{fill:#333333;}#publish-mermaid-1784561275815-0 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-1784561275815-0 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-1784561275815-0 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1784561275815-0 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-1784561275815-0 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1784561275815-0 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-1784561275815-0 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-1784561275815-0 .cluster text{fill:#333;}#publish-mermaid-1784561275815-0 .cluster span{color:#333;}#publish-mermaid-1784561275815-0 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;}#publish-mermaid-1784561275815-0 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-1784561275815-0 rect.text{fill:none;stroke-width:0;}#publish-mermaid-1784561275815-0 .icon-shape,#publish-mermaid-1784561275815-0 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1784561275815-0 .icon-shape p,#publish-mermaid-1784561275815-0 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-1784561275815-0 .icon-shape .label rect,#publish-mermaid-1784561275815-0 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1784561275815-0 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-1784561275815-0 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-1784561275815-0 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1784561275815-0 [data-look=\”neo\”].node rect,#publish-mermaid-1784561275815-0 [data-look=\”neo\”].cluster rect,#publish-mermaid-1784561275815-0 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561275815-0 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1784561275815-0 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1784561275815-0 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561275815-0 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1784561275815-0 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561275815-0 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1784561275815-0 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561275815-0 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561275815-0 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
过期删除策略
惰性删除
定期删除
CPU友好
内存可能堆积
主动清理
CPU/内存平衡
本文将深入剖析这两种策略的实现原理、执行流程、参数调优以及生产环境的监控实践。
—
2. 策略一:惰性删除(Lazy Expiration)
2.1 核心原理
惰性删除的策略非常直接:不主动删除过期键,而是在访问时检查。
#publish-mermaid-1784561275941-1{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;}}#publish-mermaid-1784561275941-1 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561275941-1 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561275941-1 .error-icon{fill:#552222;}#publish-mermaid-1784561275941-1 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1784561275941-1 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1784561275941-1 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1784561275941-1 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1784561275941-1 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1784561275941-1 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1784561275941-1 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1784561275941-1 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1784561275941-1 .marker.cross{stroke:#333333;}#publish-mermaid-1784561275941-1 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1784561275941-1 p{margin:0;}#publish-mermaid-1784561275941-1 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#publish-mermaid-1784561275941-1 .cluster-label text{fill:#333;}#publish-mermaid-1784561275941-1 .cluster-label span{color:#333;}#publish-mermaid-1784561275941-1 .cluster-label span p{background-color:transparent;}#publish-mermaid-1784561275941-1 .label text,#publish-mermaid-1784561275941-1 span{fill:#333;color:#333;}#publish-mermaid-1784561275941-1 .node rect,#publish-mermaid-1784561275941-1 .node circle,#publish-mermaid-1784561275941-1 .node ellipse,#publish-mermaid-1784561275941-1 .node polygon,#publish-mermaid-1784561275941-1 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1784561275941-1 .rough-node .label text,#publish-mermaid-1784561275941-1 .node .label text,#publish-mermaid-1784561275941-1 .image-shape .label,#publish-mermaid-1784561275941-1 .icon-shape .label{text-anchor:middle;}#publish-mermaid-1784561275941-1 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-1784561275941-1 .rough-node .label,#publish-mermaid-1784561275941-1 .node .label,#publish-mermaid-1784561275941-1 .image-shape .label,#publish-mermaid-1784561275941-1 .icon-shape .label{text-align:center;}#publish-mermaid-1784561275941-1 .node.clickable{cursor:pointer;}#publish-mermaid-1784561275941-1 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-1784561275941-1 .arrowheadPath{fill:#333333;}#publish-mermaid-1784561275941-1 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-1784561275941-1 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-1784561275941-1 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1784561275941-1 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-1784561275941-1 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1784561275941-1 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-1784561275941-1 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-1784561275941-1 .cluster text{fill:#333;}#publish-mermaid-1784561275941-1 .cluster span{color:#333;}#publish-mermaid-1784561275941-1 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;}#publish-mermaid-1784561275941-1 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-1784561275941-1 rect.text{fill:none;stroke-width:0;}#publish-mermaid-1784561275941-1 .icon-shape,#publish-mermaid-1784561275941-1 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1784561275941-1 .icon-shape p,#publish-mermaid-1784561275941-1 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-1784561275941-1 .icon-shape .label rect,#publish-mermaid-1784561275941-1 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1784561275941-1 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-1784561275941-1 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-1784561275941-1 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1784561275941-1 [data-look=\”neo\”].node rect,#publish-mermaid-1784561275941-1 [data-look=\”neo\”].cluster rect,#publish-mermaid-1784561275941-1 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561275941-1 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1784561275941-1 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1784561275941-1 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561275941-1 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1784561275941-1 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561275941-1 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1784561275941-1 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561275941-1 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561275941-1 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}读/写命令否是未过期已过期
客户端请求
操作类型
查找Key
Key是否存在?
返回nil/报错
检查过期时间
正常返回数据
调用expireIfNeeded
从数据库删除Key
传播删除到从库/AOF
返回nil
2.2 触发惰性删除的命令
并非所有命令都会触发惰性删除,只有访问到具体Key的命令才会:
| 触发删除的命令 | 不触发删除的命令 |
|—————|—————–|
| GET, SET, HSET, HGET | EXISTS, TYPE, RENAME |
| LPUSH, RPUSH, SADD | KEYS, SCAN |
| INCR, DECR, APPEND | INFO, DBSIZE |
| DEL(显式删除) | PING, SELECT |
// 惰性删除核心函数(源码简化)
int expireIfNeeded(redisDb *db, robj *key) {
// 获取过期时间戳
long long when = getExpire(db, key);
if (when < 0) return 0; // 永不过期
// 如果服务器正在加载数据,暂不处理
if (server.loading) return 0;
// 当前时间超过过期时间
if (mstime() > when) {
// 删除Key
dbDelete(db, key);
// 传播删除命令到从库和AOF
propagateExpire(db, key);
// 更新统计
server.stat_expiredkeys++;
return 1;
}
return 0;
}
2.3 惰性删除的优缺点
| 维度 | 说明 |
|——|——|
| ✅ 优点 | CPU开销极小,只在访问时检查 |
| ✅ 优点 | 实现简单,不影响正常请求延迟 |
| ❌ 缺点 | 过期但未被访问的Key会一直占用内存("僵尸Key") |
| ❌ 缺点 | 在极端情况下可能导致内存泄漏 |
#publish-mermaid-1784561276183-2{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;}}#publish-mermaid-1784561276183-2 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561276183-2 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561276183-2 .error-icon{fill:#552222;}#publish-mermaid-1784561276183-2 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1784561276183-2 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1784561276183-2 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1784561276183-2 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1784561276183-2 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1784561276183-2 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1784561276183-2 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1784561276183-2 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1784561276183-2 .marker.cross{stroke:#333333;}#publish-mermaid-1784561276183-2 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1784561276183-2 p{margin:0;}#publish-mermaid-1784561276183-2 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#publish-mermaid-1784561276183-2 .cluster-label text{fill:#333;}#publish-mermaid-1784561276183-2 .cluster-label span{color:#333;}#publish-mermaid-1784561276183-2 .cluster-label span p{background-color:transparent;}#publish-mermaid-1784561276183-2 .label text,#publish-mermaid-1784561276183-2 span{fill:#333;color:#333;}#publish-mermaid-1784561276183-2 .node rect,#publish-mermaid-1784561276183-2 .node circle,#publish-mermaid-1784561276183-2 .node ellipse,#publish-mermaid-1784561276183-2 .node polygon,#publish-mermaid-1784561276183-2 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1784561276183-2 .rough-node .label text,#publish-mermaid-1784561276183-2 .node .label text,#publish-mermaid-1784561276183-2 .image-shape .label,#publish-mermaid-1784561276183-2 .icon-shape .label{text-anchor:middle;}#publish-mermaid-1784561276183-2 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-1784561276183-2 .rough-node .label,#publish-mermaid-1784561276183-2 .node .label,#publish-mermaid-1784561276183-2 .image-shape .label,#publish-mermaid-1784561276183-2 .icon-shape .label{text-align:center;}#publish-mermaid-1784561276183-2 .node.clickable{cursor:pointer;}#publish-mermaid-1784561276183-2 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-1784561276183-2 .arrowheadPath{fill:#333333;}#publish-mermaid-1784561276183-2 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-1784561276183-2 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-1784561276183-2 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1784561276183-2 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-1784561276183-2 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1784561276183-2 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-1784561276183-2 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-1784561276183-2 .cluster text{fill:#333;}#publish-mermaid-1784561276183-2 .cluster span{color:#333;}#publish-mermaid-1784561276183-2 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;}#publish-mermaid-1784561276183-2 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-1784561276183-2 rect.text{fill:none;stroke-width:0;}#publish-mermaid-1784561276183-2 .icon-shape,#publish-mermaid-1784561276183-2 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1784561276183-2 .icon-shape p,#publish-mermaid-1784561276183-2 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-1784561276183-2 .icon-shape .label rect,#publish-mermaid-1784561276183-2 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1784561276183-2 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-1784561276183-2 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-1784561276183-2 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1784561276183-2 [data-look=\”neo\”].node rect,#publish-mermaid-1784561276183-2 [data-look=\”neo\”].cluster rect,#publish-mermaid-1784561276183-2 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276183-2 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1784561276183-2 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1784561276183-2 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276183-2 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1784561276183-2 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276183-2 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1784561276183-2 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276183-2 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276183-2 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}惰性删除问题示意无人访问无人访问无人访问
Key1 过期
一直占用内存
Key2 过期
一直占用内存
Key3 过期
一直占用内存
内存持续增长
—
3. 策略二:定期删除(Active Expiration)
3.1 核心原理
定期删除通过定时任务主动扫描过期Key,弥补惰性删除的不足。Redis默认每秒执行10次(由hz参数控制)。
#publish-mermaid-1784561276290-3{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;}}#publish-mermaid-1784561276290-3 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561276290-3 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561276290-3 .error-icon{fill:#552222;}#publish-mermaid-1784561276290-3 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1784561276290-3 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1784561276290-3 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1784561276290-3 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1784561276290-3 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1784561276290-3 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1784561276290-3 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1784561276290-3 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1784561276290-3 .marker.cross{stroke:#333333;}#publish-mermaid-1784561276290-3 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1784561276290-3 p{margin:0;}#publish-mermaid-1784561276290-3 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#publish-mermaid-1784561276290-3 .cluster-label text{fill:#333;}#publish-mermaid-1784561276290-3 .cluster-label span{color:#333;}#publish-mermaid-1784561276290-3 .cluster-label span p{background-color:transparent;}#publish-mermaid-1784561276290-3 .label text,#publish-mermaid-1784561276290-3 span{fill:#333;color:#333;}#publish-mermaid-1784561276290-3 .node rect,#publish-mermaid-1784561276290-3 .node circle,#publish-mermaid-1784561276290-3 .node ellipse,#publish-mermaid-1784561276290-3 .node polygon,#publish-mermaid-1784561276290-3 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1784561276290-3 .rough-node .label text,#publish-mermaid-1784561276290-3 .node .label text,#publish-mermaid-1784561276290-3 .image-shape .label,#publish-mermaid-1784561276290-3 .icon-shape .label{text-anchor:middle;}#publish-mermaid-1784561276290-3 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-1784561276290-3 .rough-node .label,#publish-mermaid-1784561276290-3 .node .label,#publish-mermaid-1784561276290-3 .image-shape .label,#publish-mermaid-1784561276290-3 .icon-shape .label{text-align:center;}#publish-mermaid-1784561276290-3 .node.clickable{cursor:pointer;}#publish-mermaid-1784561276290-3 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-1784561276290-3 .arrowheadPath{fill:#333333;}#publish-mermaid-1784561276290-3 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-1784561276290-3 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-1784561276290-3 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1784561276290-3 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-1784561276290-3 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1784561276290-3 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-1784561276290-3 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-1784561276290-3 .cluster text{fill:#333;}#publish-mermaid-1784561276290-3 .cluster span{color:#333;}#publish-mermaid-1784561276290-3 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;}#publish-mermaid-1784561276290-3 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-1784561276290-3 rect.text{fill:none;stroke-width:0;}#publish-mermaid-1784561276290-3 .icon-shape,#publish-mermaid-1784561276290-3 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1784561276290-3 .icon-shape p,#publish-mermaid-1784561276290-3 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-1784561276290-3 .icon-shape .label rect,#publish-mermaid-1784561276290-3 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1784561276290-3 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-1784561276290-3 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-1784561276290-3 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1784561276290-3 [data-look=\”neo\”].node rect,#publish-mermaid-1784561276290-3 [data-look=\”neo\”].cluster rect,#publish-mermaid-1784561276290-3 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276290-3 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1784561276290-3 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1784561276290-3 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276290-3 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1784561276290-3 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276290-3 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1784561276290-3 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276290-3 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276290-3 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}是否是否否是
serverCron定时任务每秒10次
执行activeExpireCycle
遍历所有数据库
从当前DB随机抽取20个Key(仅限设置了过期时间的)
统计其中过期Key数量
删除所有过期Key
过期比例 > 25%?
继续抽取下一批20个
进入下一个DB
总执行时间 > 25ms?
立即结束,下轮继续
所有DB遍历完?
本次定期删除结束
3.2 核心参数详解
| 参数 | 默认值 | 说明 |
|——|——–|——|
| hz | 10 | 每秒执行serverCron任务的次数 |
| ACTIVE_EXPIRE_CYCLE_KEYS_PER_LOOP | 20 | 每次抽样检查的Key数量 |
| ACTIVE_EXPIRE_CYCLE_SLOW_TIME_PERC | 25 | 最大CPU使用时间占比(25ms/100ms) |
| active-expire-effort | 1(1-10) | 努力程度,值越大清理越积极 |
3.3 定期删除的两种模式
#publish-mermaid-1784561276438-4{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;}}#publish-mermaid-1784561276438-4 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561276438-4 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561276438-4 .error-icon{fill:#552222;}#publish-mermaid-1784561276438-4 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1784561276438-4 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1784561276438-4 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1784561276438-4 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1784561276438-4 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1784561276438-4 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1784561276438-4 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1784561276438-4 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1784561276438-4 .marker.cross{stroke:#333333;}#publish-mermaid-1784561276438-4 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1784561276438-4 p{margin:0;}#publish-mermaid-1784561276438-4 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#publish-mermaid-1784561276438-4 .cluster-label text{fill:#333;}#publish-mermaid-1784561276438-4 .cluster-label span{color:#333;}#publish-mermaid-1784561276438-4 .cluster-label span p{background-color:transparent;}#publish-mermaid-1784561276438-4 .label text,#publish-mermaid-1784561276438-4 span{fill:#333;color:#333;}#publish-mermaid-1784561276438-4 .node rect,#publish-mermaid-1784561276438-4 .node circle,#publish-mermaid-1784561276438-4 .node ellipse,#publish-mermaid-1784561276438-4 .node polygon,#publish-mermaid-1784561276438-4 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1784561276438-4 .rough-node .label text,#publish-mermaid-1784561276438-4 .node .label text,#publish-mermaid-1784561276438-4 .image-shape .label,#publish-mermaid-1784561276438-4 .icon-shape .label{text-anchor:middle;}#publish-mermaid-1784561276438-4 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-1784561276438-4 .rough-node .label,#publish-mermaid-1784561276438-4 .node .label,#publish-mermaid-1784561276438-4 .image-shape .label,#publish-mermaid-1784561276438-4 .icon-shape .label{text-align:center;}#publish-mermaid-1784561276438-4 .node.clickable{cursor:pointer;}#publish-mermaid-1784561276438-4 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-1784561276438-4 .arrowheadPath{fill:#333333;}#publish-mermaid-1784561276438-4 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-1784561276438-4 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-1784561276438-4 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1784561276438-4 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-1784561276438-4 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1784561276438-4 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-1784561276438-4 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-1784561276438-4 .cluster text{fill:#333;}#publish-mermaid-1784561276438-4 .cluster span{color:#333;}#publish-mermaid-1784561276438-4 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;}#publish-mermaid-1784561276438-4 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-1784561276438-4 rect.text{fill:none;stroke-width:0;}#publish-mermaid-1784561276438-4 .icon-shape,#publish-mermaid-1784561276438-4 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1784561276438-4 .icon-shape p,#publish-mermaid-1784561276438-4 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-1784561276438-4 .icon-shape .label rect,#publish-mermaid-1784561276438-4 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1784561276438-4 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-1784561276438-4 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-1784561276438-4 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1784561276438-4 [data-look=\”neo\”].node rect,#publish-mermaid-1784561276438-4 [data-look=\”neo\”].cluster rect,#publish-mermaid-1784561276438-4 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276438-4 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1784561276438-4 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1784561276438-4 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276438-4 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1784561276438-4 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276438-4 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1784561276438-4 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276438-4 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276438-4 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}hz=30
每秒30次
每次8.3ms
CPU占用约25%
过期删除更及时
hz=10 默认
每秒10次
每次25ms
CPU占用约25%
过期删除较温和
快模式触发条件:当慢模式中过期Key比例超过25%时,会进入快模式加速清理。
3.4 源码核心逻辑
void activeExpireCycle(int type) {
static unsigned int current_db = 0;
int dbs_per_call = 16; // 每次最多检查16个DB
long long start = ustime(), timelimit;
// 计算时间限制(慢模式25ms,快模式1ms)
if (type == ACTIVE_EXPIRE_CYCLE_FAST)
timelimit = 1000; // 1ms
else
timelimit = 25000; // 25ms
for (int j = 0; j < dbs_per_call; j++) {
redisDb *db = server.db + current_db;
current_db++;
do {
// 1. 随机抽取20个设置了过期时间的Key
expired = 0;
for (i = 0; i < 20; i++) {
de = dbFindExpires(db);
if (de && isExpired(db, de)) {
dbDelete(db, de);
expired++;
}
}
// 2. 如果过期比例 > 25%,继续抽取
} while (expired > 20 * 0.25 &&
(ustime() – start) < timelimit);
// 3. 如果超时,跳出循环
if ((ustime() – start) > timelimit) break;
}
}
3.5 active-expire-effort 参数详解
Redis 6.0+ 引入了active-expire-effort参数(1-10),控制定期删除的激进程度:
| 值 | 每次抽样数 | 执行时间上限 | 适用场景 |
|—-|———–|————-|———|
| 1(默认) | 20 | 25ms | 通用场景 |
| 2-3 | 20-30 | 30-40ms | 有较多过期Key |
| 4-6 | 30-40 | 40-50ms | 内存紧张,需积极清理 |
| 7-10 | 40-50 | 50-60ms | 极端场景,CPU换内存 |
# 调高active-expire-effort加速过期清理
active-expire-effort 3
—
4. 两种策略的协同工作流
4.1 完整执行时序图
C2内存定时任务(serverCron)Redis主线程客户端C2内存定时任务(serverCron)Redis主线程客户端#publish-mermaid-1784561276638-5{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;}}#publish-mermaid-1784561276638-5 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561276638-5 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561276638-5 .error-icon{fill:#552222;}#publish-mermaid-1784561276638-5 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1784561276638-5 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1784561276638-5 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1784561276638-5 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1784561276638-5 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1784561276638-5 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1784561276638-5 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1784561276638-5 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1784561276638-5 .marker.cross{stroke:#333333;}#publish-mermaid-1784561276638-5 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1784561276638-5 p{margin:0;}#publish-mermaid-1784561276638-5 .actor{stroke:#9370DB;fill:#ECECFF;stroke-width:1;}#publish-mermaid-1784561276638-5 rect.actor.outer-path[data-look=\”neo\”]{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276638-5 rect.note[data-look=\”neo\”]{stroke:#aaaa33;fill:#fff5ad;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276638-5 text.actor>tspan{fill:black;stroke:none;}#publish-mermaid-1784561276638-5 .actor-line{stroke:#9370DB;}#publish-mermaid-1784561276638-5 .innerArc{stroke-width:1.5;stroke-dasharray:none;}#publish-mermaid-1784561276638-5 .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#publish-mermaid-1784561276638-5 .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#publish-mermaid-1784561276638-5 [id$=\”-arrowhead\”] path{fill:#333;stroke:#333;}#publish-mermaid-1784561276638-5 .sequenceNumber{fill:white;}#publish-mermaid-1784561276638-5 [id$=\”-sequencenumber\”]{fill:#333;}#publish-mermaid-1784561276638-5 [id$=\”-crosshead\”] path{fill:#333;stroke:#333;}#publish-mermaid-1784561276638-5 .messageText{fill:#333;stroke:none;}#publish-mermaid-1784561276638-5 .labelBox{stroke:#9370DB;fill:#ECECFF;filter:none;}#publish-mermaid-1784561276638-5 .labelText,#publish-mermaid-1784561276638-5 .labelText>tspan{fill:black;stroke:none;}#publish-mermaid-1784561276638-5 .loopText,#publish-mermaid-1784561276638-5 .loopText>tspan{fill:black;stroke:none;}#publish-mermaid-1784561276638-5 .sectionTitle,#publish-mermaid-1784561276638-5 .sectionTitle>tspan{fill:black;stroke:none;}#publish-mermaid-1784561276638-5 .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:#9370DB;fill:#9370DB;}#publish-mermaid-1784561276638-5 .note{stroke:#aaaa33;fill:#fff5ad;}#publish-mermaid-1784561276638-5 .noteText,#publish-mermaid-1784561276638-5 .noteText>tspan{fill:black;stroke:none;font-weight:normal;}#publish-mermaid-1784561276638-5 .activation0{fill:#f4f4f4;stroke:#666;}#publish-mermaid-1784561276638-5 .activation1{fill:#f4f4f4;stroke:#666;}#publish-mermaid-1784561276638-5 .activation2{fill:#f4f4f4;stroke:#666;}#publish-mermaid-1784561276638-5 .actorPopupMenu{position:absolute;}#publish-mermaid-1784561276638-5 .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#publish-mermaid-1784561276638-5 .actor-man circle,#publish-mermaid-1784561276638-5 line{fill:#ECECFF;stroke-width:2px;}#publish-mermaid-1784561276638-5 g rect.rect{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));stroke:#9370DB;}#publish-mermaid-1784561276638-5 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1784561276638-5 [data-look=\”neo\”].node rect,#publish-mermaid-1784561276638-5 [data-look=\”neo\”].cluster rect,#publish-mermaid-1784561276638-5 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276638-5 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1784561276638-5 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1784561276638-5 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276638-5 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1784561276638-5 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276638-5 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1784561276638-5 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276638-5 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276638-5 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}时间线10秒后,Key已过期但仍在内存或者:惰性删除先行同时触发定期删除的抽样SET key EX 10 "value"存储Key,设置过期时间10秒OK每秒10次定期删除任务随机抽取20个Key检查发现Key已过期删除Key,释放内存GET key(延迟访问)检查Key是否存在不存在(已被定期删除)nilGET another_key(未过期)检查发现已过期删除Keynil
4.2 组合策略的优势互补
#publish-mermaid-1784561276707-6{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;}}#publish-mermaid-1784561276707-6 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561276707-6 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561276707-6 .error-icon{fill:#552222;}#publish-mermaid-1784561276707-6 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1784561276707-6 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1784561276707-6 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1784561276707-6 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1784561276707-6 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1784561276707-6 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1784561276707-6 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1784561276707-6 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1784561276707-6 .marker.cross{stroke:#333333;}#publish-mermaid-1784561276707-6 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1784561276707-6 p{margin:0;}#publish-mermaid-1784561276707-6 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#publish-mermaid-1784561276707-6 .cluster-label text{fill:#333;}#publish-mermaid-1784561276707-6 .cluster-label span{color:#333;}#publish-mermaid-1784561276707-6 .cluster-label span p{background-color:transparent;}#publish-mermaid-1784561276707-6 .label text,#publish-mermaid-1784561276707-6 span{fill:#333;color:#333;}#publish-mermaid-1784561276707-6 .node rect,#publish-mermaid-1784561276707-6 .node circle,#publish-mermaid-1784561276707-6 .node ellipse,#publish-mermaid-1784561276707-6 .node polygon,#publish-mermaid-1784561276707-6 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1784561276707-6 .rough-node .label text,#publish-mermaid-1784561276707-6 .node .label text,#publish-mermaid-1784561276707-6 .image-shape .label,#publish-mermaid-1784561276707-6 .icon-shape .label{text-anchor:middle;}#publish-mermaid-1784561276707-6 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-1784561276707-6 .rough-node .label,#publish-mermaid-1784561276707-6 .node .label,#publish-mermaid-1784561276707-6 .image-shape .label,#publish-mermaid-1784561276707-6 .icon-shape .label{text-align:center;}#publish-mermaid-1784561276707-6 .node.clickable{cursor:pointer;}#publish-mermaid-1784561276707-6 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-1784561276707-6 .arrowheadPath{fill:#333333;}#publish-mermaid-1784561276707-6 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-1784561276707-6 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-1784561276707-6 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1784561276707-6 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-1784561276707-6 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1784561276707-6 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-1784561276707-6 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-1784561276707-6 .cluster text{fill:#333;}#publish-mermaid-1784561276707-6 .cluster span{color:#333;}#publish-mermaid-1784561276707-6 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;}#publish-mermaid-1784561276707-6 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-1784561276707-6 rect.text{fill:none;stroke-width:0;}#publish-mermaid-1784561276707-6 .icon-shape,#publish-mermaid-1784561276707-6 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1784561276707-6 .icon-shape p,#publish-mermaid-1784561276707-6 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-1784561276707-6 .icon-shape .label rect,#publish-mermaid-1784561276707-6 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1784561276707-6 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-1784561276707-6 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-1784561276707-6 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1784561276707-6 [data-look=\”neo\”].node rect,#publish-mermaid-1784561276707-6 [data-look=\”neo\”].cluster rect,#publish-mermaid-1784561276707-6 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276707-6 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1784561276707-6 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1784561276707-6 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276707-6 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1784561276707-6 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276707-6 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1784561276707-6 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276707-6 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276707-6 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}定期删除惰性删除弥补控制
优点: CPU开销低
缺点: 内存可能堆积
优点: 主动清理过期Key
缺点: CPU消耗可控
最终效果: CPU和内存平衡
—
5. 过期删除与内存淘汰的关系
5.1 两者的区别
#publish-mermaid-1784561276791-7{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;}}#publish-mermaid-1784561276791-7 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561276791-7 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561276791-7 .error-icon{fill:#552222;}#publish-mermaid-1784561276791-7 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1784561276791-7 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1784561276791-7 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1784561276791-7 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1784561276791-7 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1784561276791-7 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1784561276791-7 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1784561276791-7 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1784561276791-7 .marker.cross{stroke:#333333;}#publish-mermaid-1784561276791-7 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1784561276791-7 p{margin:0;}#publish-mermaid-1784561276791-7 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#publish-mermaid-1784561276791-7 .cluster-label text{fill:#333;}#publish-mermaid-1784561276791-7 .cluster-label span{color:#333;}#publish-mermaid-1784561276791-7 .cluster-label span p{background-color:transparent;}#publish-mermaid-1784561276791-7 .label text,#publish-mermaid-1784561276791-7 span{fill:#333;color:#333;}#publish-mermaid-1784561276791-7 .node rect,#publish-mermaid-1784561276791-7 .node circle,#publish-mermaid-1784561276791-7 .node ellipse,#publish-mermaid-1784561276791-7 .node polygon,#publish-mermaid-1784561276791-7 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1784561276791-7 .rough-node .label text,#publish-mermaid-1784561276791-7 .node .label text,#publish-mermaid-1784561276791-7 .image-shape .label,#publish-mermaid-1784561276791-7 .icon-shape .label{text-anchor:middle;}#publish-mermaid-1784561276791-7 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-1784561276791-7 .rough-node .label,#publish-mermaid-1784561276791-7 .node .label,#publish-mermaid-1784561276791-7 .image-shape .label,#publish-mermaid-1784561276791-7 .icon-shape .label{text-align:center;}#publish-mermaid-1784561276791-7 .node.clickable{cursor:pointer;}#publish-mermaid-1784561276791-7 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-1784561276791-7 .arrowheadPath{fill:#333333;}#publish-mermaid-1784561276791-7 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-1784561276791-7 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-1784561276791-7 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1784561276791-7 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-1784561276791-7 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1784561276791-7 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-1784561276791-7 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-1784561276791-7 .cluster text{fill:#333;}#publish-mermaid-1784561276791-7 .cluster span{color:#333;}#publish-mermaid-1784561276791-7 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;}#publish-mermaid-1784561276791-7 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-1784561276791-7 rect.text{fill:none;stroke-width:0;}#publish-mermaid-1784561276791-7 .icon-shape,#publish-mermaid-1784561276791-7 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1784561276791-7 .icon-shape p,#publish-mermaid-1784561276791-7 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-1784561276791-7 .icon-shape .label rect,#publish-mermaid-1784561276791-7 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1784561276791-7 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-1784561276791-7 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-1784561276791-7 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1784561276791-7 [data-look=\”neo\”].node rect,#publish-mermaid-1784561276791-7 [data-look=\”neo\”].cluster rect,#publish-mermaid-1784561276791-7 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276791-7 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1784561276791-7 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1784561276791-7 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276791-7 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1784561276791-7 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276791-7 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1784561276791-7 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276791-7 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276791-7 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
内存管理
过期删除
内存淘汰
针对设置了EXPIRE的Key
主动/被动清理过期数据
目的是释放过期数据
针对所有Key(取决于策略)
内存达到maxmemory时触发
目的是防止OOM
| 对比维度 | 过期删除 | 内存淘汰 |
|———|———|———|
| 触发条件 | 基于TTL时间 | 内存达到maxmemory |
| 作用对象 | 仅设置了EXPIRE的Key | 取决于淘汰策略 |
| 执行时机 | 持续运行 | 内存超限时 |
| 目的 | 释放过期数据 | 腾出内存空间 |
5.2 协同工作流程
#publish-mermaid-1784561276873-8{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;}}#publish-mermaid-1784561276873-8 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561276873-8 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561276873-8 .error-icon{fill:#552222;}#publish-mermaid-1784561276873-8 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1784561276873-8 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1784561276873-8 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1784561276873-8 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1784561276873-8 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1784561276873-8 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1784561276873-8 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1784561276873-8 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1784561276873-8 .marker.cross{stroke:#333333;}#publish-mermaid-1784561276873-8 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1784561276873-8 p{margin:0;}#publish-mermaid-1784561276873-8 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#publish-mermaid-1784561276873-8 .cluster-label text{fill:#333;}#publish-mermaid-1784561276873-8 .cluster-label span{color:#333;}#publish-mermaid-1784561276873-8 .cluster-label span p{background-color:transparent;}#publish-mermaid-1784561276873-8 .label text,#publish-mermaid-1784561276873-8 span{fill:#333;color:#333;}#publish-mermaid-1784561276873-8 .node rect,#publish-mermaid-1784561276873-8 .node circle,#publish-mermaid-1784561276873-8 .node ellipse,#publish-mermaid-1784561276873-8 .node polygon,#publish-mermaid-1784561276873-8 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1784561276873-8 .rough-node .label text,#publish-mermaid-1784561276873-8 .node .label text,#publish-mermaid-1784561276873-8 .image-shape .label,#publish-mermaid-1784561276873-8 .icon-shape .label{text-anchor:middle;}#publish-mermaid-1784561276873-8 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-1784561276873-8 .rough-node .label,#publish-mermaid-1784561276873-8 .node .label,#publish-mermaid-1784561276873-8 .image-shape .label,#publish-mermaid-1784561276873-8 .icon-shape .label{text-align:center;}#publish-mermaid-1784561276873-8 .node.clickable{cursor:pointer;}#publish-mermaid-1784561276873-8 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-1784561276873-8 .arrowheadPath{fill:#333333;}#publish-mermaid-1784561276873-8 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-1784561276873-8 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-1784561276873-8 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1784561276873-8 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-1784561276873-8 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1784561276873-8 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-1784561276873-8 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-1784561276873-8 .cluster text{fill:#333;}#publish-mermaid-1784561276873-8 .cluster span{color:#333;}#publish-mermaid-1784561276873-8 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;}#publish-mermaid-1784561276873-8 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-1784561276873-8 rect.text{fill:none;stroke-width:0;}#publish-mermaid-1784561276873-8 .icon-shape,#publish-mermaid-1784561276873-8 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1784561276873-8 .icon-shape p,#publish-mermaid-1784561276873-8 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-1784561276873-8 .icon-shape .label rect,#publish-mermaid-1784561276873-8 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1784561276873-8 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-1784561276873-8 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-1784561276873-8 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1784561276873-8 [data-look=\”neo\”].node rect,#publish-mermaid-1784561276873-8 [data-look=\”neo\”].cluster rect,#publish-mermaid-1784561276873-8 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276873-8 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1784561276873-8 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1784561276873-8 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276873-8 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1784561276873-8 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276873-8 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1784561276873-8 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276873-8 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561276873-8 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}是否
写请求
分配内存
used_memory > maxmemory?
触发内存淘汰
继续执行
定时任务
定期删除过期Key
释放部分内存
读请求
惰性删除检查
删除过期Key释放内存
—
6. 生产环境配置与调优
6.1 核心配置参数
# ====== 过期删除相关配置 ======
# serverCron频率(每秒执行次数)
# 默认10,取值范围1-500
hz 10
# 主动过期清理的努力程度(1-10)
# 默认1,值越大清理越积极
active-expire-effort 1
# 最大内存及淘汰策略
maxmemory 4gb
maxmemory-policy allkeys-lru
6.2 hz参数的影响
#publish-mermaid-1784561277003-9{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;}}#publish-mermaid-1784561277003-9 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561277003-9 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561277003-9 .error-icon{fill:#552222;}#publish-mermaid-1784561277003-9 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1784561277003-9 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1784561277003-9 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1784561277003-9 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1784561277003-9 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1784561277003-9 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1784561277003-9 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1784561277003-9 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1784561277003-9 .marker.cross{stroke:#333333;}#publish-mermaid-1784561277003-9 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1784561277003-9 p{margin:0;}#publish-mermaid-1784561277003-9 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#publish-mermaid-1784561277003-9 .cluster-label text{fill:#333;}#publish-mermaid-1784561277003-9 .cluster-label span{color:#333;}#publish-mermaid-1784561277003-9 .cluster-label span p{background-color:transparent;}#publish-mermaid-1784561277003-9 .label text,#publish-mermaid-1784561277003-9 span{fill:#333;color:#333;}#publish-mermaid-1784561277003-9 .node rect,#publish-mermaid-1784561277003-9 .node circle,#publish-mermaid-1784561277003-9 .node ellipse,#publish-mermaid-1784561277003-9 .node polygon,#publish-mermaid-1784561277003-9 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1784561277003-9 .rough-node .label text,#publish-mermaid-1784561277003-9 .node .label text,#publish-mermaid-1784561277003-9 .image-shape .label,#publish-mermaid-1784561277003-9 .icon-shape .label{text-anchor:middle;}#publish-mermaid-1784561277003-9 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-1784561277003-9 .rough-node .label,#publish-mermaid-1784561277003-9 .node .label,#publish-mermaid-1784561277003-9 .image-shape .label,#publish-mermaid-1784561277003-9 .icon-shape .label{text-align:center;}#publish-mermaid-1784561277003-9 .node.clickable{cursor:pointer;}#publish-mermaid-1784561277003-9 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-1784561277003-9 .arrowheadPath{fill:#333333;}#publish-mermaid-1784561277003-9 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-1784561277003-9 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-1784561277003-9 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1784561277003-9 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-1784561277003-9 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1784561277003-9 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-1784561277003-9 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-1784561277003-9 .cluster text{fill:#333;}#publish-mermaid-1784561277003-9 .cluster span{color:#333;}#publish-mermaid-1784561277003-9 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;}#publish-mermaid-1784561277003-9 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-1784561277003-9 rect.text{fill:none;stroke-width:0;}#publish-mermaid-1784561277003-9 .icon-shape,#publish-mermaid-1784561277003-9 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1784561277003-9 .icon-shape p,#publish-mermaid-1784561277003-9 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-1784561277003-9 .icon-shape .label rect,#publish-mermaid-1784561277003-9 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1784561277003-9 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-1784561277003-9 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-1784561277003-9 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1784561277003-9 [data-look=\”neo\”].node rect,#publish-mermaid-1784561277003-9 [data-look=\”neo\”].cluster rect,#publish-mermaid-1784561277003-9 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561277003-9 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1784561277003-9 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1784561277003-9 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561277003-9 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1784561277003-9 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561277003-9 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1784561277003-9 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561277003-9 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561277003-9 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}hz=30
每秒30次
每次8.3ms
CPU占用约25%
过期删除更及时
hz=10(默认)
每秒10次
每次25ms
CPU占用约25%
过期删除较温和
| hz值 | CPU占用 | 过期删除及时性 | 适用场景 |
|——|———|—————|———|
| 5 | 低 | 较低 | 低负载环境 |
| 10(默认) | 中等 | 中等 | 通用场景 |
| 20 | 较高 | 较高 | 高并发写入 |
| 50+ | 高 | 很高 | 谨慎使用(CPU敏感) |
6.3 监控指标
# 1. 查看过期Key统计
INFO stats
# expired_keys: 1234567 # 累计过期删除数量
# evicted_keys: 0 # 内存淘汰数量
# 2. 查看数据库Key数量
INFO keyspace
# db0: keys=1000000, expires=200000, avg_ttl=3600
# 3. 查看当前时间与过期Key情况
DBSIZE
# 对比expires字段,评估过期Key比例
6.4 监控告警规则
# Prometheus告警规则
groups:
– name: redis_expire
rules:
# 过期Key堆积告警
– alert: RedisExpiredKeysHigh
expr: redis_db_keys{expires="true"} / redis_db_keys > 0.3
annotations:
summary: "过期Key占比超过30%"
# 内存淘汰告警
– alert: RedisEviction
expr: rate(redis_evicted_keys_total[5m]) > 0
annotations:
summary: "发生内存淘汰,可能需要扩容"
—
7. 常见问题与解决方案
7.1 大量Key同时过期导致CPU飙升
问题现象:
- 业务高峰,大量Key在同一秒过期
- CPU突然飙升至100%
- 请求延迟增加
#publish-mermaid-1784561277101-10{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;}}#publish-mermaid-1784561277101-10 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561277101-10 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561277101-10 .error-icon{fill:#552222;}#publish-mermaid-1784561277101-10 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1784561277101-10 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1784561277101-10 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1784561277101-10 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1784561277101-10 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1784561277101-10 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1784561277101-10 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1784561277101-10 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1784561277101-10 .marker.cross{stroke:#333333;}#publish-mermaid-1784561277101-10 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1784561277101-10 p{margin:0;}#publish-mermaid-1784561277101-10 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#publish-mermaid-1784561277101-10 .cluster-label text{fill:#333;}#publish-mermaid-1784561277101-10 .cluster-label span{color:#333;}#publish-mermaid-1784561277101-10 .cluster-label span p{background-color:transparent;}#publish-mermaid-1784561277101-10 .label text,#publish-mermaid-1784561277101-10 span{fill:#333;color:#333;}#publish-mermaid-1784561277101-10 .node rect,#publish-mermaid-1784561277101-10 .node circle,#publish-mermaid-1784561277101-10 .node ellipse,#publish-mermaid-1784561277101-10 .node polygon,#publish-mermaid-1784561277101-10 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1784561277101-10 .rough-node .label text,#publish-mermaid-1784561277101-10 .node .label text,#publish-mermaid-1784561277101-10 .image-shape .label,#publish-mermaid-1784561277101-10 .icon-shape .label{text-anchor:middle;}#publish-mermaid-1784561277101-10 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-1784561277101-10 .rough-node .label,#publish-mermaid-1784561277101-10 .node .label,#publish-mermaid-1784561277101-10 .image-shape .label,#publish-mermaid-1784561277101-10 .icon-shape .label{text-align:center;}#publish-mermaid-1784561277101-10 .node.clickable{cursor:pointer;}#publish-mermaid-1784561277101-10 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-1784561277101-10 .arrowheadPath{fill:#333333;}#publish-mermaid-1784561277101-10 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-1784561277101-10 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-1784561277101-10 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1784561277101-10 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-1784561277101-10 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1784561277101-10 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-1784561277101-10 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-1784561277101-10 .cluster text{fill:#333;}#publish-mermaid-1784561277101-10 .cluster span{color:#333;}#publish-mermaid-1784561277101-10 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;}#publish-mermaid-1784561277101-10 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-1784561277101-10 rect.text{fill:none;stroke-width:0;}#publish-mermaid-1784561277101-10 .icon-shape,#publish-mermaid-1784561277101-10 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1784561277101-10 .icon-shape p,#publish-mermaid-1784561277101-10 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-1784561277101-10 .icon-shape .label rect,#publish-mermaid-1784561277101-10 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1784561277101-10 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-1784561277101-10 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-1784561277101-10 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1784561277101-10 [data-look=\”neo\”].node rect,#publish-mermaid-1784561277101-10 [data-look=\”neo\”].cluster rect,#publish-mermaid-1784561277101-10 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561277101-10 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1784561277101-10 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1784561277101-10 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561277101-10 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1784561277101-10 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561277101-10 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1784561277101-10 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561277101-10 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561277101-10 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
凌晨0点
100万个Key同时过期
定期删除集中扫描
CPU飙升
服务抖动
解决方案:
// 解决方案1: 过期时间加随机偏移
long ttl = 3600 + ThreadLocalRandom.current().nextInt(300); // 3600±150秒
redisTemplate.expire("key:" + id, ttl, TimeUnit.SECONDS);
// 解决方案2: 分散过期时间
// 按业务ID取模,分散到不同时间点
long baseTTL = 3600;
long randomOffset = (id % 100) * 30; // 0-2970秒随机偏移
redisTemplate.expire("key:" + id, baseTTL + randomOffset, TimeUnit.SECONDS);
配置优化:
# 适当调低hz值,减少CPU占用
hz 8
# 降低active-expire-effort
active-expire-effort 1
7.2 过期Key堆积导致内存不足
问题现象:
- 内存使用持续增长
- expired_keys增长缓慢
- 频繁触发内存淘汰
#publish-mermaid-1784561277175-11{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;}}#publish-mermaid-1784561277175-11 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561277175-11 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561277175-11 .error-icon{fill:#552222;}#publish-mermaid-1784561277175-11 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1784561277175-11 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1784561277175-11 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1784561277175-11 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1784561277175-11 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1784561277175-11 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1784561277175-11 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1784561277175-11 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1784561277175-11 .marker.cross{stroke:#333333;}#publish-mermaid-1784561277175-11 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1784561277175-11 p{margin:0;}#publish-mermaid-1784561277175-11 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#publish-mermaid-1784561277175-11 .cluster-label text{fill:#333;}#publish-mermaid-1784561277175-11 .cluster-label span{color:#333;}#publish-mermaid-1784561277175-11 .cluster-label span p{background-color:transparent;}#publish-mermaid-1784561277175-11 .label text,#publish-mermaid-1784561277175-11 span{fill:#333;color:#333;}#publish-mermaid-1784561277175-11 .node rect,#publish-mermaid-1784561277175-11 .node circle,#publish-mermaid-1784561277175-11 .node ellipse,#publish-mermaid-1784561277175-11 .node polygon,#publish-mermaid-1784561277175-11 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1784561277175-11 .rough-node .label text,#publish-mermaid-1784561277175-11 .node .label text,#publish-mermaid-1784561277175-11 .image-shape .label,#publish-mermaid-1784561277175-11 .icon-shape .label{text-anchor:middle;}#publish-mermaid-1784561277175-11 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-1784561277175-11 .rough-node .label,#publish-mermaid-1784561277175-11 .node .label,#publish-mermaid-1784561277175-11 .image-shape .label,#publish-mermaid-1784561277175-11 .icon-shape .label{text-align:center;}#publish-mermaid-1784561277175-11 .node.clickable{cursor:pointer;}#publish-mermaid-1784561277175-11 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-1784561277175-11 .arrowheadPath{fill:#333333;}#publish-mermaid-1784561277175-11 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-1784561277175-11 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-1784561277175-11 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1784561277175-11 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-1784561277175-11 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1784561277175-11 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-1784561277175-11 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-1784561277175-11 .cluster text{fill:#333;}#publish-mermaid-1784561277175-11 .cluster span{color:#333;}#publish-mermaid-1784561277175-11 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;}#publish-mermaid-1784561277175-11 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-1784561277175-11 rect.text{fill:none;stroke-width:0;}#publish-mermaid-1784561277175-11 .icon-shape,#publish-mermaid-1784561277175-11 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1784561277175-11 .icon-shape p,#publish-mermaid-1784561277175-11 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-1784561277175-11 .icon-shape .label rect,#publish-mermaid-1784561277175-11 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1784561277175-11 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-1784561277175-11 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-1784561277175-11 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1784561277175-11 [data-look=\”neo\”].node rect,#publish-mermaid-1784561277175-11 [data-look=\”neo\”].cluster rect,#publish-mermaid-1784561277175-11 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561277175-11 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1784561277175-11 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1784561277175-11 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561277175-11 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1784561277175-11 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561277175-11 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1784561277175-11 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561277175-11 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561277175-11 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
过期Key堆积
内存持续增长
maxmemory上限
触发淘汰策略
可能淘汰未过期Key
缓存命中率下降
解决方案:
| 方案 | 操作 | 适用场景 |
|——|——|———|
| 提高hz | CONFIG SET hz 20 | CPU有余量 |
| 提高active-expire-effort | CONFIG SET active-expire-effort 3 | 积极清理 |
| 手动扫描 | SCAN + TTL + DEL | 一次性清理 |
| 内存扩容 | 增加maxmemory | 根本解决 |
7.3 主从复制中的过期删除
重要原则:从库不主动执行过期删除,全部依赖主库同步。
ClientSlaveMasterClientSlaveMaster#publish-mermaid-1784561277279-12{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;}}#publish-mermaid-1784561277279-12 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561277279-12 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561277279-12 .error-icon{fill:#552222;}#publish-mermaid-1784561277279-12 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1784561277279-12 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1784561277279-12 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1784561277279-12 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1784561277279-12 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1784561277279-12 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1784561277279-12 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1784561277279-12 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1784561277279-12 .marker.cross{stroke:#333333;}#publish-mermaid-1784561277279-12 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1784561277279-12 p{margin:0;}#publish-mermaid-1784561277279-12 .actor{stroke:#9370DB;fill:#ECECFF;stroke-width:1;}#publish-mermaid-1784561277279-12 rect.actor.outer-path[data-look=\”neo\”]{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561277279-12 rect.note[data-look=\”neo\”]{stroke:#aaaa33;fill:#fff5ad;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561277279-12 text.actor>tspan{fill:black;stroke:none;}#publish-mermaid-1784561277279-12 .actor-line{stroke:#9370DB;}#publish-mermaid-1784561277279-12 .innerArc{stroke-width:1.5;stroke-dasharray:none;}#publish-mermaid-1784561277279-12 .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#publish-mermaid-1784561277279-12 .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#publish-mermaid-1784561277279-12 [id$=\”-arrowhead\”] path{fill:#333;stroke:#333;}#publish-mermaid-1784561277279-12 .sequenceNumber{fill:white;}#publish-mermaid-1784561277279-12 [id$=\”-sequencenumber\”]{fill:#333;}#publish-mermaid-1784561277279-12 [id$=\”-crosshead\”] path{fill:#333;stroke:#333;}#publish-mermaid-1784561277279-12 .messageText{fill:#333;stroke:none;}#publish-mermaid-1784561277279-12 .labelBox{stroke:#9370DB;fill:#ECECFF;filter:none;}#publish-mermaid-1784561277279-12 .labelText,#publish-mermaid-1784561277279-12 .labelText>tspan{fill:black;stroke:none;}#publish-mermaid-1784561277279-12 .loopText,#publish-mermaid-1784561277279-12 .loopText>tspan{fill:black;stroke:none;}#publish-mermaid-1784561277279-12 .sectionTitle,#publish-mermaid-1784561277279-12 .sectionTitle>tspan{fill:black;stroke:none;}#publish-mermaid-1784561277279-12 .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:#9370DB;fill:#9370DB;}#publish-mermaid-1784561277279-12 .note{stroke:#aaaa33;fill:#fff5ad;}#publish-mermaid-1784561277279-12 .noteText,#publish-mermaid-1784561277279-12 .noteText>tspan{fill:black;stroke:none;font-weight:normal;}#publish-mermaid-1784561277279-12 .activation0{fill:#f4f4f4;stroke:#666;}#publish-mermaid-1784561277279-12 .activation1{fill:#f4f4f4;stroke:#666;}#publish-mermaid-1784561277279-12 .activation2{fill:#f4f4f4;stroke:#666;}#publish-mermaid-1784561277279-12 .actorPopupMenu{position:absolute;}#publish-mermaid-1784561277279-12 .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#publish-mermaid-1784561277279-12 .actor-man circle,#publish-mermaid-1784561277279-12 line{fill:#ECECFF;stroke-width:2px;}#publish-mermaid-1784561277279-12 g rect.rect{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));stroke:#9370DB;}#publish-mermaid-1784561277279-12 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1784561277279-12 [data-look=\”neo\”].node rect,#publish-mermaid-1784561277279-12 [data-look=\”neo\”].cluster rect,#publish-mermaid-1784561277279-12 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561277279-12 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1784561277279-12 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1784561277279-12 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561277279-12 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1784561277279-12 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561277279-12 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1784561277279-12 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561277279-12 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561277279-12 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}Key过期从库不检查过期时间惰性/定期删除DEL命令(通过复制流)执行DEL删除KeyGET key(从库读取)可能返回过期数据!
注意事项:
- 从库读请求可能返回过期数据(主库未及时同步DEL)
- 从库的replica-read-only应保持yes
- 监控主从延迟,避免过期数据长时间滞留从库
—
8. 性能测试与经验数据
8.1 不同策略下的CPU/内存对比
| 场景 | 惰性删除CPU | 定期删除CPU | 内存占用 |
|——|————|————|———|
| 100%访问活跃Key | 低(5%) | 中(10%) | 低 |
| 50%访问过期Key | 中(15%) | 中(12%) | 中 |
| 无访问过期Key | 低(2%) | 中(15%) | 高(堆积) |
| 大量同时过期 | 高(30%) | 高(40%) | 逐步释放 |
8.2 经验调优建议
# 根据业务特征调整
业务特征:
读多写少:
hz: 10
active-expire-effort: 1
写多读少:
hz: 15-20
active-expire-effort: 2-3
大量短TTL Key:
hz: 20-30
active-expire-effort: 3-5
内存紧张:
hz: 15
active-expire-effort: 3-5
maxmemory-policy: volatile-lru
—
9. 总结:平衡的艺术
#publish-mermaid-1784561277320-13{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;}}#publish-mermaid-1784561277320-13 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561277320-13 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1784561277320-13 .error-icon{fill:#552222;}#publish-mermaid-1784561277320-13 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1784561277320-13 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1784561277320-13 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1784561277320-13 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1784561277320-13 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1784561277320-13 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1784561277320-13 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1784561277320-13 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1784561277320-13 .marker.cross{stroke:#333333;}#publish-mermaid-1784561277320-13 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1784561277320-13 p{margin:0;}#publish-mermaid-1784561277320-13 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#publish-mermaid-1784561277320-13 .cluster-label text{fill:#333;}#publish-mermaid-1784561277320-13 .cluster-label span{color:#333;}#publish-mermaid-1784561277320-13 .cluster-label span p{background-color:transparent;}#publish-mermaid-1784561277320-13 .label text,#publish-mermaid-1784561277320-13 span{fill:#333;color:#333;}#publish-mermaid-1784561277320-13 .node rect,#publish-mermaid-1784561277320-13 .node circle,#publish-mermaid-1784561277320-13 .node ellipse,#publish-mermaid-1784561277320-13 .node polygon,#publish-mermaid-1784561277320-13 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1784561277320-13 .rough-node .label text,#publish-mermaid-1784561277320-13 .node .label text,#publish-mermaid-1784561277320-13 .image-shape .label,#publish-mermaid-1784561277320-13 .icon-shape .label{text-anchor:middle;}#publish-mermaid-1784561277320-13 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-1784561277320-13 .rough-node .label,#publish-mermaid-1784561277320-13 .node .label,#publish-mermaid-1784561277320-13 .image-shape .label,#publish-mermaid-1784561277320-13 .icon-shape .label{text-align:center;}#publish-mermaid-1784561277320-13 .node.clickable{cursor:pointer;}#publish-mermaid-1784561277320-13 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-1784561277320-13 .arrowheadPath{fill:#333333;}#publish-mermaid-1784561277320-13 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-1784561277320-13 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-1784561277320-13 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1784561277320-13 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-1784561277320-13 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1784561277320-13 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-1784561277320-13 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-1784561277320-13 .cluster text{fill:#333;}#publish-mermaid-1784561277320-13 .cluster span{color:#333;}#publish-mermaid-1784561277320-13 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;}#publish-mermaid-1784561277320-13 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-1784561277320-13 rect.text{fill:none;stroke-width:0;}#publish-mermaid-1784561277320-13 .icon-shape,#publish-mermaid-1784561277320-13 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1784561277320-13 .icon-shape p,#publish-mermaid-1784561277320-13 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-1784561277320-13 .icon-shape .label rect,#publish-mermaid-1784561277320-13 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1784561277320-13 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-1784561277320-13 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-1784561277320-13 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1784561277320-13 [data-look=\”neo\”].node rect,#publish-mermaid-1784561277320-13 [data-look=\”neo\”].cluster rect,#publish-mermaid-1784561277320-13 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561277320-13 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1784561277320-13 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1784561277320-13 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561277320-13 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1784561277320-13 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561277320-13 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1784561277320-13 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561277320-13 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1784561277320-13 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}Redis过期删除哲学
CPU资源
惰性删除
定期删除
内存资源
定期删除
CPU友好,内存可能堆积
CPU/内存平衡
两者互补
生产环境稳定运行
核心要点速记
┌─────────────────────────────────────────────────────────────────┐
│ 过期删除策略核心口诀 │
├─────────────────────────────────────────────────────────────────┤
│ 惰性删除不主动,访问检查即清除; │
│ 定期任务每秒跑,抽样检查二十个; │
│ 过期比例超25%,继续抽取不停歇; │
│ 时间限制25ms,CPU内存两不误; │
│ 两者配合补短板,生产环境稳如山。 │
└─────────────────────────────────────────────────────────────────┘
最终建议
—


