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

Sorry for the necroposting, but I’m facing the same error under the same conditions. How did you find out the internal url for Redis? I tried with .internal and it didn’t work, and I can’t find the docs for the internal url of redis

Sounds like you need a dedicated Redis instance to use the private network: Idle Redis connections eventually begin to hang, high Redis latency - #2 by kurt

I’m struggling with this right now too. Can’t find anything that is helping. The example linked in the next comment has a solution for python that uses a pool, but that doesn’t work for the js node-redis client…

Please let me know if you found a solution for this @rmortes !

I did, actually. Just run your own Redis instance while the provided one is not stable, and try again in a few months :slight_smile: