redis中分布式锁_-Redisson分布式锁的实现原理与加锁机制(含可视化) - 伪代码引用:redis
Redisson分布式锁的实现原理与加锁机制(含可视化)
概述
Redisson 基于 Redis 提供了健壮的分布式锁实现,核心由原子化 Lua 脚本、可重入计数、看门狗自动续期、自旋重试、以及在集群环境下的路由与脚本缓存优化共同构成。本文在校正与扩展原有资料的基础上,系统性梳理其原理与工程实践,并附带配色优化的 Mermaid 图以提升可读性。
简介与项目背景
业务痛点:多实例并发下的资源互斥、临界区保护、幂等控制。传统问题:仅依赖 setnx/expire 容易出现“非原子”与“误释放”问题。Redisson 解法:以 Lua 保证原子性,以哈希结构记录重入,以看门狗解决长耗时,以自旋保证获得锁,以 EVALSHA 提升脚本执行性能。
名词解释
客户端ID:通常为 UUID + “:” + threadId,用于标识“谁持有锁”。例如 UUID.randomUUID() 与线程ID拼接。可重入锁:同一线程在持有锁的情况下可以再次进入临界区,计数 +1;释放时计数 -1 直至 0 才真正释放。看门狗(watchdog):锁持有期间的后台续期任务,默认每隔 10 秒检查并延长过期。EVAL/EVALSHA:Redis 执行 Lua 脚本的两种方式;EVALSHA 通过脚本摘要命中缓存,减少传输与编译开销。参见 EVALSHA。TTL/PTTL:键剩余存活时间(毫秒级为 PTTL);用于判定何时可以重试加锁。
架构原理图
#mermaid-svg-dPbPM06iNs5Qman7{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-dPbPM06iNs5Qman7 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-dPbPM06iNs5Qman7 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-dPbPM06iNs5Qman7 .error-icon{fill:#552222;}#mermaid-svg-dPbPM06iNs5Qman7 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-dPbPM06iNs5Qman7 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-dPbPM06iNs5Qman7 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-dPbPM06iNs5Qman7 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-dPbPM06iNs5Qman7 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-dPbPM06iNs5Qman7 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-dPbPM06iNs5Qman7 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-dPbPM06iNs5Qman7 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-dPbPM06iNs5Qman7 .marker.cross{stroke:#333333;}#mermaid-svg-dPbPM06iNs5Qman7 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-dPbPM06iNs5Qman7 p{margin:0;}#mermaid-svg-dPbPM06iNs5Qman7 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-dPbPM06iNs5Qman7 .cluster-label text{fill:#333;}#mermaid-svg-dPbPM06iNs5Qman7 .cluster-label span{color:#333;}#mermaid-svg-dPbPM06iNs5Qman7 .cluster-label span p{background-color:transparent;}#mermaid-svg-dPbPM06iNs5Qman7 .label text,#mermaid-svg-dPbPM06iNs5Qman7 span{fill:#333;color:#333;}#mermaid-svg-dPbPM06iNs5Qman7 .node rect,#mermaid-svg-dPbPM06iNs5Qman7 .node circle,#mermaid-svg-dPbPM06iNs5Qman7 .node ellipse,#mermaid-svg-dPbPM06iNs5Qman7 .node polygon,#mermaid-svg-dPbPM06iNs5Qman7 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-dPbPM06iNs5Qman7 .rough-node .label text,#mermaid-svg-dPbPM06iNs5Qman7 .node .label text,#mermaid-svg-dPbPM06iNs5Qman7 .image-shape .label,#mermaid-svg-dPbPM06iNs5Qman7 .icon-shape .label{text-anchor:middle;}#mermaid-svg-dPbPM06iNs5Qman7 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-dPbPM06iNs5Qman7 .rough-node .label,#mermaid-svg-dPbPM06iNs5Qman7 .node .label,#mermaid-svg-dPbPM06iNs5Qman7 .image-shape .label,#mermaid-svg-dPbPM06iNs5Qman7 .icon-shape .label{text-align:center;}#mermaid-svg-dPbPM06iNs5Qman7 .node.clickable{cursor:pointer;}#mermaid-svg-dPbPM06iNs5Qman7 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-dPbPM06iNs5Qman7 .arrowheadPath{fill:#333333;}#mermaid-svg-dPbPM06iNs5Qman7 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-dPbPM06iNs5Qman7 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-dPbPM06iNs5Qman7 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-dPbPM06iNs5Qman7 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-dPbPM06iNs5Qman7 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-dPbPM06iNs5Qman7 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-dPbPM06iNs5Qman7 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-dPbPM06iNs5Qman7 .cluster text{fill:#333;}#mermaid-svg-dPbPM06iNs5Qman7 .cluster span{color:#333;}#mermaid-svg-dPbPM06iNs5Qman7 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-dPbPM06iNs5Qman7 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-dPbPM06iNs5Qman7 rect.text{fill:none;stroke-width:0;}#mermaid-svg-dPbPM06iNs5Qman7 .icon-shape,#mermaid-svg-dPbPM06iNs5Qman7 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-dPbPM06iNs5Qman7 .icon-shape p,#mermaid-svg-dPbPM06iNs5Qman7 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-dPbPM06iNs5Qman7 .icon-shape rect,#mermaid-svg-dPbPM06iNs5Qman7 .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-dPbPM06iNs5Qman7 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-dPbPM06iNs5Qman7 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-dPbPM06iNs5Qman7 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}#mermaid-svg-dPbPM06iNs5Qman7 .client>*{fill:#4C9AFF!important;stroke:#1B5EAB!important;stroke-width:2!important;color:#fff!important;}#mermaid-svg-dPbPM06iNs5Qman7 .client span{fill:#4C9AFF!important;stroke:#1B5EAB!important;stroke-width:2!important;color:#fff!important;}#mermaid-svg-dPbPM06iNs5Qman7 .client tspan{fill:#fff!important;}#mermaid-svg-dPbPM06iNs5Qman7 .master>*{fill:#AED581!important;stroke:#558B2F!important;stroke-width:2!important;color:#1B5E20!important;}#mermaid-svg-dPbPM06iNs5Qman7 .master span{fill:#AED581!important;stroke:#558B2F!important;stroke-width:2!important;color:#1B5E20!important;}#mermaid-svg-dPbPM06iNs5Qman7 .master tspan{fill:#1B5E20!important;}#mermaid-svg-dPbPM06iNs5Qman7 .slave>*{fill:#E6EE9C!important;stroke:#9E9D24!important;stroke-width:2!important;color:#2E7D32!important;}#mermaid-svg-dPbPM06iNs5Qman7 .slave span{fill:#E6EE9C!important;stroke:#9E9D24!important;stroke-width:2!important;color:#2E7D32!important;}#mermaid-svg-dPbPM06iNs5Qman7 .slave tspan{fill:#2E7D32!important;}#mermaid-svg-dPbPM06iNs5Qman7 .watchdog>*{fill:#FF8A65!important;stroke:#D84315!important;stroke-width:2!important;color:#fff!important;}#mermaid-svg-dPbPM06iNs5Qman7 .watchdog span{fill:#FF8A65!important;stroke:#D84315!important;stroke-width:2!important;color:#fff!important;}#mermaid-svg-dPbPM06iNs5Qman7 .watchdog tspan{fill:#fff!important;}#mermaid-svg-dPbPM06iNs5Qman7 .key>*{fill:#B39DDB!important;stroke:#5E35B1!important;stroke-width:2!important;color:#311B92!important;}#mermaid-svg-dPbPM06iNs5Qman7 .key span{fill:#B39DDB!important;stroke:#5E35B1!important;stroke-width:2!important;color:#311B92!important;}#mermaid-svg-dPbPM06iNs5Qman7 .key tspan{fill:#311B92!important;}
Redis Cluster
spin
spin
Client1 Redisson
Master
Client2 Redisson
Slave
Master
Slave
Watchdog Auto-Renew
Watchdog Auto-Renew
Lock Key K
加锁机制(核心流程校正与拆解)
选路到 Master:在 Redis Cluster 中,客户端根据 key 的哈希槽选择目标 Master 节点,避免跨节点操作。原子加锁 Lua 逻辑:使用 Lua 保证“检查 + 设置 + 过期 + 重入计数”的整体原子性:
若 key 不存在:创建哈希结构,字段为客户端ID,值为重入计数 1;同时设置过期(默认 30s)。若 key 存在且字段为本客户端ID:执行可重入,计数 +1,并刷新过期。若 key 存在但持有者非本客户端:返回剩余 TTL(PTTL),用于指导下一次重试间隔。 自旋与退避:客户端循环尝试加锁,可根据 PTTL 实现“精准睡眠”,避免无意义忙等。看门狗续期:锁成功持有后,后台任务每 10 秒续期一次,确保业务超过初始过期时间仍不丢锁。释放锁:减计数;当减至 0 时删除 key;非持有者不可释放,避免“误删他人锁”。
Lua 逻辑语义解读(无代码版)
exists(KEYS[1]) == 0 → hset(KEYS[1], ARGV[2], 1); pexpire(KEYS[1], ARGV[1]); return nilhexists(KEYS[1], ARGV[2]) == 1 → hincrby(KEYS[1], ARGV[2], 1); pexpire(KEYS[1], ARGV[1]); return nil其他情况 → return pttl(KEYS[1]) 以指导等待时长
EVALSHA 优化机制与伪代码示意
首次执行:发送完整 Lua,Redis 缓存并返回 SHA1。后续执行:优先以 SHA1 执行;若未命中缓存,再回退完整 Lua 并重建缓存。客户端侧:预计算并本地缓存内置 Lua 的 SHA1 摘要,减少运行时计算。集群侧:每个 Master 独立缓存脚本;客户端需针对不同节点维护脚本摘要命中。伪代码引用:redis.evalsha() / redis.eval() / 异常 NoScriptException
续期机制(Watchdog)工作原理
触发条件:当线程成功获得锁且仍在执行临界区。执行频率:默认每 10 秒(可配置),调用 pexpire 刷新 TTL 至设定值(如 30 秒)。结束条件:线程完成业务并释放锁,或线程中断/应用停止(需配合健壮的释放逻辑)。设计要点:续期必须与“持有者身份”绑定,防止非持有者续期导致锁僵死。
可重入锁实现要点
结构选择:使用 Redis Hash,field = 客户端ID,value = 重入计数。加锁路径:本客户端持有时计数 +1;初次持有时设置过期。释放路径:本客户端计数 -1;当计数为 0 → del 整个 key。并发安全:所有操作在 Lua 中原子执行,避免并发条件竞争。
释放锁的安全性
核验持有者:仅当 hexists(KEYS[1], ARGV[2]) == 1 才允许 hincrby -1 或 del。防误删:若不是持有者,直接返回,避免删除他人锁导致“临界区暴露”。最终删除:计数归零时 del key;否则继续保持锁与 TTL。
时序图(获取锁→续期→释放)
Watchdog
Redis Master
Redisson
客户端1
Watchdog
Redis Master
Redisson
客户端1
#mermaid-svg-puBNPUiaS6AMeUqw{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-puBNPUiaS6AMeUqw .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-puBNPUiaS6AMeUqw .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-puBNPUiaS6AMeUqw .error-icon{fill:#552222;}#mermaid-svg-puBNPUiaS6AMeUqw .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-puBNPUiaS6AMeUqw .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-puBNPUiaS6AMeUqw .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-puBNPUiaS6AMeUqw .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-puBNPUiaS6AMeUqw .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-puBNPUiaS6AMeUqw .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-puBNPUiaS6AMeUqw .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-puBNPUiaS6AMeUqw .marker{fill:#333333;stroke:#333333;}#mermaid-svg-puBNPUiaS6AMeUqw .marker.cross{stroke:#333333;}#mermaid-svg-puBNPUiaS6AMeUqw svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-puBNPUiaS6AMeUqw p{margin:0;}#mermaid-svg-puBNPUiaS6AMeUqw .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-puBNPUiaS6AMeUqw text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-puBNPUiaS6AMeUqw .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-puBNPUiaS6AMeUqw .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-puBNPUiaS6AMeUqw .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-puBNPUiaS6AMeUqw .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-puBNPUiaS6AMeUqw #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-puBNPUiaS6AMeUqw .sequenceNumber{fill:white;}#mermaid-svg-puBNPUiaS6AMeUqw #sequencenumber{fill:#333;}#mermaid-svg-puBNPUiaS6AMeUqw #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-puBNPUiaS6AMeUqw .messageText{fill:#333;stroke:none;}#mermaid-svg-puBNPUiaS6AMeUqw .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-puBNPUiaS6AMeUqw .labelText,#mermaid-svg-puBNPUiaS6AMeUqw .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-puBNPUiaS6AMeUqw .loopText,#mermaid-svg-puBNPUiaS6AMeUqw .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-puBNPUiaS6AMeUqw .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-puBNPUiaS6AMeUqw .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-puBNPUiaS6AMeUqw .noteText,#mermaid-svg-puBNPUiaS6AMeUqw .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-puBNPUiaS6AMeUqw .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-puBNPUiaS6AMeUqw .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-puBNPUiaS6AMeUqw .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-puBNPUiaS6AMeUqw .actorPopupMenu{position:absolute;}#mermaid-svg-puBNPUiaS6AMeUqw .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));}#mermaid-svg-puBNPUiaS6AMeUqw .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-puBNPUiaS6AMeUqw .actor-man circle,#mermaid-svg-puBNPUiaS6AMeUqw line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-puBNPUiaS6AMeUqw :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
alt
[K 不存在或可重入]
[被他人持有] 拼多多帮助力入口
请求加锁(K)
1
路由至 Master 并执行 Lua
2
成功(nil)
3
获得锁
4
每10秒 pexpire(K, TTL)
5
返回 PTTL
6
自旋等待(依据 PTTL)
7
释放锁
8
Lua 验证持有者并减计数/删除
9
成功
10
容错与边界场景
时钟漂移与长 GC:续期线程被阻塞可能导致 TTL 过期,需合理设置过期与续期间隔,并监控 GC 暂停。网络分区:客户端误以为释放失败或续期失败,建议增加重试与幂等保护。主从复制延迟:锁写入在主库,读写一致性以主为准,避免从库读取造成误判。进程崩溃:看门狗停止,TTL 到期自动释放,保证锁不会永久占用。阻塞临界区:单点长事务应尽量分解,缩短持锁时间,避免对系统整体吞吐的影响。
性能与实践建议
设置合理 TTL:初始 30s 仅为默认;结合业务耗时与续期频率动态评估。精准退避:根据返回的 PTTL 进行 sleep,降低无效重试。指标与日志:记录“加锁成功率、等待时长、持锁时长、续期次数、释放失败数”等关键指标。统一封装:对外暴露统一的加锁接口,内部包含重试、续期、释放、异常处理与监控。
高级话题与对比
公平锁 vs 非公平锁:Redisson 默认非公平;公平锁需要排队队列语义,吞吐与延迟权衡。读写锁:读锁共享、写锁互斥;适用于读多写少场景,但实现复杂度更高。RedLock(多主多副本):理论争议较大,工程实践需谨慎评估网络分区与延迟,参考官方讨论。
与 Java API 的对应关系(名称引用)
获取与释放:RLock.lock() / RLock.unlock()尝试加锁:RLock.tryLock()(支持等待与租期)自动续期:Watchdog 线程负责续期,结合 pexpire
参考资料与权威文献
Redis 官方 Lua/EVAL/EVALSHA 文档:https://redis.io/docs/latest/develop/reference/scripting/Redis Keyspace TTL/PTTL 文档:https://redis.io/commands/pttl/Redisson 官方文档与源码(GitHub):https://github.com/redisson/redissonRedis Cluster 介绍与槽位:https://redis.io/docs/latest/operate/cluster/RedLock 原论文与讨论(antirez):https://redis.io/docs/latest/develop/use/patterns/distributed-locks/
速记口(便于复述与面试)
一句概括:Lua 原子 + Hash 重入 + Watchdog 续期 + EVALSHA 缓存 + 自旋退避 + 主库路由。关键流程:exists → hset/hexists → hincrby → pexpire → 返回 pttl → 自旋 → 释放减计数/删除。风险点:长 GC/网络分区/主从延迟/错误释放;监控与退避不可少。配置要点:合理 TTL 与续期频率;精准自旋;健壮释放;统一封装与度量。
结语
Redisson 分布式锁以工程化手段解决了“原子性、持有者识别、长耗时续期、重入与释放安全”等关键问题。理解其在集群中的脚本缓存与路由细节、在边界场景下的容错策略,才能做到知其然并知其所以然,在真实生产环境中稳定发挥作用。