Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to connect to redis cluster with node client, what am I doing wrong?

I have spun up an AWS ElastiCache redis instance running in clustered mode, which currently has 1 shard and 2 nodes

In order to connect to it from my local machine I have opened up an SSH tunnel using my SSH config file

Host myRedisTunnel
  HostName 1.2.3.4
  ...
  LocalForward 6378 5.6.7.8:6379

the tunnel works, I can connect to my VPC successfully

$ ssh myRedisTunnel
Welcome to Ubuntu 18.04.6 LTS (GNU/Linux 5.4.0-1060-aws x86_64)
...

I can connect to the redis cluster locally via redis-cli after opening my tunnel and passing -c as an argument for clustered mode

$ redis-cli -c -h localhost -p 6378
localhost:6378> ping
PONG

but when I try to use redis for nodejs it wont connect, it just times out, am i missing some configuration settings, or is it physically impossible to connect to my remote redis via tunnel?

const { createCluster } = require('redis')

const client = createCluster({
  rootNodes: [
    { url: 'redis://localhost:6378' }
  ]
})

await client.connect()
const res = await client.ping()
console.log({ res })
Error: Connection timeout
      at Socket.<anonymous> (node_modules/@node-redis/client/dist/lib/client/socket.js:163:124)
      at Object.onceWrapper (node:events:513:28)
      at Socket.emit (node:events:394:28)
      at Socket._onTimeout (node:net:486:8)
      at listOnTimeout (node:internal/timers:557:17)
      at processTimers (node:internal/timers:500:7)

I have tried several nodejs clients for redis and all of them have timed out in the same way, so I know the issue has to either be that I have a config setting wrong in my nodejs redis client configuration - or it has something to do with only one of the redis ip addresses is accessible via tunnel, all the rest of the cluster would likely not be accessible unless i open tunnels for each one. Im just at a loss for how mock my production environment in development so i can write code.

like image 218
alilland Avatar asked Jan 30 '26 08:01

alilland


1 Answers

  1. As you've pointed out, you'll need to create a tunnel for each node in the cluster
  2. The clients are using the CLUSTER NODES command in order to discover all the nodes and the cluster, and this command will return the actual IPs and ports of the nodes, not the tunnels. You can use ioredis with "NAT Mapping" to solve that.

Edit: If you found this question but using node-redis, you can use the nodeAddressMap option

like image 187
Leibale Eidelman Avatar answered Jan 31 '26 21:01

Leibale Eidelman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!