Distributed lock Redisson

One, SpringBoot integrates Redisson

1.1 Introduce pom

1.2 Configuration class

1.3 Test class

Two, simulate redis power off

Start two instances. When one is ready to release the lock after acquiring the lock, close the service, simulate a power failure, and see if a deadlock will be sent

After accessing 10000, after adding the lock, close the service and simulate a power outage. At this time, the 10000 service has not been unlocked yet.

Access 10001, get the lock normally

In addition, the TTL time is constantly changing and automatically renews

Three, watchdog principle

3.1 Specify the lock release time

3.2 Unspecified lock release time

Four, read-write lock

It is guaranteed that the latest data can be read. During the modification, the write lock is an exclusive lock (mutual exclusion lock). Read lock is a shared lock

The write lock is not released, and the read must wait  

Read + Read: Equivalent to lock-free, concurrent read, only recorded in redis, all current read locks. They will all be locked at the same time

Write + Read: Wait for the write lock to be released

Write + write: blocking mode

Read + Write: There is a read lock, and writing also needs to wait.

As long as there is writing, we must wait

4.1 Write delay 30s

4.2 Reading is also delayed by 30s

Five, latch (CountDownLatch)

For example, the door is locked during school holidays, no one in class 1 and no one in class 2, and so on, we can lock the door only when all 5 classes are finished.

Six, semaphore (Semaphore)

Parking in the garage, if there are three parking spaces, a car will occupy one parking space, and one parking space will be released when one goes away.

Distributed current limiting operations can be performed to limit traffic. For example, our current system can withstand up to 10,000 concurrent requests per second. If all requests come to my service, it may be overwhelming. So, you can do this.

As long as all requests come up, it first obtains a semaphore. For example, the total amount is 10,000. If it can obtain this semaphore, it means there are free threads for it to process. If it cannot obtain it, then wait for others to give it. It is released and processed again, so it can be used for current limiting operations.

Video tutorial

Guess you like

Origin blog.csdn.net/qq_38826019/article/details/115033918