Redis SocketClosedUnexpectedlyError: Socket closed unexpectedly

Hello

On fly.io it seems that redis is disconnected consistently (every 1 minute)

SocketClosedUnexpectedlyError: Socket closed unexpectedly
    at Socket.<anonymous> (/Users/muhajir/dev/quran/quran.com-users-backend/node_modules/@redis/client/dist/lib/client/socket.js:156:118)
    at Object.onceWrapper (node:events:646:26)
    at Socket.emit (node:events:526:28)
    at Socket.emit (node:domain:475:12)
    at TCP.<anonymous> (node:net:687:12)
redis connected 2022-07-07T06:08:34.656Z
SocketClosedUnexpectedlyError: Socket closed unexpectedly
    at Socket.<anonymous> (/Users/muhajir/dev/quran/quran.com-users-backend/node_modules/@redis/client/dist/lib/client/socket.js:156:118)
    at Object.onceWrapper (node:events:646:26)
    at Socket.emit (node:events:526:28)
    at Socket.emit (node:domain:475:12)
    at TCP.<anonymous> (node:net:687:12)
redis connected 2022-07-07T06:09:34.726Z
SocketClosedUnexpectedlyError: Socket closed unexpectedly
    at Socket.<anonymous> (/Users/muhajir/dev/quran/quran.com-users-backend/node_modules/@redis/client/dist/lib/client/socket.js:156:118)
    at Object.onceWrapper (node:events:646:26)
    at Socket.emit (node:events:526:28)
    at Socket.emit (node:domain:475:12)
    at TCP.<anonymous> (node:net:687:12)
redis connected 2022-07-07T06:10:34.781Z
SocketClosedUnexpectedlyError: Socket closed unexpectedly
    at Socket.<anonymous> (/Users/muhajir/dev/quran/quran.com-users-backend/node_modules/@redis/client/dist/lib/client/socket.js:156:118)
    at Object.onceWrapper (node:events:646:26)
    at Socket.emit (node:events:526:28)
    at Socket.emit (node:domain:475:12)
    at TCP.<anonymous> (node:net:687:12)
redis connected 2022-07-07T06:11:34.847Z
SocketClosedUnexpectedlyError: Socket closed unexpectedly
    at Socket.<anonymous> (/Users/muhajir/dev/quran/quran.com-users-backend/node_modules/@redis/client/dist/lib/client/socket.js:156:118)
    at Object.onceWrapper (node:events:646:26)
    at Socket.emit (node:events:526:28)
    at Socket.emit (node:domain:475:12)
    at TCP.<anonymous> (node:net:687:12)
redis connected 2022-07-07T06:12:34.910Z
SocketClosedUnexpectedlyError: Socket closed unexpectedly
    at Socket.<anonymous> (/Users/muhajir/dev/quran/quran.com-users-backend/node_modules/@redis/client/dist/lib/client/socket.js:156:118)
    at Object.onceWrapper (node:events:646:26)
    at Socket.emit (node:events:526:28)
    at Socket.emit (node:domain:475:12)
    at TCP.<anonymous> (node:net:687:12)
redis connected 2022-07-07T06:13:35.143Z
SocketClosedUnexpectedlyError: Socket closed unexpectedly
    at Socket.<anonymous> (/Users/muhajir/dev/quran/quran.com-users-backend/node_modules/@redis/client/dist/lib/client/socket.js:156:118)
    at Object.onceWrapper (node:events:646:26)
    at Socket.emit (node:events:526:28)
    at Socket.emit (node:domain:475:12)
    at TCP.<anonymous> (node:net:687:12)
redis connected 2022-07-07T06:14:35.203Z
SocketClosedUnexpectedlyError: Socket closed unexpectedly
    at Socket.<anonymous> (/Users/muhajir/dev/quran/quran.com-users-backend/node_modules/@redis/client/dist/lib/client/socket.js:156:118)
    at Object.onceWrapper (node:events:646:26)
    at Socket.emit (node:events:526:28)
    at Socket.emit (node:domain:475:12)
    at TCP.<anonymous> (node:net:687:12)
redis connected 2022-07-07T06:15:35.257Z
SocketClosedUnexpectedlyError: Socket closed unexpectedly
    at Socket.<anonymous> (/Users/muhajir/dev/quran/quran.com-users-backend/node_modules/@redis/client/dist/lib/client/socket.js:156:118)
    at Object.onceWrapper (node:events:646:26)
    at Socket.emit (node:events:526:28)
    at Socket.emit (node:domain:475:12)
    at TCP.<anonymous> (node:net:687:12)
redis connected 2022-07-07T06:16:35.315Z
SocketClosedUnexpectedlyError: Socket closed unexpectedly
    at Socket.<anonymous> (/Users/muhajir/dev/quran/quran.com-users-backend/node_modules/@redis/client/dist/lib/client/socket.js:156:118)
    at Object.onceWrapper (node:events:646:26)
    at Socket.emit (node:events:526:28)
    at Socket.emit (node:domain:475:12)
    at TCP.<anonymous> (node:net:687:12)
redis connected 2022-07-07T06:17:35.363Z
SocketClosedUnexpectedlyError: Socket closed unexpectedly
    at Socket.<anonymous> (/Users/muhajir/dev/quran/quran.com-users-backend/node_modules/@redis/client/dist/lib/client/socket.js:156:118)
    at Object.onceWrapper (node:events:646:26)
    at Socket.emit (node:events:526:28)
    at Socket.emit (node:domain:475:12)
    at TCP.<anonymous> (node:net:687:12)
redis connected 2022-07-07T06:18:35.491Z

But on my local machine, it works fine.

Here’s my redis fly.toml

# fly.toml file generated for quran-staging-redis on 2022-07-03T00:34:27+07:00

app = "quran-backend-redis"
kill_signal = "SIGINT"
kill_timeout = 5
processes = []

[build]
image = "flyio/redis:6.2.6"

[env]

[experimental]
allowed_public_ports = []
auto_rollback = true

[mounts]
destination = "/data"
source = "redis_server"

[metrics]
port = 9091
path = "/metrics"

[[services]]
internal_port = 6379
protocol = "tcp"

[[services.ports]]
port = 10000

[[services.tcp_checks]]
interval = "10000"
timeout = "2000"

We are using redis on node. Here’s the code how we connect to redis

import * as Sentry from '@sentry/node';
import { createClient } from 'redis';

export type RedisClientType = ReturnType<typeof createClient>;

export const initRedis = async () => {
  const client = createClient({
    url: process.env.REDIS_URL,
  });
  client.on('connect', () => {
    console.log('redis connected', new Date().toISOString());
  });
  client.on('error', (err) => {
    Sentry.captureException(err);
    // eslint-disable-next-line no-console
    console.error(err);
  });
  await client.connect();
  return client;
};

Am I missing something? or is there any other information that I can provide?

So it seems that the issue only happen when conneting to redis using public network

switching to private network solved the issue