Redis Java client for Google Cloud Memorystore for Redis

What is Google Cloud Memorystore for Redis?

Along with Amazon Web Services and Microsoft Azure, Google Cloud Platform is one of the “big three” public cloud providers. As such, Google offers no shortage of cloud computing services for nearly every use case conceivable, including Google Cloud Memorystore for Redis.

Memorystore for Redis is a fully managed service that makes it easy for users to set up and manage a Redis instance running in Google Cloud. Redis is an open-source in-memory data structure store that is commonly used for a range of applications, including NoSQL databases, caches, and message brokers.

According to Google, Memorystore for Redis users “can achieve extreme performance by leveraging the highly scalable, available, secure Redis service without the burden of managing complex Redis deployments.” Memorystore for Redis is intended to be Google Cloud’s answer to competing solutions, such as Amazon MemoryDB for Redis and Amazon ElastiCache for Redis.

Redis Java client for Google Cloud Memorystore for Redis

While Redis is extremely useful for many different use cases, it comes with a significant caveat for developers: It isn’t immediately compatible with Java and many other popular programming languages. Instead, developers can use a third-party Redis Java framework, such as Redisson, to enjoy access to Redis features and functionality while programming in Java.

Redisson is an open-source Redis Java client that includes support for many of the most common Java data structures, services, and other objects. The goal of Redisson is to dramatically lower the Redis learning curve for Java developers, making it easy to get up and running with your own Redis Java project.

In particular, Redisson simplifies the task of connecting to a Google Cloud Memorystore for Redis instance. Below is a short Redisson code snippet demonstrating how to use Google Cloud Memorystore for Redis in Java:

package redis.demo;

import org.redisson.Redisson;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;

public class Application {

    public static void main(String[] args) {

        Config config = new Config();

        RedissonClient redisson = Redisson.create(config);

        // perform operations
        RBucket bucket = redisson.getBucket("simpleObject");
        bucket.set("This is object value");
        RMap map = redisson.getMap("simpleMap");
        map.put("mapKey", "This is map value");

        String objectValue = bucket.get();
        System.out.println("stored object value: " + objectValue);
        String mapValue = map.get("mapKey");
        System.out.println("stored map value: " + mapValue);

The above lines of code briefly demonstrate how users can store and retrieve data using Redisson and Google Cloud Memorystore for Redis. After establishing a connection to Google Cloud using the setAddress function, the code performs two storage operations using the RBucket and RMap objects:

  • The RBucket object is a general-purpose container for objects with a maximum size of 512 megabytes.

  • The RMap object uses a map data structure to store objects: a specific value is associated with each key in the map.

Redisson’s cluster mode can be set up with the following line of Java code:

ClusterServersConfig clusterConfig = config.useClusterServers();

For the full list of settings that can be configured via the clusterConfig object, visit the Redisson GitHub documentation.

Similar articles