What is a proper way to connect to AWS Elasticache (Redis cluster) from Java?

Bromo Programmer picture Bromo Programmer · Jul 19, 2017 · Viewed 11.8k times · Source

I'm new to AWS Elasticache redis, and I got below endpoint.

Image

I'm confused in either using Jedis and Redisson, because both provides single connection and cluster connection class.

Like in Jedis, for a single connection we can use:

Jedis conn = new Jedis("endpoint_address");

And for cluster connection we use:

Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
jedisClusterNodes.add(new HostAndPort("redis_cluster_ip", 7379));
JedisCluster jc = new JedisCluster(jedisClusterNodes);

These option also occur when I want to use Redisson. I'm not try to compare these two lib, my question is: WHICH ONE is the right method of connecting to AWS Redis Elasticache cluster, when you only have one end-point and still can utilize AWS auto scaling feature?

Expected answer is: use SINGLE or CLUSTER MODE.

Thanks :)

Answer

strongjz picture strongjz · Jul 20, 2017

It depends on how you have the redis cluster configured. Whether or not cluster mode is enabled.

You can find it in the console

http://docs.aws.amazon.com/AmazonElastiCache/latest/UserGuide/Endpoints.html

Redis (cluster mode disabled) clusters, use the Primary Endpoint for all write operations. Use the individual Node Endpoints for read operations (In the API/CLI these are referred to as Read Endpoints).

Redis (cluster mode enabled) clusters, use the cluster's Configuration Endpoint for all operations. You must use a client that supports Redis Cluster (Redis 3.2). You can still read from individual node endpoints (In the API/CLI these are referred to as Read Endpoints).

Or with the AWS CLI

aws elasticache describe-cache-clusters \
    --cache-cluster-id mycluster \
    --show-cache-node-info  

http://docs.aws.amazon.com/cli/latest/reference/elasticache/describe-cache-clusters.html

ConfigurationEndpoint -> (structure) Represents a Memcached cluster endpoint which, if Automatic Discovery is enabled on the cluster, can be used by an application to connect to any node in the cluster. The configuration endpoint will always have .cfg in it. Example: mem-3.9dvc4r.cfg.usw2.cache.amazonaws.com:11211