Do a simple program wang, share dry goods, talk about life.

Wechat public account: Advanced backend architecture

Follow me to find more dry goods, microservices, Spring source, JVM, SpringCloud Alibaba, K8S, etc.

If you found this article helpful, please give me a like and thank you for your support!

Summary of historical articles: Netty, source code, concurrency, JVM, etc

Today I will share some interview content of Redis which is common in interview. If you need it you can review it, if you don’t you can save it and bring it back when you need it.

The background,

Interviewer: Do you use distributed locks in your projects?

Me: Yes.

Interviewer: What do you use distributed locks for?

Me: Prevent dirty data when multiple nodes concurrently access the same data.

Interviewer: What are the solutions for distributed locking? Which one do you use?

Me: There are zK-based schemes for temporary sequential nodes, and there are Redis schemes for setnx and for specifying expire timeout.

Interviewer: Which plan do you use?

Me: Redisson, a package component of Redis.

Interviewer: What clustering model do you use for Redis?

Me: I use the Cluster mode of Redis.

Interviewer: What modes can Redisson configure?

Me: Single node mode, Cluster mode, Sentinel mode

Interviewer: How is the lock timeout for distributed locks configured?

Me: The timeout time needs to be evaluated according to the pressure test results based on the business scenario, and the pressure test results should be slightly enlarged by 1 to 2 times.

Interviewer: What if the distributed lock is automatically released before the timeout period is set to 2s?

Me: Well, I don’t remember.

Interviewer: Ok. In your Redis cluster mode, what if the master fails?

Me: We are a three-master three-slave cluster mode. If more than half of the primary nodes communicate with the faulty primary node, the current primary node is considered to be down, and the secondary node below the primary node will become the primary node. If there is no slave below the primary, the cluster enters the fail state. A secondary node is a backup of the primary node.

Interviewer: There are several redis persistence mechanisms. What are the advantages and disadvantages?

I: Redis persistence is divided into RDB and AOF, one is snapshot, the other is append. Snapshot is a period of time for data backup, the addition is as long as there is a command to execute, record the command information. Data can be recovered according to instructions.

If the snapshot mode is suspended, data may be lost during the snapshot period. In the snapshot mode, data recovery is fast, while in the mirror mode, data recovery is slow. In the snapshot mode, data is fully stored and a persistent policy is configured to prevent data loss.

Interviewer: How do you use it?

Me: We use both and back up our data regularly. After hanging convenient quick recovery and ensure data integrity.

It’s like asking questions, brother Jimei, killing me.

But a yard is a yard, or let me recall a lot of knowledge. Also by the way, the next time you encounter this problem will not say anything.

Next, take a closer look at Redisson’s unfinished business after the lock timeout.

Redisson lock timeout watchdog mechanism

The goal of Redisson is to promote the Separation of Concern for Redisso that users can focus more on business logic.

First go to Github address, Chinese document, thief 6.

Redisson 英 文 版 github.com/redisson/re…

What if the lock has timed out, but the thread has not completed its task?

Reentrant Lock

Based on Redis Redisson distributed reentrant Lock RLock Java object implements the Java. Util. Concurrent. The locks. Lock interface. It also provides Async, Reactive and RxJava2 standard interfaces.

RLock lock = redisson.getLock("anyLock"); Lock. Lock ();Copy the code

As you know, if the Redisson node that stores the distributed lock goes down, and the lock happens to be in the locked state, the lock will be locked.

To prevent this from happening, Redisson internally provides a lock watchdog that continuously extends the lifetime of the lock before the Redisson instance is closed.

By default, the watchdog’s lock timeout is 30 seconds,

Can also by modifying Config. LockWatchdogTimeout to specify separately.

In addition, Redisson provides the leaseTime parameter to specify the lock time through the lock method. After this time, the lock unlocks automatically.

Lock (10, timeunit.seconds); Boolean res = lock.tryLock(100, 10, timeunit.seconds); if (res) { try { ... } finally { lock.unlock(); }}Copy the code

Development:

Redisson also provides a related method for asynchronous execution of distributed locks:

RLock lock = redisson.getLock("anyLock");
lock.lockAsync();
lock.lockAsync(10, TimeUnit.SECONDS);
Future<Boolean> res = lock.tryLockAsync(100, 10, TimeUnit.SECONDS);
Copy the code

The RLock object fully conforms to the Java Lock specification. That is only a process of lock can unlock, other processes to unlock will throw IllegalMonitorStateException errors. However, if you need other processes to unlock, use distributed Semaphore objects.

conclusion

The above is the summary and sharing of today’s content, thank you for your thumbs up, attention and favorites!

Wechat public account: Advanced backend architecture

More articles are coming, like to remember to give me a 👍, thank you for your support!

Public article synchronous update! Focus on me, don’t get lost!