{ //ID of the process private final String clientId = UUID.randomUUID().toString(); private final String registryKey; //prefix of the name of each key private final StringRedisTemplate redisTemplate; private final long expireAfter;.//TTL of each key public RedisLockRegistry(RedisConnectionFactory connectionFactory, String registryKey, long expireAfter) { this.redisTemplate = new StringRedisTemplate(connectionFactory); this.registryKey = registryKey; this.expireAfter = expireAfter; //... } }
of RedisLock final Lock lock = lockRegistry.obtain(“key”); try { if (lock.tryLock(10, TimeUnit.MINUTES)) { // do something } } catch (Exception e) { // log issue } finally { lock.unlock(); } Reference: How To Implement a Spring Distributed Lock
of RedisLock(6.1.3) Process 1 Process 2 Redis Node lock:key expired execute unlock script ok time fail Lock status: owned by process2 acquire Lock status: owned by process1
of RedisLock(6.1.3) Process 1 Process 2 Redis Node lock:key expired execute unlock script ok time fail acquire ok Lock status: owned by process1 Lock status: owned by process2 Lock status: empty execute unlock script
control for threads in a process 2) Redis Node: keep the status of locks 3) Atomic execution: acquire/release locks 4) TTL: Deadlock prevention 5) Redis versions that do not support UNLINK Spring Distributed Lock(Redis) has not(yet) consider: 1) Custom TTL per lock(Issue #3444) 2) Supporting Redis Cluster(Redlock Algorithm)