0

We are about to start using redis in elastic cache. I can save a config file with my endpoints, that's fine. But if we decide to add on another replica, and we are using elastic beanstalk, it means rebuilding the app on all the servers just to add that in. I could put them in a file on S3, or even query the API, (there is a handy call to get the info about an elastic cache cluster, that contains the endpoint.)

But I don't want to do that every time I want to set up a connection to redis. I was considering in this case setting up a global variable that contains the end points, with a timestamp, and retrieving the information once an hour or half hour.

I know that global variables are considered loathsome by some, but I can't think of any other way to do this. I could save the results in redis itself, but I don't really want to hit redis before every time I want to call redis.

Any thoughts on the viability of this, or other ways to achieve the same end? I want to auto discover any new redis/elastic cache end points.

CargoMeister
  • 4,199
  • 6
  • 27
  • 44

1 Answers1

0

So, I wrote a very simply function, using the AWS SDK and setinterval.

aws_config = require("./config/aws_config.json");
AWS = require('aws-sdk');
AWS.config.update(aws_config);

var elasticache = new AWS.ElastiCache();

var elastic_cache_endpoints = {};

redis_end_points = {};

check_redis_endpoints();

setInterval(function ()
{
    check_redis_endpoints();
}, 10000);

function check_redis_endpoints()
{
    elasticache.describeReplicationGroups(
    {
        ReplicationGroupId:'test-dev'
    },
    function(err, data)
    {
        redis_end_points.write_end_point = data["ReplicationGroups"][0]["NodeGroups"][0]["PrimaryEndpoint"]["Address"];

        var replication_groups = data["ReplicationGroups"][0]["NodeGroups"][0]["NodeGroupMembers"];

        var end_points = [];

        Object.keys(replication_groups).forEach(function(element)
        {
            var read_end_point = replication_groups[element]["ReadEndpoint"]["Address"];
            end_points.push(read_end_point);
        });

        if (end_points.length > 0)
                redis_end_points.read_end_points = end_points;
    });
}

Which results in a nice structure that gets refreshed every x seconds.

{
    write_end_point: 'test-dev.ababababababa.cache.amazonaws.com',
    read_end_points: [ 'test-dev-001.ababababab.cache.amazonaws.com', 'test-dev-002.ababababb.cache.amazonaws.com' ]
}
CargoMeister
  • 4,199
  • 6
  • 27
  • 44