Skip to content

Conversation

kistlers
Copy link

@kistlers kistlers commented Oct 9, 2025

This is an attempt to make issue #10471 work without support for SPUBLISH.
I append the lockKey to the channelUnlockKey. This allows us to control the cluster slot cache this operation is hashed into, by including a hash tag ({...}) in the lockKey. Refer to the issue description for an explanation of why this matters.

As a consequence, I had to change the ChannelTopic to a PatternTopic to keep receiving subscription events for the PubSub lock.

Copy link
Member

@artembilan artembilan left a comment

Choose a reason for hiding this comment

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

Please, add DCO into your commit message before we start reviewing: https://spring.io/blog/2025/01/06/hello-dco-goodbye-cla-simplifying-contributions-to-spring.
Thanks

@kistlers
Copy link
Author

kistlers commented Oct 9, 2025

Please, add DCO into your commit message before we start reviewing: https://spring.io/blog/2025/01/06/hello-dco-goodbye-cla-simplifying-contributions-to-spring. Thanks

Done!

return Boolean.TRUE.equals(RedisLockRegistry.this.redisTemplate.execute(
UNLINK_UNLOCK_REDIS_SCRIPT, Collections.singletonList(this.lockKey),
RedisLockRegistry.this.clientId, RedisLockRegistry.this.unLockChannelKey));
RedisLockRegistry.this.clientId, RedisLockRegistry.this.unLockChannelKey + ":" + this.lockKey));
Copy link
Member

Choose a reason for hiding this comment

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

I understand how this work from the pattern perspective.
But is it really OK to have so many channels for every single lock we are going to use in our application?

Copy link
Author

Choose a reason for hiding this comment

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

Maybe not. However, I do not see any other way to ensure that all three redis calls in the UNLINK_UNLOCK_SCRIPT hash to the same shard if we are running on a system that requires it, like Valkey on AWS.

If the call to PUBLISH would not be in that script, that would make all calls in that script use the same KEYS[1] as the first argument to a redis call. But, I suppose is has to be in there.
If that were not necessary, the call to Publish could be in a separate script, but that might not be okay, for reasons I do not yet know.

Copy link
Author

Choose a reason for hiding this comment

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

I did some research in the middle of typing out the last message.
As far as I do understand it, lots of channels are not an issue. This, as channels do not consume memory, only subscribers do, irrespective of pattern or exact match.

Copy link
Author

@kistlers kistlers Oct 9, 2025

Choose a reason for hiding this comment

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

On the other hand, extracting the PUBLISH call to a separate script might be okay, but it could happen that the application dies between the two script calls and that would make other subscribers wait for the remaining TTL of the lock.

On another note, my understanding is currently that any values used as a key of an operation should be included in the keys, rather than the args. Meaning either the current or my new unlockChannelKey should be passed as KEYS[2] rather than ARGV[2]. See https://docs.aws.amazon.com/AmazonElastiCache/latest/dg/WorkingWithRedis.html, bullet point "Use sharded pub/sub" or https://redis.io/docs/latest/develop/pubsub/#sharded-pubsub

Copy link
Member

@artembilan artembilan left a comment

Choose a reason for hiding this comment

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

This looks more closer to what I won't worry about 😄 .

If you confirm that this works with Valkey, please, add your name to the @author list of the changed class.

Please, also rebase your branch (and re-push to PR) to the latest main as I've just pushed the fix Redis tests according to the latest Spring Data Redis.

Thanks

…ontrol cluster slot hashing

As a consequence, change the ChannelTopic to a PatternTopic to keep receiving subscription events for PubSub lock

Signed-off-by: Severin Kistler <[email protected]>
…f `ARGV[2]` as values used as keys should be passed as such

Signed-off-by: Severin Kistler <[email protected]>
@kistlers
Copy link
Author

According to my tests against valkey this is working.
My name is in the authors list of the file and I have rebased the branch. While at it, I prepended the issue id to the commit messages.

@kistlers kistlers requested a review from artembilan October 10, 2025 07:42
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.

2 participants