In previous lessons, we worked with simple Redis buckets to store and retrieve single key-value pairs.
Now, let’s explore one of Redisson’s interesting features — the ability to retrieve multiple buckets at once and collect them into a single map.
This is extremely useful when you need to fetch multiple keys from Redis in one network call instead of repeatedly calling getBucket()
for each key.
Step 1: Creating the Test Class
Let’s start by creating a new test class named BucketAsMapTest
.
public class BucketAsMapTest extends TestBase { }
We’ll define a test method inside it:
@Test public void bucketAsMapTest() { // logic will go here }
Step 2: Understanding the Use Case
Normally, when working with Redis through Redisson, we retrieve data like this:
RBucket<String> bucket1 = redisson.getBucket("user:1:name"); RBucket<String> bucket2 = redisson.getBucket("user:2:name"); System.out.println(bucket1.get()); System.out.println(bucket2.get());
This works fine for a few keys, but what if you need to fetch hundreds of such keys?
Repeated network calls to Redis can slow down your application.
That’s where getBuckets()
comes in — it allows you to retrieve multiple key-value pairs in one call and return them as a Map.
Step 3: Setting Up the Test Data
Before we execute the test, let’s add some sample data directly into Redis.
You can do this through the Redis CLI:
SET user:1:name "Sam" SET user:2:name "Jake" SET user:3:name "Mike"
Now, Redis has three key-value pairs representing user names.
Step 4: Using getBuckets()
Back in our test class, we can now use getBuckets()
to retrieve multiple values at once.
@Test public void bucketAsMapTest() { // Step 1: Create a reference to RedissonBuckets RBuckets<String> buckets = redisson.getBuckets(); // Step 2: Retrieve multiple buckets as a map Map<String, String> userMap = buckets.get("user:1:name", "user:2:name", "user:3:name"); // Step 3: Print the results userMap.forEach((key, value) -> System.out.println(key + " -> " + value)); StepVerifier.create(Mono.just(userMap)) .expectNextCount(1) .verifyComplete(); }
When you run this, you’ll get output similar to:
user:1:name -> Sam user:2:name -> Jake user:3:name -> Mike
This confirms that getBuckets()
successfully fetched all three keys and values in one shot.
Step 5: What Happens for Missing Keys
Now, let’s try to include a non-existent key to see how Redisson handles it.
Modify the call like this:
Map<String, String> userMap = buckets.get( "user:1:name", "user:2:name", "user:3:name", "user:4:name" // this key does not exist );
When you print the result, you’ll notice that non-existent keys are simply ignored.
They do not appear in the returned map.
Example output:
user:1:name -> Sam user:2:name -> Jake user:3:name -> Mike
There’s no user:4:name
entry, which means Redisson gracefully handles missing keys instead of throwing an exception.
Step 6: How getBuckets()
Works Internally
Under the hood, getBuckets()
issues a multi-key GET command to Redis.
This is far more efficient than sending separate GET
requests for each key.
It’s particularly useful when:
- You have a batch of related keys to fetch.
- You want to minimize network round-trips.
- You need the data as a map structure in Java for further processing.
Step 7: Summary
Let’s summarize what we learned:
- The
RBucket
interface handles single key-value pairs. - The
RBuckets
interface lets you retrieve multiple key-value pairs in one operation. - You can call
redisson.getBuckets().get(key1, key2, key3...)
to fetch multiple buckets at once. - The result is returned as a Map where:
- Keys are the Redis keys.
- Values are the deserialized Redis values.
- Missing keys are ignored and not included in the map.
- This approach is efficient and reduces network overhead.
Example CLI Verification
Before running the test:
SET user:1:name "Sam" SET user:2:name "Jake" SET user:3:name "Mike"
After running the test:
user:1:name -> Sam user:2:name -> Jake user:3:name -> Mike
If you include a non-existent key like user:4:name
, it will not appear in the output — Redisson simply skips it.
Step 8: When to Use getBuckets()
Use getBuckets()
when:
- You need to fetch multiple Redis keys together.
- The keys represent similar or related data (for example, user profiles, order IDs, configuration entries).
- You want to avoid the overhead of repeated single
GET
operations.
It’s a clean, performant, and elegant way to batch-fetch data from Redis into your Java application.