Redis Java Client for Amazon MemoryDB

Java developers who want to use Amazon MemoryDB for Redis can use third-party Redis Java frameworks like Redisson. The Redisson Java client for Redis makes it easy to connect to and configure an Amazon MemoryDB instance while programming in Java.

What is Amazon MemoryDB?

As one of the leading public cloud providers, Amazon Web Services offers several solutions for nearly every imaginable use case. Amazon MemoryDB is an AWS database product called a “durable, in-memory database service for ultra-fast performance.” According to AWS, MemoryDB can handle more than 160 million requests per second, making it optimized for high-demand applications.

In particular, Amazon MemoryDB is intended for use with Redis, an open-source in-memory data structure store commonly used to implement NoSQL databases, caches, and message brokers. MemoryDB uses Redis data structures such as streams, lists, and sets to build complex data stores in support of web and mobile applications.

Redisson: A Redis Java framework for Amazon MemoryDB

Amazon MemoryDB and Redis can be a potent combination for large-scale, highly performant use cases. However, there’s one issue: Redis lacks out-of-the-box support for the Java programming language. Instead, the Redis project recommends that Java developers use a third-party Redis Java client such as Redisson.

Redisson is an open-source Redis Java client that makes it easy for Java developers to start working with Redis. Developers can draw on the dozens of Java objects and services provided by Redisson to connect to a Redis instance and start storing and querying data.

In particular, Redisson includes support for Amazon MemoryDB for Redis. Below is a code snippet that illustrates how to connect Redisson to Amazon MemoryDB:

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();
        config.useClusterServers()
              .addNodeAddress("redis://memorydb.amazonaws.com:6379");

        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);
        redisson.shutdown();
    }
}

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

ClusterServersConfig clusterConfig = config.useClusterServers();

Redisson’s cluster mode is compatible with Amazon MemoryDB for Redis, as well as other hosting options such as Amazon ElastiCache for Redis. Developers can use Redisson to specify a number of settings for the ClusterServersConfig object, including:

  • nodeAddresses: The address of the Redis cluster node or Redis endpoint in host:port format. Once the address is provided, Redisson can automatically discover the node’s topology.

  • scanInterval: The number of milliseconds between scans of the Redis cluster topology.

  • readMode: The node type used for read operations. The available values are SLAVE (read from slave nodes), MASTER (read from master nodes), and MASTER_SLAVE (read from both).

  • loadBalancer: The configuration for the load balancer between different nodes in the Redis cluster. The available values are RoundRobinLoadBalancer (default), WeightedRoundRobinBalancer, and RandomLoadBalancer.

  • retryAttempts: The number of attempts made to send a command to the Redis server before an error is thrown.

  • timeout: The number of milliseconds before the Redis server sends a timeout response after a Redis command is successfully sent.

Similar articles