feat: Optimize MGET to GET for single-key partitions #2432 #3387
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Make sure that:
mvn formatter:formattarget. Don’t submit any formatting related changes.Summary
This pull request resolves issue #2432, which was discussed and marked as
status: help-wanted.When an
MGETcommand is executed in a Redis Cluster environment, the internal logic now checks the number of keys for each slot-based partition. If a partition contains only a single key, this change translates the command to a more lightweightGETinstead ofMGET.This optimization reduces unnecessary overhead on both the client and server, improving throughput and lowering latency. This is especially effective in applications where single-key
MGETcalls occur frequently.Changes
src/main/java/io/lettuce/core/cluster/RedisAdvancedClusterAsyncCommandsImpl.javamget(Iterable<K> keys)to check the number of keys per partition.GETcommand.MGETcommand for that partition.src/test/java/io/lettuce/core/cluster/commands/StringClusterCommandIntegrationTests.javamgetOptimizationintegration test.mgetmethod, verifies the result, and cleans up the keys afterward.Testing
Using the Redis Cluster environment launched via
src/test/resources/docker-env/docker-compose.yml, I have verified the behavior change with theMONITORcommand.Before (Original Code):
The client sends an
MGETcommand for each key, even when there's only one key for the slot.After (With this PR):
The client now correctly sends a
GETcommand for each single-key partition.This confirms that the optimization is working as intended.
Thank you for the opportunity to contribute.
This is my first pull request to the project, so I'm still getting familiar with the contribution process. I'm more than happy to make any changes and welcome all feedback 🤗