Search code examples
dockergoredisredigo

redis: dial tcp [REDIS ADDRESS] connect: connection refused


I'm using redigo in go with docker. I have a server that processes incoming events and uses redis for rate limiting. One in every 100k+ connections or so I get the following error:

redis: dial tcp IP ADDRESS: connect: connection refused

The configuration is all inside docker using docker-compose. I've got sentry sending me these errors, I was wondering if there were any tweaks/settings I could do to remove/reduce this error

My redigo configuration is

redis := &redis.Pool{
    MaxActive: idleConnections,
    MaxIdle:   idleConnections,
    Wait:      true,
    Dial: func() (redis.Conn, error) {
        return redis.Dial("tcp", address, options...)
    },
}

Solution

  • You could retry the dial using some sort of exponential backoff:

    Dial: func() (redis.Conn, error) {
        c, err := redis.Dial("tcp", address, options...)
        for retries := 0; err != nil && retries < 5; retries++ {
            time.Sleep((50 << retries) * time.Millisecond)
            c, err = redis.Dial("tcp", address, options...)
        }
        return c, err
    },