Using Redisson for Distributed Locks in Spring Boot

This article explains how to integrate Redisson into a Spring Boot application to implement various Redis‑based distributed synchronization primitives—including re‑entrant locks, read‑write locks, and semaphores—while covering configuration, code examples, the watchdog mechanism, and practical testing procedures.

Wukong Talks Architecture
Wukong Talks Architecture
Wukong Talks Architecture
Using Redisson for Distributed Locks in Spring Boot

The article builds on a previous discussion of five Redis distributed‑lock solutions and presents Redisson as a superior, concise approach for implementing distributed locks within a Spring Boot project.

Redisson is a Java in‑memory data grid built on Netty that wraps native Redis data structures (Hash, List, Set, String, Geo, HyperLogLog, etc.) into familiar Java collections and provides high‑level distributed primitives such as Lock, MultiLock, ReadWriteLock, Semaphore, and more.

To start, add the Redisson Maven dependency:

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

Create a configuration class that builds a RedissonClient for a single‑node Redis server:

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

Acquire a re‑entrant lock with simple code and expose it via a REST endpoint for testing:

@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";
}

The article demonstrates that the lock blocks other threads until it is released and shows that Redisson’s watchdog automatically extends the lock’s TTL (default 30 seconds) to avoid deadlocks when a node crashes.

Additional distributed primitives are covered: a read‑write lock ( RReadWriteLock) with shared read and exclusive write semantics, and a semaphore ( RSemaphore) used to model a parking‑lot scenario. Example code for acquiring and releasing a semaphore is provided.

Redisson also supports advanced lock types such as FairLock, MultiLock, RedLock, PermitExpirableSemaphore, and CountDownLatch, which can be explored in the official documentation.

Overall, Redisson offers a concise "king‑level" solution for distributed synchronization in Java backend services, simplifying configuration and usage compared with raw Redis commands.

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.

JavaconcurrencyredisSpring Bootredisson
Wukong Talks Architecture
Written by

Wukong Talks Architecture

Explaining distributed systems and architecture through stories. Author of the "JVM Performance Tuning in Practice" column, open-source author of "Spring Cloud in Practice PassJava", and independently developed a PMP practice quiz mini-program.

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.