For AI agents: the complete documentation index is available at /docs/llms.txt, the full documentation bundle is available at /docs/llms-full.txt, and this page is available as Markdown at /docs/interview/redis/index.md.
  • 简体中文
  • 2026
  • 概览

    分布式锁

    • 锁实现。通过 lua 脚本实现:
      • 使用 GET 判断锁是否存在
      • 如果没有,通过 SET key value EX seconds NX 设置锁
      • 如果有,加锁失败,进入等待或阻塞
    • 锁续期。获取锁成功后,启动一个 watch dog 线程,watch dog 每隔一段时间检查一次,如果客户端仍持有锁将锁的 ttl 进行重置。只有在获取锁时显式指定过期时间,才会有锁续期。
    • 锁竞争。当加锁失败时,基于内部的 pub/sub 机制,当锁竞争失败时会订阅锁事件,如果锁被释放会重新竞争,直到到达获取锁的超时时间,返回竞争锁失败