invertase / sails-ioredis

Redis adapter for waterline with sentinel and cluster support support (ioredis)
http://redis.xyz
MIT License
4 stars 6 forks source link

AWS Elasticache Redis Cluster MOVED Redirect fails #4

Open neonexus opened 7 years ago

neonexus commented 7 years ago

When using AWS's Elasticache Redis Cluster (has built-in failover / replication / load balancing, no need to know the server addresses), occasionally Elasticache has to move data around, so it issues a MOVED Redirect, but this causes the adapter to fail, so Sails falls flat on it's face during lift. This is the console output I get when attempting to lift the app:

warn:  --minUptime not set. Defaulting to: 1000ms
warn:  --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
A hook (`orm`) failed to load!
{ Error
    at ReplyParser._parseResult (/var/www/backend/node_modules/ioredis/lib/parsers/javascript.js:39:14)
    at ReplyParser.execute (/var/www/backend/node_modules/ioredis/lib/parsers/javascript.js:139:20)
    at Socket.<anonymous> (/var/www/backend/node_modules/ioredis/lib/redis/event_handler.js:94:22)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    at TCP.onread (net.js:551:20)
  name: 'ReplyError',
  message: 'MOVED 3216 172.31.31.43:6379',
  command: 
   { name: 'get',
     args: [ 'waterline:{activepeople}:_sequences:id' ] } }

As you can probably tell, I have a model called ActivePeople that uses the adapter; it tracks live usage of the app (and wether or not the app is in focus).

Please help!

Salakar commented 7 years ago

@neonexus please make an issue on the ioredis repo, will take a look there.

neonexus commented 7 years ago

@Salakar there's no need, I found the fix yesterday as I was leaving work. Ultimately, it would be good to make mention of this in the documentation.

Because AWS Elasticache Redis Clusters are designed to be used with a single endpoint address, I was configuring the connection with "host". Didn't realize I had to use "hosts: [{host: 'Elasticache CFG Endpoint'}]" to trigger the clustering options. Once I fixed the config, everything worked again.

ghost commented 6 years ago

I was having trouble with this too so I'm posting my solution to connect to an AWS Redis (ElasticCache) cluster.

I had the most success using the ioredis package for connecting to Redis from a Node.js script on my EC2 instance.

I used the path that AWS calls the "Configuration Endpoint" for redisHost and redisPort .

My EC2 is behind a VPC, so I also made sure my inbound network connections allowed port 6379 as documented here.

This solution also fixed the MOVED error I would see when trying to set a value in Redis.

const Redis = require('ioredis');
var client = new Redis.Cluster([`//${redisHost}:${redisPort}`], {
  scaleReads: 'slave'
});
client.hget(key, id).then(data => {
    // manipulate data
});
client.hgetall(key).then(data=>{
    // manipulate data
});
Hemit99123 commented 3 months ago

thank you for the insight! will help me a lot :)