skip to Main Content

Hi how can I connect my local golang code to Amazon Elastic Redis cluster with the help of go-redis sdk?

3

Answers


  1. With using github.com/go-redis/redis package , you can connect with Amazon Elastic Redis cluster

    import "github.com/go-redis/redis/v8"
    
    client := redis.NewClient(&redis.Options{
        Addr:     "the-aws-redis-cluster-host:port",
        Password: "optional-password",
    })
    
    state, err := client.Ping(context.Background()).Result()
    if err != nil {
        panic(err)
    }
    
    fmt.Println(state)
    

    Before connecting to the AWS redis cluster from local please make sure

    • The cluster instance is configured to accept the connections from your IP
    • The security group which is attached with the cluster allowing the incoming traffic on the port 6379.
    Login or Signup to reply.
  2. A note when using redis version less than 6.0.0 is that you need to leave your username empty. You also need to set up tls if your redis requires it. Here is an example code:

    import (
        "context"
        "crypto/tls"
        redisClient "github.com/redis/go-redis/v9"
        "strings"
        "fmt"
    )
    
    
    // URL example: redis://username:password@host:port
    opt, err := redisClient.ParseURL(${URL})
    if err != nil {
        panic(err)
    }
    
    // Remove username to avoid error: ERR wrong number of arguments for 'auth' command when using redis cloud
    // Root cause: Redis before 6.0.0 does not support username in URL
    // Reference: https://github.com/redis/go-redis/issues/1343
    opt.Username = ""
    opt.ClientName = ${ClientName}
    opt.DB = ${DB}
    opt.PoolSize = ${ConnectionPoolSize}
    opt.MinIdleConns = ${ConnectionMinimumIdleSize}
    
    // Set TLS config for redis cloud
    if !strings.Contains(${URL}, "localhost") {
        opt.TLSConfig = &tls.Config{
            MinVersion: tls.VersionTLS12,
        }
    }
    
    rdb = redisClient.NewClient(opt)
    
    // Test connection
    _, err = rdb.Ping(context.Background()).Result()
    if err != nil {
        panic(err)
    }
    
    fmt.Println("Successfully")
    
    
    
    Login or Signup to reply.
  3. Install the go-redis package :

    go get github.com/go-redis/redis/v8
    

    Import the go-redis package :

    package main
    import (
       "context"
       "github.com/go-redis/redis/v8"
    )
    

    Create a new Redis client by passing the Redis endpoint and credentials to the redis.NewClient function:

    
    Client := redis.NewClient(&redis.Options{
       Addr:     "YourRedisEndpoint:YourRedisPort",
       Password: "YourRedisPassword",
       DB:       0, // use default DB
    })
    

    you have to replace YourRedisEndpoint, YourRedisPort, and YourRedisPassword with the actual values of your Amazon Elastic Redis cluster. Find these values in the Elasticache console of your AWS account.

    Use the Redis client to perform Redis operations. To set a value in Redis, Use the Set method:

    ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second*10))
    defer cancel()
    err := Client.Set(ctx, "MyKey", "MyValue", 0).Err()
    if err != nil {
       fmt.Println("client set error : ",err)
    }
    

    This will set the value of the MyKey key to MyValue in Redis.

    You can now use the go-redis package to connect to your Amazon Elastic Redis cluster and perform Redis operations from your Golang code.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search