Implementing Distributed Locks with Redisson in Spring Boot
This article demonstrates how to integrate Redisson with Spring Boot to create Redis‑based distributed locks, provides Maven dependency configuration, shows a Spring @Configuration class for Redisson, offers a utility wrapper for lock operations, and includes a REST controller with a concurrent test to illustrate correct locking behavior versus no‑lock scenarios.
In many interview scenarios candidates are asked about distributed locks, yet few have hands‑on experience; this guide walks through implementing a Redis‑based lock using Redisson within a Spring Boot application.
Adding dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.10.6</version>
</dependency>Redis configuration (application.yml)
spring:
redis:
host: 47.103.5.190
port: 6379
jedis:
pool:
max-active: 100
max-idle: 10
max-wait: -1
timeout: 5000
database: 0Redisson configuration class
@Configuration
public class RedissonConfig {
@Value("${spring.redis.host}") private String host;
@Value("${spring.redis.port}") private String port;
@Value("${spring.redis.password:}") private String password;
@Bean
public RedissonClient redissonClient() {
Config config = new Config();
config.useSingleServer().setAddress("redis://" + host + ":" + port);
if (StringUtils.isEmpty(password)) {
config.useSingleServer().setPassword(null);
} else {
config.useSingleServer().setPassword(password);
}
return Redisson.create(config);
}
}Utility class for lock operations
public class RedisLockUtil {
private static DistributedLocker distributedLocker =
SpringContextHolder.getBean("distributedLocker", DistributedLocker.class);
public static RLock lock(String lockKey) {
return distributedLocker.lock(lockKey);
}
public static RLock lock(String lockKey, int timeout) {
return distributedLocker.lock(lockKey, timeout);
}
public static RLock lock(String lockKey, int timeout, TimeUnit unit) {
return distributedLocker.lock(lockKey, unit, timeout);
}
public static boolean tryLock(String lockKey, int waitTime, int leaseTime) {
return distributedLocker.tryLock(lockKey, TimeUnit.SECONDS, waitTime, leaseTime);
}
public static void unlock(String lockKey) {
distributedLocker.unlock(lockKey);
}
public static void unlock(RLock lock) {
distributedLocker.unlock(lock);
}
}REST controller with concurrent test
@RestController
@RequestMapping("/redisson")
public class RedissonLockController {
private Integer lockCount = 10;
private Integer count = 10;
private static int threadNum = 10;
@GetMapping("/test")
public void lock() {
CountDownLatch latch = new CountDownLatch(1);
for (int i = 0; i < threadNum; i++) {
new Thread(new MyRunnable(latch)).start();
}
latch.countDown();
}
private void testLockCount() {
String lockKey = "lock-test";
try {
RedisLockUtil.lock(lockKey, 2, TimeUnit.SECONDS);
lockCount--;
log.info("lockCount值:" + lockCount);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
RedisLockUtil.unlock(lockKey);
}
}
private void testCount() {
count--;
log.info("count值:" + count);
}
class MyRunnable implements Runnable {
private final CountDownLatch latch;
MyRunnable(CountDownLatch latch) { this.latch = latch; }
@Override
public void run() {
try { latch.await(); } catch (InterruptedException e) { log.error(e.getMessage(), e); }
testCount();
testLockCount();
}
}
}The test output shows that without locking the count-- operations interleave and produce out‑of‑order values, while the locked version yields a deterministic decrement sequence, confirming the correctness of the Redisson distributed lock implementation.
Although the demo runs on a single node, the same principles apply to multi‑node deployments, providing a reliable way to protect critical sections in concurrent Java services.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
