How to connect to managed Redis database with Golang

Posted April 21, 2020 6.2k views

I was wondering if anyone has had any experience with using the package to connect to a Digital Ocean managed Redis database?

I’ve tried the basic connection from the README file

client  := redis.NewClient(&redis.Options{
        Addr:     "CONNECT_STRING : PORT",
        Password: "PASSWORD", // password set
        DB:       0,  // use default DB
        MaxRetries: 3, //added after a google suggestion

    pong, err = client.Ping().Result()

    if err != nil {
        fmt.Printf("Cannot Ping: %v\n", err.Error())
    } else {
        fmt.Printf("Pong: %v\n", pong)

But when I come to test the connect, I don’t receive Pong as a reply, I get an error with the message


And when I come to check the logs in the control panel, I cannot see anything that looks like a connection attempt.

Any help would be greatly appreciated.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
3 answers

Wish I could help. Just chiming in to say that I have the same issue, but with nodejs and ioredis. I’ve entered my credentials as supplied, and I’ve tried connecting with just the rediss:// connection string, no dice.

The Digital Ocean docs mention using redli to connect to the managed instance, which works fine, but I don’t see how that is a solution to anything other than wanting to inspect. Haven’t seen any tutorials or docs about how to connect via an application.

Actually, maybe this will help you? I couldn’t figure it out either, and generally it seems like configuring tls for redis is a bit of a pain.

What I ended up doing, which works just fine for my purposes, was run redis as a docker container on the host. So I have a docker-compose setup that launches my app and redis as separate services, and the app depends on the redis service. Then I just set ‘redis’ (or whatever you decide to name the service) as my redis host.

I’m sure you could do this easily without a docker-compose setup, but this has been my local dev situation so it was easier to just translate that over for me.

Hope it helps

It’s bad, that DO doesn’t have an option to disable tls for Redis. I recently migrated from dickerized Redis on droplet - to managed DO Redis and had to change all connection code to Redis..

I found 2 ways to do that in go:

1) Native go, I’m using redigo and there is TLS option.I haven’t tried it, but seems like it does tls.
The problem with this approach for me was, that I’m using Iris framework, that doesn’t currently have an option to add this flag.

2) Global way with stunnel. I followed this instruction and simply added it to my docker app.

  • apt-get install stunnel4 -y
  • download cert wget
  • create my.conf
client = yes
foreground = yes
debug = info
output = stunnel.log

accept  =<do-redis-port>
connect = <do-redis-host>:<do-redis-port>
TIMEOUTclose = 0
CAfile = <path to my.conf>
  • tunnel my.conf