Redis Error: Error: Client network socket disconnected before secure TLS connection was established

aleferreinert
HOBBY

14 days ago

Hi, i have a persistent problem with redis:
Logs:
Reconnecting to Redis in 1000ms

Redis Error: Error: read ECONNRESET

at TLSWrap.onStreamRead (node:internal/stream_base_commons:218:20) {

errno: -104,

code: 'ECONNRESET',

syscall: 'read'

}

Reconnecting to Redis in 1000ms

Redis Error: Error: Client network socket disconnected before secure TLS connection was established

at TLSSocket.onConnectEnd (node:_tls_wrap:1730:19)

at TLSSocket.emit (node:events:536:35)

at endReadableNT (node:internal/streams/readable:1698:12)

at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {

code: 'ECONNRESET',

path: undefined,

host: 'switchback.proxy.rlwy.net',

port: 49250,

localAddress: undefined

}

// src\redis\client.ts

import Redis from 'ioredis'
import { env } from '../env'

export const redis = new Redis(env.REDIS_URL, {
  tls: {
    rejectUnauthorized: false,
  },
  connectTimeout: 15000,
  retryStrategy: times => Math.min(times * 30, 1000),
})

redis.on('command', command => {
  console.log('Redis Command:', command)
})

redis.on('error', err => {
  console.error('Redis Error:', err)
})

redis.on('connect', () => {
  console.log('Connected to Redis!')
})

redis.on('reconnecting', (delay: number) => {
  console.log(`Reconnecting to Redis in ${delay}ms`)
})

// ENV
REDIS_URL="rediss://default:PASSWORD@switchback.proxy.rlwy.net:49250"

Repository: https://github.com/AleferReinert/devstage-api

$10 Bounty

2 Replies

Railway
BOT

14 days ago


14 days ago

This thread has been marked as public for community involvement, as it does not contain any sensitive or personal information. Any further activity in this thread will be visible to everyone.

Status changed to Open brody 14 days ago


idiegea21
HOBBYTop 10% Contributor

13 days ago

This error usuall occurs when you are trying to connect to Redis over TLS from a Railway service that’s on the same project but internal Redis URLs don’t support TLS.

So a suggested fix will be to remove the tlsoption entirely when connecting to the internal Railway Redis.

export const redis = new Redis(env.REDIS_URL, {
  // remove the tls block
  // tls: {
  //   rejectUnauthorized: false,
  // },
  connectTimeout: 15000,
  retryStrategy: times => Math.min(times * 30, 1000),
})

but if you are connecting externally(not from railway) then you do need TLS and in that case, double-check your environment credentials (like password, Redis URL, and port) to make sure they’re correct.