published fresh until

Spring Boot: use a Redis lock with Redisson

Summary As of October 2026, use Redisson RLock for cooperative locking across Spring Boot instances, bound acquisition time and release only after successful acquisition. Lease expiry or failover still requires stale-writer protection; start with Redisson 4.8.0.

Not for Spring Boot 3.5 to 4 migration, see spring-boot-4-upgrade-from-3-5-jackson-3-starters-openrewrite

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Use Redisson's RLock around the smallest shared operation, with bounded acquisition and a finally block, and design the protected resource to reject stale work.

Do first: set app.redis.address to your Redis endpoint

Choose a stable resource key such as invoice-export:account-123. Decide what a losing caller does: retry later or return busy. Set a wait budget. Every cooperating instance must use the same Redis deployment and key. A Java synchronized block does not coordinate separate processes.

Wire the client without changing Spring Data

For a small standalone integration, add org.redisson:redisson:4.8.0 to the existing build. The getting-started guide documents direct client creation; the latest stable release is 4.8.0, checked 2026-10-28.

In an existing Spring configuration class, expose one client bean (imports omitted):

@Bean(destroyMethod = "shutdown")
RedissonClient redissonClient(@Value("${app.redis.address}") String address) {
    Config config = new Config();
    config.useSingleServer().setAddress(address);
    return Redisson.create(config);
}

Set app.redis.address through your deployment's configuration to the correct Redis endpoint. Use a proper Sentinel/cluster configuration when applicable; the single-server example is not a cluster configuration. Keep authentication material in your existing secret mechanism.

In a blocking service method, after injecting RedissonClient redisson:

RLock lock = redisson.getLock(resourceKey);
boolean acquired = lock.tryLock(2, 30, TimeUnit.SECONDS);
if (!acquired) {
    throw new IllegalStateException("Resource busy");
}
try {
    performBoundedOperation();
} finally {
    lock.unlock();
}

The method must propagate or correctly handle InterruptedException. The sample's 2-second wait and 30-second lease are illustrative application choices. It is suitable only when the protected operation and its side effects remain safely bounded; after the lease, another worker may enter even if the first is still alive. An unlock failure after expiry needs investigation, not silent success.

Select a lock lifetime deliberately

Redisson's lock reference, checked 2026-10-28, documents automatic expiry for a specified lease and watchdog renewal otherwise. Its default watchdog timeout is 30 seconds. Only the owner thread can unlock an RLock.

ApproachWinsLosesPick this if
Explicit leasePredictable upper lifetimeWork can outlive the leaseYour work and failure handling fit a bounded lease
Watchdog renewalDuration varies while a healthy client renewsPauses or renewal failures still permit expiryYou can test renewal failure and reject stale side effects

A fencing token helps only if the protected resource enforces token order. A Redis lock alone cannot promise exactly-once effects across pauses and failover. Use idempotency or storage-side checks for irreversible operations.

If you instead choose redisson-spring-boot-starter, follow the Spring integration compatibility table: its default Spring Data module targets the newest Boot line and may need adjustment for your app. Do not add the starter and this manual bean indiscriminately.

Defaults and cost

Solo application with one instance: avoid distributed locking until multiple processes can compete. Small team with multiple replicas: direct RLock integration for a cooperative job, plus duplicate-safe effects. Enterprise critical writes: require failover tests and resource-enforced stale-writer protection before using the lock as a correctness boundary.

Redisson Community uses Apache-2.0; you pay for the Redis service and operations you deploy. No hosted price or throughput guarantee is assumed. checked 2026-10-28.

Public evidence

ptanov, issue #7272, 2026-07-31: “the watchdog is renewing an orphan”. The report describes an interruption/renewal race. It is not proof that a fixed lease is always safer or that this persists in the current release; test your acquisition and cancellation paths.

Verify and ask the agent

Run two app instances against a disposable Redis deployment with the same resource key. Hold one operation open and confirm the second reports busy. Then test owner termination, work exceeding the lease and a delayed stale write.

Add one resource-specific RLock with a bounded wait. Show contention, interruption, expiry and stale-write tests. Explain exactly what protects the side effect if a worker resumes after losing ownership.

Limits

No Spring application, contention test or failover test was run. The sample is blocking Java code, not a reactive recipe, and does not make a multi-resource transaction atomic. checked 2026-10-28.

Started from a post by @Redisson, packaged by the editor. Original post

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Spring Boot: use a Redis lock with Redisson
Summary: As of October 2026, use Redisson RLock for cooperative locking across Spring Boot instances, bound acquisition time and release only after successful acquisition. Lease expiry or failover still requires stale-writer protection; start with Redisson 4.8.0.
Copy for agent
get the whatworks thread spring-boot-redisson-redis-distributed-lock-recipe and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread spring-boot-redisson-redis-distributed-lock-recipe
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp