
# GeminiDB Redis 基于lua实现分布式锁
在分布式系统中，为了保证数据的一致性和防止并发冲突，经常需要使用分布式锁来确保同一时间只有一个进程或线程能够执行特定的代码段。
本文将介绍如何利用lua实现分布式锁。
#### Redis分布式锁原理
Redis分布式锁的基本原理是利用Redis的原子命令来创建一个锁。最简单的实现方式是使用SETNX命令，这个命令只有在键不存在时才会设置键的值，如果键已经存在，命令不会执行任何操作。这样，第一个获取锁的进程会成功设置键的值，而后续尝试获取锁的进程都会失败，直到锁被释放。
为了防止锁永远不被释放（例如，持有锁的进程崩溃），通常会给锁设置一个过期时间，这可以通过EXPIRE命令来实现。Redis 2.6.12版本之后，SET命令增加了EX和NX选项，可以在设置键的同时设置过期时间，这个操作是原子的。
- 加锁
可以通过下面的命令实现加锁
```
SET resource_name my_random_value NX PX 30000
```
NX参数会检查key的存在性，当key不存在, 即没有人持有锁，才会加锁成功；
PX参数用于设置锁过期时间，单位毫秒，这个参数必须设置，以确保及时锁的持有者异常退出了，锁过期即自动解锁，以确保死锁问题不会发生。
- 解锁
解锁的动作为复杂一些，解锁的时候需要检查你是否是锁的持有者，检查通过才能解锁，为了串行化地执行这个解锁的过程， 需要依赖lua脚本来完成
lua脚本：
```
if redis.call("get",KEYS[1]) == ARGV[1] then
    return redis.call("del",KEYS[1])
else
    return 0
end
```
这个lua脚本需要搭配EVAL命令来执行，示例如下：
```
EVAL 'if redis.call("get",KEYS[1]) == ARGV[1] then return redis.call("del",KEYS[1]) else return 0 end' 1 resource_name my_random_value
```
通过这个脚本，就能保证只有解锁的持有者才能解锁成功。
- 方案分析
  上述的方案简单易用，但功能有点不足：
  - 锁过期释放，业务没执行完；
  
  - 锁不可重入；
  
  - 没有通知机制，需要轮询抢锁，CPU耗费较多；
    
 
所以在生产应用场景下，还是比较推荐直接使用成熟的Redis分布式锁类库来实现分布式锁，以平衡功能及性能；
下面以redisson为例，介绍一下Redis分布式锁类库的用法。
#### 通过Redisson实现分布式锁
Redisson是一个基于Redis的Java客户端，提供了分布式锁的功能。分布式锁是一种用于在分布式系统中同步访问共享资源的机制。Redisson通过Redis的原子操作来实现分布锁，确保只有一个客户端能够同时访问某个资源。
Redisson的分布式锁主要有以下几个特点：
- 高效性：利用Redis的高性能和内存存储特性，分布式锁操作非常快速。
- 简单易用：提供了丰富的API，使开发者可以轻松地在Java应用中使用分布式锁。
- 可靠性：Redisson的分布式锁具有高可靠性，即使在网络分区或节点宕机的情况下，也能保证锁的正确性。
使用示例
```
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
public class LockExamples {
    public static void main(String[] args) {
        // 创建Redisson客户端
        Config config = new Config();
        config.useSingleServer().setAddress("redis://127.0.0.1:7200");
        RedissonClient redisson = Redisson.create(config);
        // 获取分布式锁
        RLock lock = redisson.getLock("myLock");
        try {
            // 加锁
            lock.lock();
            System.out.println("Lock acquired, executing critical section...");
            // 执行需要加锁的代码
            // ...
            System.out.println("Critical section executed, releasing lock...");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 释放锁
            lock.unlock();
        }
        // 关闭Redisson客户端
        redisson.shutdown();
    }
}
```
#### 更多的分布式锁实现推荐
鉴于Redisson是Java生态的，其实分布式锁在各语言下都有了成熟的实现，这里直接引用[redis官网](https://redis.io/docs/latest/develop/use/patterns/distributed-locks/)的推荐：
- [Redlock-rb](https://github.com/antirez/redlock-rb) (Ruby implementation). There is also a [fork of Redlock-rb](https://github.com/leandromoreira/redlock-rb) that adds a gem for easy distribution.
- [RedisQueuedLocks](https://github.com/0exp/redis_queued_locks) (Ruby implementation).
- [Redlock-py](https://github.com/SPSCommerce/redlock-py) (Python implementation).
- [Pottery](https://github.com/brainix/pottery#redlock) (Python implementation).
- [Aioredlock](https://github.com/joanvila/aioredlock) (Asyncio Python implementation).
- [RedisMutex](https://github.com/malkusch/lock#redismutex) (PHP implementation with both [Redis extension](https://github.com/phpredis/phpredis) and [Predis library](https://github.com/predis/predis) clients support).
- [Redlock-php](https://github.com/ronnylt/redlock-php) (PHP implementation).
- [cheprasov/php-redis-lock](https://github.com/cheprasov/php-redis-lock) (PHP library for locks).
- [rtckit/react-redlock](https://github.com/rtckit/reactphp-redlock) (Async PHP implementation).
- [Redsync](https://github.com/go-redsync/redsync) (Go implementation).
- [Redisson](https://github.com/mrniko/redisson) (Java implementation).
- [Redis::DistLock](https://github.com/sbertrang/redis-distlock) (Perl implementation).
- [Redlock-cpp](https://github.com/jacket-code/redlock-cpp) (C++ implementation).
- [Redis-plus-plus](https://github.com/sewenew/redis-plus-plus/#redlock) (C++ implementation).
- [Redlock-cs](https://github.com/kidfashion/redlock-cs) (C#/.NET implementation).
- [RedLock.net](https://github.com/samcook/RedLock.net) (C#/.NET implementation). Includes async and lock extension support.
- [ScarletLock](https://github.com/aidapsibr/scarletlock) (C# .NET implementation with configurable datastore).
- [Redlock4Net](https://github.com/LiZhenNet/Redlock4Net) (C# .NET implementation).
- [node-redlock](https://github.com/mike-marcacci/node-redlock) (NodeJS implementation). Includes support for lock extension.
- [simple-redis-mutex](https://github.com/AmrSaber/simple-redis-mutex) (Node.js implementation) Available as an [NPM package](https://www.npmjs.com/package/simple-redis-mutex).
- [Deno DLM](https://github.com/oslabs-beta/Deno-Redlock) (Deno implementation)
- [Rslock](https://github.com/hexcowboy/rslock) (Rust implementation). Includes async and lock extension support.
 
