Skip to content

Redis create cluster issue #1782

Open
Open

Description

@ellik95

Hello,
I have a Redis MemoryDB cluster with 2 nodes in AWS and I'm trying to connect to it through createCluster, but the connection isn't established.
Here is how I try to connect:

import { createCluster } from 'redis';
(async () => {
    const REDIS_USERNAME = 'test-username';
    const REDIS_PASSWORD = 'test-pass';
    const cluster = createCluster({
        rootNodes: [
            {
                url: `rediss://redis-region-0001-001.amazonaws.com:6379`,
                username: REDIS_USERNAME,
                password: REDIS_PASSWORD,
            },
            {
                url: `rediss://redis-region-0001-002.amazonaws.com:6379`,
                username: REDIS_USERNAME,
                password: REDIS_PASSWORD,
            },
        ]
    });
    cluster.on('error', (err) => console.log('Redis Cluster Error', err));
    console.log('before connection');
    await cluster.connect();
    console.log('connected to cluster...');
    await cluster.set('key', 'value');
    const value = await cluster.get('key');
    console.log(value);
    await cluster.disconnect();
})();

The log connected to cluster... is never showed up.
If I add in the configuration of createCluster to the defaults option the master node endpoint like this:

defaults: {
            url: `rediss://redis-region-0001-001.amazonaws.com:6379`,
            username: REDIS_USERNAME,
            password: REDIS_PASSWORD,
        }

The connection is established and everything works fine!
Why do I need the defaults option? Shouldn't it work only with rootNodes?
Thank you!

Environment:

  • Node.js Version: 16-alpine3.12
  • Redis Server Version: 6
  • Node Redis Version: 4.0.0

Activity

  1. leibale commented on Dec 20, 2021

    @leibale
    Contributor

    As described in the clustering guide:

    • rootNodes - "An array of root nodes that are part of the cluster, which will be used to get the cluster topology. Each element in the array is a client configuration object. There is no need to specify every node in the cluster, 3 should be enough to reliably connect and obtain the cluster configuration from the server"
    • defaults - "The default configuration values for every client in the cluster. Use this for example when specifying an ACL user to connect with"

    node-specific properties should be listed in rootNodes, the rest in defaults

    createCluster({
     rootNodes: [{
       url: `rediss://redis-region-0001-001.amazonaws.com:6379`
     }, {
       url: `rediss://redis-region-0001-002.amazonaws.com:6379`
     }],
     defaults: {
       username: REDIS_USERNAME,
       password: REDIS_PASSWORD
     }
    });
  2. ellik95 commented on Dec 21, 2021

    @ellik95
    Author

    I had tried this also and the same thing happened! Could you please reopen the issue?

  3. leibale commented on Dec 21, 2021

    @leibale
    Contributor

    @ellik95 It didn't work even when the username & password are in the defaults object? can you please share your code?

  4. ellik95 commented on Dec 21, 2021

    @ellik95
    Author

    Yes, with this code:

    import { createCluster } from 'redis';
    (async () => {
        const REDIS_USERNAME = 'test-username';
        const REDIS_PASSWORD = 'test-pass';
        const cluster = createCluster({
            rootNodes: [
                {
                    url: `rediss://redis-region-0001-001.amazonaws.com:6379`
                },
                {
                    url: `rediss://redis-region-0001-002.amazonaws.com:6379`
                },
            ],
           defaults: {
                username: REDIS_USERNAME,
                password: REDIS_PASSWORD,
            }
        });
        cluster.on('error', (err) => console.log('Redis Cluster Error', err));
        console.log('before connection');
        await cluster.connect();
        console.log('connected to cluster...');
        await cluster.set('key', 'value');
        const value = await cluster.get('key');
        console.log(value);
        await cluster.disconnect();
    })();
    

    it is not connected. Only if I add the url of the master node to the defaults options works. Like this:

    defaults: {
                url: `rediss://redis-region-0001-001.amazonaws.com:6379`,
                username: REDIS_USERNAME,
                password: REDIS_PASSWORD,
            }
    

    And I was wondering why is this needed.

  5. leibale commented on Dec 21, 2021

    @leibale
    Contributor
    createCluster({
      rootNodes: [{
        url: `rediss://redis-region-0001-001.amazonaws.com:6379`
      }, {
        url: `rediss://redis-region-0001-002.amazonaws.com:6379`
      }],
      defaults: {
        tls: true,
        username: REDIS_USERNAME,
        password: REDIS_PASSWORD
      }
    });
  6. ellik95 commented on Jan 2, 2022

    @ellik95
    Author

    It's not working either and tls option does not exist in type Partial<RedisClusterClientOptions>.

  7. leibale commented on Jan 2, 2022

    @leibale
    Contributor

    @ellik95

    createCluster({
      rootNodes: [{
        url: `rediss://redis-region-0001-001.amazonaws.com:6379`
      }, {
        url: `rediss://redis-region-0001-002.amazonaws.com:6379`
      }],
      defaults: {
        socket: {
          tls: true
        },
        username: REDIS_USERNAME,
        password: REDIS_PASSWORD
      }
    });
  8. ellik95 commented on Jan 2, 2022

    @ellik95
    Author

    Unfortunately it is not working either. Only with the below code is:

    createCluster({
      rootNodes: [{
        url: `rediss://redis-region-0001-001.amazonaws.com:6379`
      }, {
        url: `rediss://redis-region-0001-002.amazonaws.com:6379`
      }],
      defaults: {
        url: `rediss://redis-region-0001-001.amazonaws.com:6379`
        username: REDIS_USERNAME,
        password: REDIS_PASSWORD
      }
    });
    
  9. ellik95 commented on Jan 21, 2022

    @ellik95
    Author

    Also when I connect I sometimes get the error ReplyError: MOVED 12539 rediss://redis-region-0001-002.amazonaws.com:6379 and I cannot get the value from the key.

  10. lohanbodevan commented on Feb 10, 2022

    @lohanbodevan

    Same issue as @ellik95.
    I'm trying to connect with a Redis cluster running on Kubernetes and the connection is always ReplyError: MOVED xxxx
    In my case I have Load Balancer to connect with, so I'm using createClient rather than createCluster.

    Connection through redis-client command line works fine, though.
    Is there any specific configuration we have to do with createClient when using Load Balanced redis cluster?

  11. 1 remaining item

  12. leibale commented on Feb 16, 2022

    @leibale
    Contributor

    @acb this line should fix it

  13. acb commented on Feb 16, 2022

    @acb

    @leibale oh, awesome! Do you have any idea when that might be pushed out in a release?

  14. leibale commented on Feb 16, 2022

    @leibale
    Contributor

    @acb Sunday/Monday :)

  15. acb commented on Feb 22, 2022

    @acb

    @leibale I just tried release 4.0.4 and unfortunately I'm still having the same problem. With the code

    const client = createCluster({
        rootNodes: [
            {
                url: "rediss://redis-1-url:6379",
            },
            {
                url: "rediss://redis-2-url:6379",
            },
            {
                url: "rediss://redis-3-url:6379",
            },
        ],
        defaults: {
            password: "password123"
        }
    });
    client.on("error", (err: any) => {
        console.log("REDIS ERROR");
        console.log(err);
    });
    
    console.log('connecting to redis');
    client.connect().then(() => {
        console.log('connected to redis');
    });
    

    I see 'connecting to redis' in my logs but it seems to hang and never actually connects. If I update my defaults to be

        defaults: {
            url: "rediss://redis-1-url:6379",
            password: "password123"
        }
    

    Then in my logs I get 'connecting to redis' followed immediately by 'connected to redis', but the connection is only to the one redis server that's in the default. If I try to sample random keys 2/3rds of them return with something like MOVED 11388 redis-2-url.

  16. leibale commented on Feb 22, 2022

    @leibale
    Contributor

    @acb try this

    const client = createCluster({
        rootNodes: [
            {
                url: "rediss://redis-1-url:6379",
            },
            {
                url: "rediss://redis-2-url:6379",
            },
            {
                url: "rediss://redis-3-url:6379",
            },
        ],
        defaults: {
            password: "password123",
            socket: { tls: true }
        }
    });
  17. acb commented on Feb 25, 2022

    @acb

    @leibale you're a wizard! That did it, I'm getting successful cluster connections now. I thought that having rediss as the protocol would handle the tls-ing but I guess cluster mode handles things a little differently.

    Thanks so much for the help!

  18. killdash9 commented on Mar 29, 2022

    @killdash9

    For me, I found both the :6379 and the extra s in rediss:// to be unnecessary. This worked:

    const client = createCluster({
        rootNodes: [
            {
                url: "redis://redis-1-url",
            },
            {
                url: "redis://redis-2-url",
            },
            {
                url: "redis://redis-3-url",
            },
        ],
        defaults: {
            password: "password123",
            socket: { tls: true }
        }
    });
    
    
  19. nixRajan commented on Jun 9, 2022

    @nixRajan

    I am still getting the Error MOVED 12218 ip:6379.
    Following is the code

    import {createClient} from "redis"; // redis: ^4.0.1
    const client = createClient({url: "redis://xyz.abc.clustercfg.use2.cache.amazonaws.com:6379"});
    await client.connect();
    console.log("client connected");
    console.log(await client.ping());
    

    I do get the output : "client connected" and "PONG".
    But when i do await client.get() or await client.set() i get the MOVED error.

  20. nixRajan commented on Jun 10, 2022

    @nixRajan

    This is to way to connect for above issue.

        const redis = require('redis');
    
        const client = redis.createCluster({
          rootNodes: [
            {
              url: `redis://${ConfigurationEndpoint}:${port}`,
            },
          ],
          useReplicas: true,
         });
    
  21. nibblesnbits commented on Aug 16, 2023

    @nibblesnbits

    Has this ever been addressed? I can't go to production with MemoryDB if this isn't fixed, and the approaches noted here result in an infinite loop of trying connection to localhost.

  22. leibale commented on Aug 16, 2023

    @leibale
    Contributor

    @nibblesnbits none of these worked for you?
    #1782 (comment)
    #1782 (comment)

    wanna share some code?

  23. nibblesnbits commented on Aug 16, 2023

    @nibblesnbits

    Well it looks like lack of sleep and attention to detail killed me. I missed that the call was createCluster, not createClient. 🤦‍♂️

    But now I'm curious; is it more correct to use the cluster(s)' hostname, or the individual nodes? Seems logical that the latter is more correct.

  24. timvi402 commented on Feb 27, 2024

    @timvi402

    For anyone trying to connect to a MemoryDb cluster using TLS, this has proven to be the solution:

        const redisClient = redis.createCluster({
            rootNodes: [{
                url: `redis://${redisHost}:6379`
            }],
            useReplicas: true,
            defaults: {
                socket: { tls: true }
            }
        });
    

    Well it looks like lack of sleep and attention to detail killed me. I missed that the call was createCluster, not createClient. 🤦‍♂️

    But now I'm curious; is it more correct to use the cluster(s)' hostname, or the individual nodes? Seems logical that the latter is more correct.

    AFAIK you want to use the cluster hostname(cluster endpoint), especially for MemoryDb where you may add or delete nodes dynamically.

  25. cb-rnag commented on Mar 4, 2024

    @cb-rnag

    For me, I found both the :6379 and the extra s in rediss:// to be unnecessary. This worked [...]

    @killdash9 correct. Not sure what the implications are, but redis:// instead of rediss:// appears to work. Port seems unnecessary too.

    With those changes in mind, my code now becomes:

    import { createCluster } from "redis";
    
    const socket = {
    	tls: true,
    	rejectUnauthorized: false, // Disable certificate validation
    };
    
    // Assuming you're using a Redis client that supports cluster mode and TLS
    const redisClient = createCluster({
    	rootNodes: [
    		{
    			url: "redis://<shard #1, node #1 endpoint>",
    		},
    		{
    			url: "redis://<shard #2, node #1 endpoint>",
    		},
    		{
    			url: "redis://<shard #3, node #1 endpoint>",
    		},
    	],
    	defaults: {
    		password: "TODO",
    		socket,
    	},
    });
    
    // Handle errors
    redisClient.on("error", (err) => {
    	console.error("Redis Cluster Error", err);
    });
    
    // Connect to the cluster
    await redisClient.connect().catch(console.error);
    

    Example of <shard #2, node #1 endpoint>

    example-0002-001.example.abc123.use1.cache.amazonaws.com
    
  26. huan086 commented on Dec 19, 2025

    @huan086

    I had issues with hanging at await redisClient.connect(). What worked for me is to remove useReplicas. Not sure if it was due to my Redis replicas being on port 6380 and my masters on 6379

    const client = createCluster({
        RESP: 3,
        //// useReplicas: true,
        rootNodes: [
            {
                url: "rediss://redis-1-url:6379",
            },
            {
                url: "rediss://redis-2-url:6379",
            },
            {
                url: "rediss://redis-3-url:6379",
            },
            {
                url: "rediss://redis-1-url:6380",
            },
            {
                url: "rediss://redis-2-url:6380",
            },
            {
                url: "rediss://redis-3-url:6380",
            },
        ],
        defaults: {
            username,
            password,
            socket: { tls: true, ca, cert, key }
        }
    }).on("error", (err) => console.log("Redis cluster error", err));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions