Master Redisson: Simplify Distributed Locks in Spring Boot

This tutorial explains how to integrate Redisson into a Spring Boot application to implement robust distributed locks, covering Redisson's architecture, configuration, code examples, lock types, watchdog mechanism, and practical testing procedures for reentrant, read‑write, and semaphore locks.

Su San Talks Tech
Su San Talks Tech
Su San Talks Tech
Master Redisson: Simplify Distributed Locks in Spring Boot

In the previous article we discussed five Redis distributed‑lock solutions; this piece introduces the superior approach using Redisson.

Redisson is a Java in‑memory data grid built on Redis, providing a simple API for distributed locks, various data structures, and advanced synchronization primitives.

1. What is Redisson?

Redisson abstracts Redis operations through the Netty NIO framework, offering Java‑friendly mappings for native Redis structures such as Hash, List, Set, String, Geo, HyperLogLog, and more.

It also supplies distributed collections (Multimap, LocalCachedMap, SortedSet, etc.) and synchronization tools (Lock, MultiLock, ReadWriteLock, RedLock, Semaphore, PermitExpirableSemaphore, CountDownLatch).

Netty framework : Provides the underlying client for Redis connections and command execution.

Basic data structures : Wraps Redis Hash, List, Set, String, Geo, HyperLogLog into familiar Java collections.

Distributed data structures : Includes Multimap, LocalCachedMap, SortedSet, Queue, Bloom Filter, AtomicLong, etc.

Distributed locks : Implements Lock, MultiLock, ReadWriteLock, RedLock, Semaphore, PermitExpirableSemaphore, CountDownLatch.

Node support : Allows independent nodes to execute remote tasks.

2. Integrating Redisson

Two integration methods exist for Spring Boot; this article uses programmatic configuration.

2.1 Add Maven dependency

<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson</artifactId>
    <version>3.15.5</version>
</dependency>

2.2 Create configuration class

@Configuration
public class MyRedissonConfig {
    @Bean(destroyMethod="shutdown")
    public RedissonClient redisson() throws IOException {
        Config config = new Config();
        // Single‑node mode
        config.useSingleServer().setAddress("redis://127.0.0.1:6379");
        return Redisson.create(config);
    }
}

2.3 Test the configuration

@Autowired
RedissonClient redissonClient;

@Test
public void TestRedisson() {
    System.out.println(redissonClient);
}
Github : https://github.com/Jackson0714/PassJava-Platform Gitee : https://gitee.com/jayh2018/PassJava-Platform Tutorial site : www.passjava.cn

3. Distributed Reentrant Lock

3.1 Basic lock test

@ResponseBody
@GetMapping("test-lock")
public String TestLock() {
    RLock lock = redisson.getLock("WuKong-lock");
    lock.lock();
    try {
        System.out.println("Lock acquired, thread ID: " + Thread.currentThread().getId());
        Thread.sleep(10000);
    } catch (Exception e) {
        // handle
    } finally {
        lock.unlock();
        System.out.println("Lock released, thread ID: " + Thread.currentThread().getId());
    }
    return "test lock ok";
}

Two concurrent HTTP requests demonstrate that the second thread must wait until the first releases the lock.

3.2 Watchdog mechanism

Redisson uses a watchdog to automatically extend the lock’s TTL while the client is alive, preventing deadlocks when a node crashes. The default watchdog interval is 30 seconds and it refreshes the lock every 10 seconds.

3.3 Setting lock expiration

lock.lock(8, TimeUnit.SECONDS);

If business execution exceeds the expiration time, manual unlock will fail.

4. Distributed Read‑Write Lock

RReadWriteLock rwlock = redisson.getReadWriteLock("anyRWLock");
rwlock.readLock().lock(); // or rwlock.writeLock().lock();

Read lock is shared, write lock is exclusive. The API also supports timed locks and tryLock with wait/lease parameters.

5. Distributed Semaphore

@ResponseBody
@RequestMapping("park")
public String park() throws InterruptedException {
    RSemaphore park = redisson.getSemaphore("park");
    park.acquire();
    return "OK";
}

@ResponseBody
@RequestMapping("leave")
public String leave() throws InterruptedException {
    RSemaphore park = redisson.getSemaphore("park");
    park.release();
    return "OK";
}

The semaphore mimics a parking lot with three slots; acquire blocks when no slots are available.

6. Other Redisson Locks

Fair Lock

MultiLock

RedLock

ReadWriteLock

PermitExpirableSemaphore

CountDownLatch

For more details, refer to the official Redisson documentation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

redisSpring Bootredisson
Su San Talks Tech
Written by

Su San Talks Tech

Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.