To summarize the locking process, select a machine and send a Lua script with three parameters: the name of the lock (specified in the code), the duration of the lock (30 seconds by default), and the client ID of the lock (one for each client). The script then determines if there is a lock with that name, and if not, adds the client ID of that lock to the data structure.

Lock mutually exclusive mechanism

If myLock exists, the hash data structure of the myLock key contains the ID of client 2, but it does not. If myLock exists, the hash data structure of client 2 contains the ID of client 2. Because that contains the ID of client 1.

Therefore, client 2 will get a number returned by PTTL myLock that represents the remaining lifetime of the lock key myLock. Let’s say you have 15,000 milliseconds to live.

Client 2 will then enter a while loop, constantly trying to lock. Until client 1 releases myLock.

Automatic lock time delay mechanism

The default lock duration for client 1 is 30 seconds. What if client 1 still wants to hold the lock after 30 seconds? The mechanism is as follows:

Once client 1 successfully locks, a watch dog will be started, which is a background thread that will check every 10 seconds. If client 1 still has the lock key, the lifetime of the lock key will be continuously extended.

Reentrant locking mechanism

What if client 1 already owns the lock and wants to lock it? For example, the code logic is as follows:

Rlock lock = redisson. GetLock (" myLock "); lock.lock(); // a block of code lock.lock(); // a block of code lock.unlock(); lock.unlock();Copy the code

Let’s take a look at the above Lua script. The first if judgment is definitely not true. “exists myLock” indicates that the lock key already exists. The second if judgment is valid because the ID contained in the hash data structure of myLock is the ID of client 1, which is “8743C9c0-0795-4907-87FD-6C719a6b45866:1”.

Incrby myLock 8743C9c0-0775-4907-87fD-6c71a6b4586:1 1 Increments the lock count of client 1 by 1.

At this point, myLock’s data structure looks like this:

MyLock: {" 8743C9c0-0795-4907-87FD-6c719a6b4586:1 ": 2}Copy the code

As you can see, the last number represents the number of times the lock with that ID was locked.

Lock release mechanism

If lock.unlock() is executed to release a distributed lock, the business logic is very simple. In other words, each time the lock in the myLock data structure is reduced by one. If the lock count is 0, the client no longer holds the lock. In this case, the “del myLock” command is used to delete the key from redis. Then, the other client 2 can try to complete the lock.

This is the implementation mechanism of Redis distributed lock based on open source Redisson framework.

Disadvantages of Redis distributed locks

The biggest disadvantage of this distributed lock is that if the value of the myLock key is written to a Redis-master instance, the data will be copied to the Redis-slave instance asynchronously.

However, during this process, the Redis-master went down. The master and slave switched over, and the Redis-slave became redis-master.

Client 2 then attempts to lock the new redis-master, and client 1 thinks it is locked, causing multiple clients to complete the lock on the same distributed lock.

Today, we have collected: 2020 Internet Big factory real interview questions, mainly: Ant Financial, Pinduoduo, Ali Cloud, Baidu, Vipshop, Ctrip, Fengchao Technology, Lexin, Isoftstone, OPPO, Yinsheng payment, Ping an of China and other primary, intermediate, advanced Java interview questions set.

There are also specific interview questions for JVM, SPringBoot, SpringCloud, database, Linux, caching, message-oriented middleware, source code, etc.