How to determine the primary endpoint in Redis (cluster mode enabled) replication groups

Viewed 2749

We have a clustered Redis setup (engine version 5.04) with three node groups each in their own AZ, each group contains three nodes, encryption is enabled in transit and at rest. Stunnel is configured on a bastion host, which allows me to run a bash script to flush each of the primary replicas when required (during upgrades to our application).

After applying the latest Redis software patches via the console, the primary endpoint in each node group has changed due to automatic failover and I'm looking for the best way to determine the new primary endpoints so my bash script can be updated accordingly.

The documentation states that running aws elasticache describe-replication-groups --replication-group-id my-replication-group should indicate the CurrentRole of the node group members e.g.

"NodeGroupMembers": [
   {
      "CurrentRole": "primary", 
      "PreferredAvailabilityZone": "us-west-2a", 
      "CacheNodeId": "0001", 
      "ReadEndpoint": {
         "Port": 6379, 
         "Address": "myreplgroup-001.1abc4d.0001.usw2.cache.amazonaws.com"
      }, 
      "CacheClusterId": "myreplgroup-001"
   }, 
   {
      "CurrentRole": "replica", 
      "PreferredAvailabilityZone": "us-west-2b", 
      "CacheNodeId": "0001", 
      "ReadEndpoint": {
         "Port": 6379, 
         "Address": "myreplgroup-002.1abc4d.0001.usw2.cache.amazonaws.com"
      }, 
      "CacheClusterId": "myreplgroup-002"
   }, 
   {
      ...
   }
]

My response from this command doesn't contain CurrentRole at all:

"NodeGroupMembers": [
    {
        "PreferredAvailabilityZone": "ap-southeast-2c",
        "CacheNodeId": "0001",
        "CacheClusterId": "my-replication-group-0001-001"
    },
    {
        "PreferredAvailabilityZone": "ap-southeast-2b",
        "CacheNodeId": "0001",
        "CacheClusterId": "my-replication-group-0001-002"
    },
    {
        "PreferredAvailabilityZone": "ap-southeast-2a",
        "CacheNodeId": "0001",
        "CacheClusterId": "my-replication-group-0001-003"
    }
]

Is there an alternative aws cli command that can be used to determine the primary endpoint? It's easy enough to failover the primary node via the console, but as I can't identify which node is the current primary via console/cli it means guessing which nodes need to be targeted in my flushing script.

1 Answers

I found this paragraph in the docs for Elasticache:

Redis (cluster mode enabled) clusters with replicas, because they have multiple shards (API/CLI: node groups), which mean they also have multiple primary nodes, have a different endpoint structure than Redis (cluster mode disabled) clusters. Redis (cluster mode enabled) has a configuration endpoint which "knows" all the primary and node endpoints in the cluster. Your application connects to the configuration endpoint. Whenever your application writes to or reads from the cluster's configuration endpoint, Redis, behind the scenes, determines which shard the key belongs to and which endpoint in that shard to use. It is all quite transparent to your application.

I'm guessing the example output you gave is for a Replication Group with cluster mode disabled due to this snippet from the same article as above:

Redis (cluster mode disabled) clusters with replicas have three types of endpoints; the primary endpoint, the reader endpoint and the node endpoints.

I know this is probably a moot point (being you asked this question 2 years ago), but if you need to flush the primary nodes in your cluster (with cluster mode enabled), simply use the configuration endpoint and the changes will propagate to each node.

Related