Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to ElastiCache cluster using node.js

Tags:

We know that ElastiCache is not recommended to be accessed outside Amazon instances, so we're trying below stuff inside Amazon EC2 instances only.

We've got a ElastiCache Redis Cluster with 9 nodes. When we try to connect to it using normal redis implementation, it throws some Moved errors

Have tried the retry strategy method as per @Miller. Have also tried RedisCluster with unstable and stable (poor man) implementations.

None of these implementations are working. Any suggestions please?

like image 479
xameeramir Avatar asked May 09 '17 14:05

xameeramir


People also ask

How do I access my ElastiCache cluster?

Sign in to the AWS Management Console and open the ElastiCache console at https://console.aws.amazon.com/elasticache/ . To see a list of your clusters running the Memcached engine, in the left navigation pane, choose Memcached.

How do I connect to a Redis cluster?

Open the Command Prompt and change to the Redis directory and run the command c:\Redis>redis-cli -h Redis_Cluster_Endpoint -p 6379 . Run Redis commands. You are now connected to the cluster and can run Redis commands like the following.

Can Lambda connect to ElastiCache?

Create a Lambda function to access the ElastiCache cluster. When you create the Lambda function, you provide subnet IDs in your Amazon VPC and a VPC security group to allow the Lambda function to access resources in your VPC.


1 Answers

Sharing the code for future readers:

var RedisClustr = require('redis-clustr'); var RedisClient = require('redis'); var config = require("./config.json");  var redis = new RedisClustr({     servers: [         {             host: config.redisClusterHost,             port: config.redisClusterPort         }     ],     createClient: function (port, host) {         // this is the default behaviour         return RedisClient.createClient(port, host);     } });  //connect to redis redis.on("connect", function () {   console.log("connected"); });  //check the functioning redis.set("framework", "AngularJS", function (err, reply) {   console.log("redis.set " , reply); });  redis.get("framework", function (err, reply) {   console.log("redis.get ", reply); }); 
like image 175
xameeramir Avatar answered Sep 17 '22 17:09

xameeramir