Unable to connect to redis (nor redislabs hosted, nor self, railway hosted)

orov-ioPRO

a year ago

Hi,

I'm having problems to connect a my golang service with a redis db, using the "github.com/redis/go-redis/v9" package.

I always obtain this error:

1.- If I use the redilslabs db: unable to connect to redis: dial tcp: lookup redis-11333.c77.eu-west-1-1.ec2.cloud.redislabs.com: i/o timeout

2.- If I use the selfhosted, railway private url: panic: unable to connect to redis: dial tcp: lookup redis.railway.internal: i/o timeout

I tried these combinations:

  • Use alpine:latest, ubuntu:latest and golang:1.22 (this last is the same image that i use for local development, and I'm able to connect with the redislabs redis database)

  • I print some logs to be sure that env variables are correct, double check they.

  • Service is able to connect and ping a mongo db hosted in atlas.

  • I wait 5 seconds prior to try ping the redis db.

The chunk of code that manages the redis connection is:

func MustNewConnectedRedis(ctx context.Context, connectionString string) *Redis {
    r := NewRedis(ctx)
    err := r.Connect(connectionString)
    if err != nil {
        panic(fmt.Sprintf("unable to connect to redis: %s", err.Error()))
    }
    return r
}

func NewRedis(ctx context.Context) *Redis {
    return &Redis{
        ctx: context.Background(),
    }
}

func (r *Redis) Ping() error {
    return r.client.Ping(r.ctx).Err()
}

func (r *Redis) Connect(connectionString string) error {
    if r.client != nil {
        return ErrRedisAlreadyConnect
    }
    opt, err := redis.ParseURL(connectionString)
    if err != nil {
        return err
    }

    fmt.Printf("redis options: %+v\n", opt)
    r.client = redis.NewClient(opt)
    return r.Ping()
}

What are I doing wrong? The printed redis options looks ok, and looks like the printed redis options in my local env, that is able to connect to redislabs with same redis url.

Thanks!!!

View Deploy details

ⓘ Deployment information is only viewable by project members and Railway employees.

12 Replies

a year ago

What is the current image you are using in your Dockerfile?


orov-ioPRO

a year ago

golang:1.22:

Dockerfile is so simple:

FROM golang:1.22

WORKDIR /root/
COPY ./app .

CMD ["./app"]

I also tried ubuntu:latest and alpine:latest


orov-ioPRO

a year ago

If it helps in the diagnosis, service has no domain generated in the Networking->Public Networking tab, because I have another services running caddy as a reverse proxy, so all my services can be accessed via api.example.com/service/endpoint...


a year ago

First, can you please write a proper Dockerfile? one that copies in your project, installs the deps and builds the binary.


orov-ioPRO

a year ago

Yes, I can, bu why? I already have the binary compiled and tested in my pipelines, so I want to reuse it. Is this impossible?


a year ago

It's just not typically done, personally I would go as far as to say its bad practice.

Either way, please look at this docs section -

https://docs.railway.app/guides/private-networking#initialization-time


orov-ioPRO

a year ago

ok, you are the expert. Now I have this```Dockerfile

From golang:1.22 as builder

ARG GITHUB_PAT
ARG GOPRIVATE

WORKDIR /src/app
RUN git config --global url."https://${GITHUB_PAT}:x-oauth-basic@github.com/kubik-app".insteadOf "https://github.com/kubik-app"

COPY go.* .
RUN go mod download

COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o server

FROM ubuntu:latest as prod
WORKDIR /root/
COPY --from=builder /src/app ./app
CMD ["./app/server"]

```

I also try with alpine:latest and golang:1.22 as the prod stage. Connection error is the same, from logs:

panic: unable to connect to redis: dial tcp: lookup redis-15298.c77.eu-west-1-1.ec2.cloud.redislabs.com: i/o timeout

I also try with this simple dockerfile:

From golang:1.22 as builder

ARG GITHUB_PAT
ARG GOPRIVATE

WORKDIR /src/app
RUN git config --global url."https://${GITHUB_PAT}:x-oauth-basic@github.com/kubik-app".insteadOf "https://github.com/kubik-app"

COPY go.* .
RUN go mod download

COPY . .
RUN go build -o server

CMD [ "./server" ]

but it has the same result: panic: unable to connect to redis: dial tcp: lookup redis-11333.c77.eu-west-1-1.ec2.cloud.redislabs.com: i/o timeout


Do you have Static IPs enabled?


a year ago

Please connect to a Railway hosted Redis database via the private network, see the docs page I linked.


orov-ioPRO

a year ago

Do you have Static IPs enabled?

No, I doesn't have Static IPs enabled, i'm trying it


redis-11333.c77.eu-west-1-1.ec2.cloud.redislabs.com

Are you able to connect to this locally via redis-cli?

Could you try increasing redis-go's timeouts [0]?

[0] https://github.com/redis/go-redis/blob/master/options.go#L83-L91


orov-ioPRO

a year ago

Hi got it. I was testing all your suggestions.

Static IPs doesn't works

I can connect with redislabs when I execute the same docker container locally, via docker-compose.

I can connect with redislabs via redis-cli, with same credentials:

```bash

redis-cli -u redis://default:**********@redis-11333.c77.eu-west-1-1.ec2.cloud.redislabs.com:11333
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
redis-11333.c77.eu-west-1-1.ec2.cloud.redislabs.com:11333> PING
PONG
redis-11333.c77.eu-west-1-1.ec2.cloud.redislabs.com:11333> 

This works, with no Static IPs:[0] https://github.com/redis/go-redis/blob/master/options.go#L83-L91

func (r *Redis) Connect(connectionString string) error {
	if r.client != nil {
		return ErrRedisAlreadyConnect
	}
	opt, err := redis.ParseURL(connectionString)
	if err != nil {
		return err
	}

	opt.DialTimeout = 30 * time.Second
	opt.ReadTimeout = 30 * time.Second

	opt.MaxRetries = 10
	opt.MaxRetryBackoff = 1 * time.Second

	fmt.Printf("redis options: %+v\n", opt)
	r.client = redis.NewClient(opt)
	return r.Ping()
}

This does not work

CMD sleep 5 && /root/app

I just change the redislabs urls and password just for security :)

I hate that press shift + enter add a new line in this editor, but the server trim this new line XDXDXD

Thanks a lot @rc and @brody


Unable to connect to redis (nor redislabs hosted, nor self, railway hosted) - Railway Help Station