Master Redisson Distributed Locks in Java: Features, Code & Spring Boot

This article introduces Redisson, a Java framework built on Redis for distributed systems, detailing its extensive features such as distributed objects, locks, rate limiters, and task scheduling, and provides step‑by‑step guidance on integrating Redisson’s distributed lock into Spring Boot applications with code examples.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
Master Redisson Distributed Locks in Java: Features, Code & Spring Boot

1. What is Redisson

Redisson is a Java framework based on Redis that provides rich features for distributed systems, including distributed objects, locks, rate limiters, pub‑sub, task scheduling, geospatial indexes, Bloom filters, caching, connection pools, and support for Redis cluster and sentinel.

2. Redisson Distributed Locks

Features of Redisson distributed locks

Thread safety and re‑entrancy – the same thread can acquire the lock multiple times.

Lock timeout – ability to set lease time to avoid long‑lasting locks.

Blocking acquisition – a thread can wait until the lock is released.

Non‑blocking acquisition – immediate failure if the lock is held.

Drawbacks

Single point of failure – reliability depends on Redis cluster availability.

Lock contention – high concurrency may degrade performance.

Dead‑lock risk – improper timeout handling can cause locks not to be released.

Lock granularity – too fine or too coarse granularity affects scalability.

Data consistency – distributed locks provide mutual exclusion but not full data consistency.

RLock interface source code

public interface RLock extends Lock, RLockAsync {
    String getName();
    void lockInterruptibly(long leaseTime, TimeUnit unit) throws InterruptedException;
    boolean tryLock(long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException;
    void lock(long leaseTime, TimeUnit unit);
    boolean forceUnlock();
    boolean isLocked();
    boolean isHeldByThread(long threadId);
    boolean isHeldByCurrentThread();
    int getHoldCount();
    long remainTimeToLive();
}

The RLock interface extends java.util.concurrent.locks.Lock and adds methods for lease time, asynchronous locking, and status queries.

3. Integrating Redisson with Spring Boot

Maven dependencies

<!-- Redisson dependency -->
<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson-spring-boot-starter</artifactId>
    <version>3.16.2</version>
</dependency>

<!-- Spring Data Redis dependency -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

Configuration (application.yml)

spring:
  redis:
    cluster:
      nodes:
        - 127.0.0.1:7000
        - 127.0.0.1:7001
        - 127.0.0.1:7002
    password: yourRedisPassword

Redisson client bean

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RedissonConfig {

    @Value("${spring.redis.cluster.nodes}")
    private String clusterNodes;

    @Value("${spring.redis.password}")
    private String password;

    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        config.useClusterServers()
                .addNodeAddress(clusterNodes.split(","))
                .setPassword(password);
        return Redisson.create(config);
    }
}

Using the lock

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SomeService {

    @Autowired
    private RedissonClient redissonClient;

    public void doSomething() {
        String lockKey = "myLock";
        RLock lock = redissonClient.getLock(lockKey);
        try {
            lock.lock(); // acquire lock
            // critical section
        } finally {
            lock.unlock(); // release lock
        }
    }
}

Lock acquisition behavior

RLock.lock() grants the lock immediately if it is free; otherwise the thread blocks until the lock is released. The method supports re‑entrancy, timeout settings, automatic lease renewal, and dead‑lock prevention via unique lock IDs.

Lock acquisition flow
Lock acquisition flow

Unlock behavior

RLock.unlock() releases the lock, allowing other threads to acquire it. If the lock was acquired without a lease time, Redisson’s watchdog thread automatically extends the lease every 10 seconds up to 30 seconds; if the client crashes, the lock expires after the watchdog stops.

Unlock flow
Unlock flow
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 Bootdistributed-lockredisson
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

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.