redisamazon-elasticacheredis-clusterioredis

How to connect to an AWS Redis cluster with ioredis?


I have an AWS Elasticache Redis cluster consisting of two shards, with each having one primary node and one replication node (total of four nodes).

How do I instantiate an ioredis Cluster object to connect and use all of these nodes?

The ioredis documentation indicates that the Cluster should be instantiated with a list of cluster nodes, like this:

const cluster = new Redis.Cluster([
  {
    port: 6380,
    host: "127.0.0.1",
  },
  {
    port: 6381,
    host: "127.0.0.1",
  },
]);

So, do I include all 4 of the AWS Redis endpoints? I.e., both primary and replication nodes, from each shard?

The documentation also says

does not need to enumerate all your cluster nodes, but a few so that if one is unreachable the client will try the next one, and the client will discover other nodes automatically when at least one node is connected

Does this mean I can include just one endpoint, and ioreds will figure it out?

Or, do I use the AWS Redis Configuration endpoint? If not, what is this for?

Thanks!


Solution

  • I ended up using the ioredis Cluster contructor, and this works as expected. I also did not use the "Configuration endpoint", but instead a list of the master node from each shard, like this:

    new Cluster([
      'https://<node1-master>',
      'https://<node2-master>',
      ...
    ], 
    {
      scaleReads: 'slave',
    })