Skip to content

GH-11180: Fix caching logic in the RedisLockRegistry - #11181

Merged
cppwfs merged 1 commit into
mainfrom
GH-11180
Jul 14, 2026
Merged

GH-11180: Fix caching logic in the RedisLockRegistry#11181
cppwfs merged 1 commit into
mainfrom
GH-11180

Conversation

@artembilan

Copy link
Copy Markdown
Member

Fixes: #11180

The previous solution, based on the shared pool of ReentrantLock from the DefaultLockRegistry, is proven to be inconvenient with keys collision: different keys might stumble on the same shared ReentrantLock making the application being blocked for nothing.

  • Remove DefaultLockRegistry logic from the RedisLockRegistry
  • Remove local RedisLock.holdCount as we rely on the localLock again
  • Revert back default cacheCapacity to 100_000 as we don't use a shared pool of locks anymore
  • Do not evict from the cache still in-held locks
  • In the obtain(), try to remove unused lock when cacheCapacity is reached. If it cannot clean up the room, throw a CannotAcquireLockException. This is to prevent an out-of-memory error
  • Also, throw a CannotAcquireLockException on lock calls for an orphaned lock. This is to prevent a race condition when two distinct local locks are locked in different threads against the same key
  • Add human-readable toString() into the RedisLockRegistry

Auto-cherry-pick to 7.0.x

Fixes: #11180

The previous solution, based on the shared pool of `ReentrantLock`
from the `DefaultLockRegistry`, is proven to be inconvenient with keys
collision: different keys might stumble on the same shared
`ReentrantLock` making the application being blocked for nothing.

* Remove `DefaultLockRegistry` logic from the `RedisLockRegistry`
* Remove local `RedisLock.holdCount` as we rely on the `localLock` again
* Revert back default `cacheCapacity` to `100_000` as we don't use a shared pool of locks anymore
* Do not evict from the cache still in-held locks
* In the `obtain()`, try to remove unused lock when `cacheCapacity` is reached.
If it cannot clean up the room, throw a `CannotAcquireLockException`.
This is to prevent an out-of-memory error
* Also, throw a `CannotAcquireLockException` on lock calls for an orphaned lock.
This is to prevent a race condition when two distinct local locks are locked in different threads against the same key
* Add human-readable `toString()` into the `RedisLockRegistry`

**Auto-cherry-pick to `7.0.x`**
@artembilan
artembilan requested a review from cppwfs July 14, 2026 19:20

@cppwfs cppwfs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great.
I found no issues.

Thanks for doing this!

@cppwfs
cppwfs merged commit 1f75c88 into main Jul 14, 2026
3 checks passed
@cppwfs
cppwfs deleted the GH-11180 branch July 14, 2026 21:54
cppwfs pushed a commit that referenced this pull request Jul 15, 2026
Fixes: #11180

The previous solution, based on the shared pool of `ReentrantLock`
from the `DefaultLockRegistry`, is proven to be inconvenient with keys
collision: different keys might stumble on the same shared
`ReentrantLock` making the application being blocked for nothing.

* Remove `DefaultLockRegistry` logic from the `RedisLockRegistry`
* Remove local `RedisLock.holdCount` as we rely on the `localLock` again
* Revert back default `cacheCapacity` to `100_000` as we don't use a shared pool of locks anymore
* Do not evict from the cache still in-held locks
* In the `obtain()`, try to remove unused lock when `cacheCapacity` is reached.
If it cannot clean up the room, throw a `CannotAcquireLockException`.
This is to prevent an out-of-memory error
* Also, throw a `CannotAcquireLockException` on lock calls for an orphaned lock.
This is to prevent a race condition when two distinct local locks are locked in different threads against the same key
* Add human-readable `toString()` into the `RedisLockRegistry`

**Auto-cherry-pick to `7.0.x`**
@hyunto

hyunto commented Aug 19, 2026

Copy link
Copy Markdown

Could you let me know which Spring Boot version this Pull Request will first be released in? It doesn't appear to be included in 4.0.7 or 4.1.0. Is it scheduled for 4.0.8 and 4.1.1?

@cppwfs

cppwfs commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Next Release is 4.0.5 and 4.1.1. It should be in these releases as well as the 4.2.0-M1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Redis locks are not not unique

3 participants