Redisson: don't you have such a powerful framework for implementing distributed locks?

Posted by nitm on Mon, 20 Dec 2021 14:01:17 +0100

Absrtact: Redisson framework is very powerful. Almost all types of distributed locks you can think of can be realized based on Redisson framework.

This article is shared from Huawei cloud community< [high concurrency] do you know that everyone is using Redisson to implement distributed locks!! >, author: Glacier.

Redisson framework is a distributed lock based on Redis. It is very powerful and only needs to be used.

Redisson framework is very powerful. Based on redisson framework, you can realize almost all types of distributed locks you can think of. Here, I will list several types of distributed locks and give an example program to deepen your understanding.

1. Reentrant Lock

Redisson's distributed reentrant lock RLock Java object implements Java util. concurrent. locks. Lock interface, and also supports automatic expiration and unlocking.

public void testReentrantLock(RedissonClient redisson){
	RLock lock = redisson.getLock("anyLock");
	try{
		// 1. The most common usage
		//lock.lock();
		// 2. It supports the expiration unlocking function. It will be unlocked automatically after 10 seconds without calling the unlock method to unlock manually
		//lock.lock(10, TimeUnit.SECONDS);
		// 3. Try to lock, wait up to 3 seconds, and unlock automatically 10 seconds after locking
		boolean res = lock.tryLock(3, 10, TimeUnit.SECONDS);
		if(res){ //success
		// do your business
		}
	} catch (InterruptedException e) {
		e.printStackTrace();
	} finally {
		lock.unlock();
	}
}

Redisson also provides asynchronous execution methods for distributed locks:

public void testAsyncReentrantLock(RedissonClient redisson){
	RLock lock = redisson.getLock("anyLock");
	try{
		lock.lockAsync();
		lock.lockAsync(10, TimeUnit.SECONDS);
		Future<Boolean> res = lock.tryLockAsync(3, 10, TimeUnit.SECONDS);
		if(res.get()){
		// do your business
		}
	} catch (InterruptedException e) {
		e.printStackTrace();
	} catch (ExecutionException e) {
		e.printStackTrace();
	} finally {
		lock.unlock();
	}
}

2. Fair Lock

Redisson distributed reentrant fair lock also implements Java util. concurrent. locks. A RLock object of the lock interface. While providing the automatic expiration unlocking function, it ensures that when multiple redisson client threads request locking at the same time, they are preferentially allocated to the thread that makes the request first.

public void testFairLock(RedissonClient redisson){
	RLock fairLock = redisson.getFairLock("anyLock");
	try{
		// Most common usage
		fairLock.lock();
		// It supports the expiration unlocking function. It will be unlocked automatically after 10 seconds without calling the unlock method to unlock manually
		fairLock.lock(10, TimeUnit.SECONDS);
		// Try to lock, wait for 100 seconds at most, and unlock automatically 10 seconds after locking
		boolean res = fairLock.tryLock(100, 10, TimeUnit.SECONDS);
	} catch (InterruptedException e) {
		e.printStackTrace();
	} finally {
		fairLock.unlock();
	}
}

Redisson also provides asynchronous execution methods for distributed reentrant fair locks:

RLock fairLock = redisson.getFairLock("anyLock");
fairLock.lockAsync();
fairLock.lockAsync(10, TimeUnit.SECONDS);
Future<Boolean> res = fairLock.tryLockAsync(100, 10, TimeUnit.SECONDS);

3. Interlock (MultiLock)

Redisson's RedissonMultiLock object can associate multiple RLock objects into an interlock, and each RLock object instance can come from different redisson instances.

public void testMultiLock(RedissonClient redisson1,RedissonClient redisson2, RedissonClient redisson3){
	RLock lock1 = redisson1.getLock("lock1");
	RLock lock2 = redisson2.getLock("lock2");
	RLock lock3 = redisson3.getLock("lock3");
	RedissonMultiLock lock = new RedissonMultiLock(lock1, lock2, lock3);
	try {
		// Lock at the same time: lock1 lock2 lock3. All locks are locked successfully.
		lock.lock();
		// Try to lock, wait for 100 seconds at most, and unlock automatically 10 seconds after locking
		boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);
	} catch (InterruptedException e) {
		e.printStackTrace();
	} finally {
		lock.unlock();
	}
}

4. Red lock

Redisson's RedissonRedLock object implements the locking algorithm introduced by Redlock. This object can also be used to associate multiple RLock objects into a red lock. Each RLock object instance can come from different redisson instances.

public void testRedLock(RedissonClient redisson1,RedissonClient redisson2, RedissonClient redisson3){
	RLock lock1 = redisson1.getLock("lock1");
	RLock lock2 = redisson2.getLock("lock2");
	RLock lock3 = redisson3.getLock("lock3");
	RedissonRedLock lock = new RedissonRedLock(lock1, lock2, lock3);
	try {
		// Lock at the same time: lock1, lock2, lock3. If the red lock is successfully locked on most nodes, it will be successful.
		lock.lock();
		// Try to lock, wait for 100 seconds at most, and unlock automatically 10 seconds after locking
		boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);
	} catch (InterruptedException e) {
		e.printStackTrace();
	} finally {
		lock.unlock();
	}
}

5. Read write lock (ReadWriteLock)

Redisson's distributed reentrant read / write lock rreadwritelock is implemented in Java util. concurrent. locks. Readwritelock interface. It also supports automatic expiration and unlocking. The object allows multiple read locks at the same time, but there can be at most one write lock.

RReadWriteLock rwlock = redisson.getLock("anyRWLock");
// Most common usage
rwlock.readLock().lock();
// or
rwlock.writeLock().lock();
// Support expiration unlocking function
// Automatic unlocking after 10 seconds
// There is no need to call the unlock method to unlock manually
rwlock.readLock().lock(10, TimeUnit.SECONDS);
// or
rwlock.writeLock().lock(10, TimeUnit.SECONDS);
// Try to lock, wait for 100 seconds at most, and unlock automatically 10 seconds after locking
boolean res = rwlock.readLock().tryLock(100, 10, TimeUnit.SECONDS);
// or
boolean res = rwlock.writeLock().tryLock(100, 10, TimeUnit.SECONDS);
...
lock.unlock();

6. Semaphore

Redisson's distributed Semaphore Java object RSemaphore adopts an interface and usage similar to java.util.concurrent.Semaphore.

RSemaphore semaphore = redisson.getSemaphore("semaphore");
semaphore.acquire();
//or
semaphore.acquireAsync();
semaphore.acquire(23);
semaphore.tryAcquire();
//or
semaphore.tryAcquireAsync();
semaphore.tryAcquire(23, TimeUnit.SECONDS);
//or
semaphore.tryAcquireAsync(23, TimeUnit.SECONDS);
semaphore.release(10);
semaphore.release();
//or
semaphore.releaseAsync();

7. PermitExpirableSemaphore

Redisson's expirable semaphore adds an expiration time to each signal based on the RSemaphore object. Each signal can be identified by an independent ID. when released, it can only be released by submitting this ID.

RPermitExpirableSemaphore semaphore = redisson.getPermitExpirableSemaphore("mySemaphore");
String permitId = semaphore.acquire();
// Get a signal, valid for only 2 seconds.
String permitId = semaphore.acquire(2, TimeUnit.SECONDS);
// ...
semaphore.release(permitId);

8. Lockout (CountDownLatch)

Redisson's CountDownLatch Java object RCountDownLatch adopts an interface and usage similar to java.util.concurrent.CountDownLatch.

RCountDownLatch latch = redisson.getCountDownLatch("anyCountDownLatch");
latch.trySetCount(1);
latch.await();
// In another thread or other JVM
RCountDownLatch latch = redisson.getCountDownLatch("anyCountDownLatch");
latch.countDown();

 

Click focus to learn about Huawei cloud's new technologies for the first time~

Topics: Java