R
Railway•4mo ago
Nicky

Redis connection closed on private network

Hi, I'm facing the following issue with redis. I have a nestjs application where I'm using redis as the transport layer between microservices.
const redisURL = new URL(process.env.PUB_SUB_URL)

await app.connectMicroservice<MicroserviceOptions>({
transport: Transport.REDIS,
options: {
port: parseInt(redisURL.port),
host: redisURL.hostname,
username: redisURL.username,
password: redisURL.password,
},
})
await app.startAllMicroservices()
const redisURL = new URL(process.env.PUB_SUB_URL)

await app.connectMicroservice<MicroserviceOptions>({
transport: Transport.REDIS,
options: {
port: parseInt(redisURL.port),
host: redisURL.hostname,
username: redisURL.username,
password: redisURL.password,
},
})
await app.startAllMicroservices()
When I set the PUB_SUB_URL to ${{pubsub.REDIS_URL}} which is the public url everything is fine. But if I change the variable to ${{pubsub.REDIS_PRIVATE_URL}} I a connection closed error.
ERROR [Server] Error: getaddrinfo ENOTFOUND pubsub.railway.internal
ERROR [Server] Error: getaddrinfo ENOTFOUND pubsub.railway.internal
I've read about the private network initialisation times on railway but the redis service is already up and running at the time of deploying my nestjs app projectId: 8a8bfe2d-1094-4b5f-834a-eba6388d0180
Solution:
put the sleep back to 3 seconds, and set family to 0 in the options for that transport
Jump to solution
12 Replies
Percy
Percy•4mo ago
Project ID: 8a8bfe2d-1094-4b5f-834a-eba6388d0180
Brody
Brody•4mo ago
I've read about the private network initialisation times on railway but the redis service is already up and running at the time of deploying my nestjs app
this applies to your app itself, please try adding a 3 second sleep to your start script
Nicky
Nicky•4mo ago
I've tried to do it within my app but no luck
function sleep(ms: number) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}

async function bootstrap() {
await sleep(3000)

const app = await NestFactory.create(AppModule)

app.use(cookieParser())

const redisURL = new URL(process.env.PUB_SUB_URL)

await app.connectMicroservice<MicroserviceOptions>({
transport: Transport.REDIS,
options: {
port: parseInt(redisURL.port),
host: redisURL.hostname,
username: redisURL.username,
password: redisURL.password,
},
})
await app.startAllMicroservices()

const port = process.env.DATA_SERVICE_PORT
const host = process.env.DATA_SERVICE_HOST
await app.listen(port, host)

Logger.log(`🚀 Application is running on: http://${host}:${port}`)
}
function sleep(ms: number) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}

async function bootstrap() {
await sleep(3000)

const app = await NestFactory.create(AppModule)

app.use(cookieParser())

const redisURL = new URL(process.env.PUB_SUB_URL)

await app.connectMicroservice<MicroserviceOptions>({
transport: Transport.REDIS,
options: {
port: parseInt(redisURL.port),
host: redisURL.hostname,
username: redisURL.username,
password: redisURL.password,
},
})
await app.startAllMicroservices()

const port = process.env.DATA_SERVICE_PORT
const host = process.env.DATA_SERVICE_HOST
await app.listen(port, host)

Logger.log(`🚀 Application is running on: http://${host}:${port}`)
}
Brody
Brody•4mo ago
are you deplying with a dockerfile?
Nicky
Nicky•4mo ago
No I could try if that's a possible fix
Brody
Brody•4mo ago
try increasing the sleep time what package does the redis transport use? ioredis?
Nicky
Nicky•4mo ago
Yes, ioredis@5.3.2 judging by the logs. Will try 20sec for testing
Solution
Brody
Brody•4mo ago
put the sleep back to 3 seconds, and set family to 0 in the options for that transport
Nicky
Nicky•4mo ago
It looks like nestjs doesn't allow to set the family when defining the transport... I also have another service which is using redis through ioredis and setting the family allowed for using the private url which is great. Is there any other way to make this work without the family?
jeremy
jeremy•4mo ago
Have you try setting it anyway in the options object? the interface doesn't contain family https://github.com/nestjs/nest/blob/master/packages/microservices/client/client-proxy.ts but the options are passed down to the redis client anyway https://github.com/nestjs/nest/blob/master/packages/microservices/client/client-redis.ts#L76
Nicky
Nicky•4mo ago
Tried earlier and got an error but will try again as I was trying all sorts of things at the time Ok, that actually did it! Must've been something unrelated earlier. Thanks a lot!
Brody
Brody•4mo ago
I'm glad it worked because that's exactly what I said to do 😆