Search code examples
gorediselastic-cachego-redis

I want to connect to Elasticcache for redis in which cluster mode is enabled. I want to connect it from golang (go-redis pkg)


In Golang,Go redis has 2 clients, redis.NewClient and redis.NewClusterClient.

I am not sure which one to use for connecting to elasticcache for redis.

I want to use the client which will only connect to one endoint of cluster and that enpoint will make sure I get or set the keys for redis. As clusters in elasticcache are aware of eachother

Any help on this would be very much appreciated


Solution

  • I would like to use NewClusterClient, for your question

    I want to use the client which will only connect to one endoint of cluster and that enpoint will make sure I get or set the keys for redis

    Please make sure all those parameters are false

            ReadOnly:       false,
            RouteRandomly:  false,
            RouteByLatency: false,
    

    Sample codes

    import (
      goredis "github.com/go-redis/redis/v8"
    )
    
    goredis.NewClusterClient(&goredis.ClusterOptions{
            Addrs:        []string{"cluster-configuration-endpoint:6379"},
            Password:     "password",
            PoolSize:     10, 
            MinIdleConns: 10,
    
            DialTimeout:  5 * time.Second,
            ReadTimeout:  3 * time.Second,
            WriteTimeout: 3 * time.Second,
            PoolTimeout:  4 * time.Second,
    
            IdleCheckFrequency: 60 * time.Second,
            IdleTimeout:        5 * time.Minute,
            MaxConnAge:         0 * time.Second,
    
            MaxRetries:      10,
            MinRetryBackoff: 8 * time.Millisecond,
            MaxRetryBackoff: 512 * time.Millisecond,
    
            TLSConfig: &tls.Config{
                InsecureSkipVerify: true,
            },
    
            ReadOnly:       false,
            RouteRandomly:  false,
            RouteByLatency: false,
        })