What is redis used for? In addition to caching, the first thing that comes to mind is distributed locks. Nowadays, eight-part essays are basically a must-ask in interviews. We know that stand-alone systems can use locks such as synchronized or Lock provided by JUC, but these will

2024/04/2808:15:33 hotcomm 1178
What is

redis used for? In addition to caching, the first thing that comes to mind is distributed locks. Nowadays, eight-part essays are basically a must-ask in interviews.

We know that stand-alone systems can use locks provided by JUC such as synchronized or Lock, but these will not work in distributed systems.

A reliable distributed lock needs to meet the following conditions:

  • exclusivity. There can only be one and only one thread holding
  • high availability at any time. In a redis cluster environment, acquiring locks and locks cannot occur because a node is down. In case of failure to release the lock,
  • prevents deadlock and eliminates deadlock. There must be a timeout control mechanism or undo operation, and there is a complete termination and escape plan.
  • does not grab randomly. It cannot unlock other people's locks privately. It can only lock it and release it by itself.
  • reentrancy, if the same thread on the same node obtains the lock, it can also obtain the lock again.

Classic oversold problem

Let’s look at a flash sale case. There is a product with 10 items in stock. The inventory is reduced every time

private void buyGoods() {//Get the remaining inventory of the productInteger result = (Integer) redisTemplate.opsForValue ().get("goods:001");int goodsNumber = result == null ? 0 : result;if (goodsNumber 0) {//If there is still inventory, the quantity will be reduced by one and stored in real inventoryint realNumber = goodsNumber - 1 ;redisTemplate.opsForValue().set("goods:001", realNumber);System.out.println(Thread.currentThread().getName() + " The product has been successfully sold out and there is still: " + realNumber + " ");} else {System.out.println(Thread.currentThread().getName() + "The product has been sold out, welcome to visit next time");}}

What is redis used for? In addition to caching, the first thing that comes to mind is distributed locks. Nowadays, eight-part essays are basically a must-ask in interviews. We know that stand-alone systems can use locks such as synchronized or Lock provided by JUC, but these will - DayDayNews

Simulate ten people to grab

public void testOverBuy() throws Exception {for (int i = 0; i 10; i++) {new Thread(() - buyGoods()).start();}}

What is redis used for? In addition to caching, the first thing that comes to mind is distributed locks. Nowadays, eight-part essays are basically a must-ask in interviews. We know that stand-alone systems can use locks such as synchronized or Lock provided by JUC, but these will - DayDayNews

Looking at the output, ten people grabbed the inventory but it was still 9. The reason is inventory reduction and The save operation is not an atomic operation and requires a synchronized lock or a lock. However, this is not possible in a distributed environment. Redis needs to be used to implement the lock.

Thread-13 has successfully flash-sold the product, and there are still: 9 pieces left. Thread-12 has successfully flash-sold the product, and now has: 9 pieces left. Thread-6 has successfully flash-sold the product, and now there are still: 9 pieces. Thread-7 has been successful. Flash sale products, there are still: 9 pieces Thread-8 has been successfully flash sale, and there are still 9 pieces left at this time Thread-5 has been successfully flash sale, and there are still: 9 Thread-4 products have been successfully flash sale, at this time Remaining: 9 items Thread-11 has successfully sold out the products, and there are still 9 items left at this time. Thread-10 has successfully sold out the products, and there are still 9 items left. Thread-9 has successfully sold out the products, and there are still 9 items left at this time. 

What is redis used for? In addition to caching, the first thing that comes to mind is distributed locks. Nowadays, eight-part essays are basically a must-ask in interviews. We know that stand-alone systems can use locks such as synchronized or Lock provided by JUC, but these will - DayDayNews

redis implements distributed lock

Let's see how to implement distributed lock. First, we must meet the exclusivity. Use the setnx command of redis to set the value for the key. If 1 is returned, it means the lock is successful, and 0 is returned to indicate that the lock has been occupied. In order To prevent deadlock, add an expiration time, so that the lock can be released even if the business thread hangs up. Release the lock in the finally statement block

private void testRedisLock() {String key = "goodsRedisKey";//Set the flag bit, if the setting fails, explain Already locked, add expiration time to prevent deadlock boolean flagLock = redisTemplate.opsForValue().setIfAbsent(key, 1, Duration.ofSeconds(30));if (!flagLock) {//If the lock is not grabbed, return directly return;} try {//Business logic} finally {//Release the lock redisTemplate.delete(key);}}

What is redis used for? In addition to caching, the first thing that comes to mind is distributed locks. Nowadays, eight-part essays are basically a must-ask in interviews. We know that stand-alone systems can use locks such as synchronized or Lock provided by JUC, but these will - DayDayNews

The above code has a flaw and does not implement non-grabbing, that is, other threads can delete the lock you occupy, so you need to mark it Set the thread id in the bit, and when releasing the lock, determine whether it is the same thread. Only the same thread is allowed to release

private void testRedisLock() {//Generate thread identification String value = UUID.randomUUID().toString() + Thread. currentThread().getName();String key = "goodsRedisKey";//Set the flag bit. If the setting fails, it means it has been locked. Add an expiration time to prevent deadlock. boolean flagLock = redisTemplate.opsForValue().setIfAbsent(key, value, Duration.ofSeconds(30));if (!flagLock) {//Failed directly without grabbing the lock return;}try {//Business logic} finally {//Determine whether locking and unlocking are the same client, they are not The same atomic operation if (redisTemplate.opsForValue().get(key).equals(value)) {redisTemplate.delete(key);}}}

What is redis used for? In addition to caching, the first thing that comes to mind is distributed locks. Nowadays, eight-part essays are basically a must-ask in interviews. We know that stand-alone systems can use locks such as synchronized or Lock provided by JUC, but these will - DayDayNews

redisson implements distributed lock

There are many loose ends in the above code, such as Automatic renewal of the lock is not implemented, and the operation of deleting the lock is not an atomic operation. In fact, redisson is used directly to implement distributed locks at work. Explaining the above code is mainly to understand the implementation ideas of distributed locks.

uses redisson to implement distributed locks. You can see that the code is very simple.

private void testRedisLock() {String key = "goodsRedisKey";RLock lock = redissonClient.getLock(key);lock.lock();try {//Business logic} finally {lock.unlock();}}

What is redis used for? In addition to caching, the first thing that comes to mind is distributed locks. Nowadays, eight-part essays are basically a must-ask in interviews. We know that stand-alone systems can use locks such as synchronized or Lock provided by JUC, but these will - DayDayNews

Next, let’s look at the implementation principle of redisson. First, try to acquire the lock. Thread one locks successfully and continues execution. Thread two fails to lock and spins to wait.

What is redis used for? In addition to caching, the first thing that comes to mind is distributed locks. Nowadays, eight-part essays are basically a must-ask in interviews. We know that stand-alone systems can use locks such as synchronized or Lock provided by JUC, but these will - DayDayNews

What is redis used for? In addition to caching, the first thing that comes to mind is distributed locks. Nowadays, eight-part essays are basically a must-ask in interviews. We know that stand-alone systems can use locks such as synchronized or Lock provided by JUC, but these will - DayDayNews

locking source code:

T RFutureT tryLockInnerAsync(long waitTime, long leaseTime, TimeUnit unit, long threadId, RedisStrictCommandT command) {return evalWriteAsync(getRawName(), LongCodec.INSTANCE, command,"if (redis.call('exists ' , KEYS[1]) == 0) then " +"redis.call('hincrby', KEYS[1], ARGV[2], 1); " +"redis.call('pexpire', KEYS[1] , ARGV[1]); " +"return nil; " +"end; " +"if (redis.call('hexists', KEYS[1], ARGV[2]) == 1) then " +"redis .call('hincrby', KEYS[1], ARGV[2], 1); " +"redis.call('pexpire', KEYS[1], ARGV[1]); " +"return nil; " + "end; " +"return redis.call('pttl', KEYS[1]);",Collections.singletonList(getRawName()), unit.toMillis(leaseTime), getLockName(threadId));}

What is redis used for? In addition to caching, the first thing that comes to mind is distributed locks. Nowadays, eight-part essays are basically a must-ask in interviews. We know that stand-alone systems can use locks such as synchronized or Lock provided by JUC, but these will - DayDayNews

There are many operations in redisson In order to achieve atomicity, a lua script is used. The above code is divided into three sections

. The first section exists KEYS[1]. KEYS[1] is the user-defined key. The goodsRedisKey above determines whether the key exists or not. Then add a lock and set the expiration time. The default is 30S. The following figure shows the added lock

What is redis used for? In addition to caching, the first thing that comes to mind is distributed locks. Nowadays, eight-part essays are basically a must-ask in interviews. We know that stand-alone systems can use locks such as synchronized or Lock provided by JUC, but these will - DayDayNews

What is redis used for? In addition to caching, the first thing that comes to mind is distributed locks. Nowadays, eight-part essays are basically a must-ask in interviews. We know that stand-alone systems can use locks such as synchronized or Lock provided by JUC, but these will - DayDayNews

. The second paragraph realizes reentrancy. If the lock is added by yourself, the value is added to 1

when locking. The third paragraph realizes mutual exclusion. If the above If neither is satisfied, return the lock expiration time

release lock source code:

protected RFutureBoolean unlockInnerAsync(long threadId) {return evalWriteAsync(getRawName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,"if (redis.call('hexists', KEYS [1], ARGV[3]) == 0) then " +"return nil;" +"end; " +"local counter = redis.call('hincrby', KEYS[1], ARGV[3], - 1); " +"if (counter 0) then " +"redis.call('pexpire', KEYS[1], ARGV[2]); " +"return 0; " +"else " +"redis.call ('del', KEYS[1]); " +"redis.call('publish', KEYS[2], ARGV[1]); " +"return 1; " +"end; " +"return nil; ",Arrays.asList(getRawName(), getChannelName()), LockPubSub.UNLOCK_MESSAGE, internalLockLeaseTime, getLockName(threadId));}

What is redis used for? In addition to caching, the first thing that comes to mind is distributed locks. Nowadays, eight-part essays are basically a must-ask in interviews. We know that stand-alone systems can use locks such as synchronized or Lock provided by JUC, but these will - DayDayNews

The first paragraph determines whether the lock is added by oneself, not the lock added by oneself or the lock has expired, then Return to the second paragraph of

and decrement the lock count by 1. If the remaining lock count is 0, delete the key and release the lock.

Automatic renewal - watchdog

As mentioned above, the default expiration time of the lock is 30S. If it is locked The business execution time is very long, and it will be automatically unlocked if it exceeds 30 seconds, so there needs to be a mechanism to automatically renew, which is the watchdog.

Timeout task = commandExecutor.getConnectionManager().newTimeout(new TimerTask() {@Overridepublic void run(Timeout timeout) throws Exception {ExpirationEntry ent = EXPIRATION_RENEWAL_MAP.get(getEntryName());if (ent == null) {return;} Long threadId = ent.getFirstThreadId();if (threadId == null) {return;}RFutureBoolean future = renewExpirationAsync(threadId);future.onComplete((res, e) - {if (e != null) {log.error ("Can't update lock " + getRawName() + " expiration", e);EXPIRATION_RENEWAL_MAP.remove(getEntryName());return;}if (res) {// reschedule itselfrenewExpiration();} else {cancelExpirationRenewal(null );}});}}, internalLockLeaseTime / 3, TimeUnit.MILLISECONDS);

What is redis used for? In addition to caching, the first thing that comes to mind is distributed locks. Nowadays, eight-part essays are basically a must-ask in interviews. We know that stand-alone systems can use locks such as synchronized or Lock provided by JUC, but these will - DayDayNews

As you can see, the watchdog is a TimerTask, which is renewed every 10S after internalLockLeaseTime/3, and calls renewExpirationAsync to asynchronously renew. Call renewExpiration successfully to continue waiting for renewal.

The correct way to release the lock

Some operations such as tryLock may not guarantee that the lock is obtained, so you must judge when releasing the lock, otherwise an exception will be thrown

try {lock.tryLock(5, TimeUnit.SECONDS);//Business logic} catch (InterruptedException e) {e.printStackTrace();} finally {if (lock.isLocked() && lock.isHeldByCurrentThread()) {lock.unlock();}}

What is redis used for? In addition to caching, the first thing that comes to mind is distributed locks. Nowadays, eight-part essays are basically a must-ask in interviews. We know that stand-alone systems can use locks such as synchronized or Lock provided by JUC, but these will - DayDayNews

The strongest king-RedLock

redisson's distributed lock has been implemented After meeting 4 of the 5 major conditions, exclusivity, anti-deadlock, non-grabbing, and anti-reentrancy, can high availability be achieved? The high availability of redis is redis cluster and sentinel mode.

The first one is the master-slave situation. Since the redisson lock will only exist on one master, if that master hangs up, the lock disappears.

The second one is the master-slave situation. The master hangs up and the slave takes over. , the lock is still there, there seems to be no problem, but you must know that the redis cluster is in AP mode, what does it mean? High availability, partition fault tolerance, but does not meet consistency, because the master-slave synchronization is asynchronous, it is possible that the data locked by the master has not been synchronized, causing the lock to disappear when the slave comes up.

If you want absolute high availability, you need the strongest king - RedLock. To ensure high availability,

must have 2*N+1 master servers. N represents the number of servers allowed to hang up. For example, to allow 1 server to hang up, 3 masters are needed.

What is redis used for? In addition to caching, the first thing that comes to mind is distributed locks. Nowadays, eight-part essays are basically a must-ask in interviews. We know that stand-alone systems can use locks such as synchronized or Lock provided by JUC, but these will - DayDayNews

What is redis used for? In addition to caching, the first thing that comes to mind is distributed locks. Nowadays, eight-part essays are basically a must-ask in interviews. We know that stand-alone systems can use locks such as synchronized or Lock provided by JUC, but these will - DayDayNews

  1. Try to start from 3 servers in sequence. instance, using the same key and a random value (such as UUID) to acquire the lock. When requesting Redis to acquire a lock, the client should set a timeout, which should be less than the lock expiration time. For example, if your lock automatically expires in 10 seconds, the timeout should be between 5-50 milliseconds. This prevents clients from being blocked for long periods of time while trying to talk to a downed Redis node. If an instance is unavailable, the client should try to request another Redis instance to acquire the lock as soon as possible; the
  2. client calculates the time it takes to acquire the lock by subtracting the time recorded in step 1 from the current time. If and only if the lock is obtained from most (N/2+1, here are 3 nodes) Redis nodes, and the time used to obtain the lock is less than the lock expiration time, the lock is considered successful; if
  3. obtains The real validity time of the lock is equal to the initial validity time minus the time used to acquire the lock (the result calculated in step 2).
  4. If the lock cannot be obtained for some reason (the lock cannot be obtained on at least N/2 + 1 Redis instances, or the time to obtain the lock exceeds the effective time), the client should unlock on all Redis instances (even if a certain Some Redis instances are not locked successfully at all, which prevents some nodes from acquiring the lock but the client does not receive a response and the lock cannot be reacquired in the next period of time).

redisson implements RedLock

public void getlock() {//CACHE_KEY_REDLOCK is the key of redis distributed lockRLock lock1 = redissonClient1.getLock(CACHE_KEY_REDLOCK);RLock lock2 = redissonClient2.getLock(CACHE_KEY_REDLOCK);RLock lock3 = redissonClient3.getLock(CACHE _KEY_REDLOCK);RedissonRedLock redLock = new RedissonRedLock(lock1, lock2, lock3);boolean isLock;try {//waitTime lock waiting time processing, normally wait 5s//leaseTime is the expiration time of redis key, normally wait 5 minutes. isLock = redLock.tryLock(5, 300, TimeUnit.SECONDS);log.info("Thread {}, whether to get the lock: {} ",Thread.currentThread().getName(),isLock);if (isLock) {//Business processing}} catch (Exception e) {log.error("redlock exception ",e);} finally {//No matter what, redLock.unlock();}}

What is redis used for? In addition to caching, the first thing that comes to mind is distributed locks. Nowadays, eight-part essays are basically a must-ask in interviews. We know that stand-alone systems can use locks such as synchronized or Lock provided by JUC, but these will - DayDayNews

hotcomm Category Latest News

Inflation in the United States is soaring, and imposing tariffs on solar energy is nothing more than adding fuel to the fire and exacerbating cost burdens. Currently, China's photovoltaic industry around the world is pushing costs to the extreme. If Chinese solar modules are not  - DayDayNews

Inflation in the United States is soaring, and imposing tariffs on solar energy is nothing more than adding fuel to the fire and exacerbating cost burdens. Currently, China's photovoltaic industry around the world is pushing costs to the extreme. If Chinese solar modules are not

Biden can’t hold on any longer! Under the heavy pressure of inflation, China’s photovoltaic industry is expected to open the door to the United States as it plans to relax its tariff policy in the next two years.