Error: Redis initialization failed: Error: Connection timeout

z-sako
PRO

a month ago

Error: Redis initialization failed: Error: Connection timeout

This error is likely to occur every time you deploy, using the node's redis library

import { createClient } from 'redis';

const initializeRedis = async (): Promise<RedisClient> => {
    if (redisClient) {
        return redisClient;
    }

    try {
        // Use Redis connection string if provided, otherwise use individual config
        const redisUrl = process.env.REDIS_URL;
        
        if (!redisUrl) {
            throw new Error('REDIS_URL is not set');
        }        
        redisClient = createClient({
            url: redisUrl,
            socket: {
                connectTimeout: 5000,
                reconnectStrategy: (retries) => {
                    if (retries > 10) {
                        console.error('❌ Redis max reconnection attempts reached');
                        return new Error('Max reconnection attempts reached');
                    }
                    return Math.min(retries * 100, 3000);
                }
            }
        });

        // Connect to Redis
        await redisClient.connect();
        console.log('✅ Redis connected successfully');
        return redisClient;
    } catch (error) {
        console.error('Failed to initialize Redis:', error);
        throw new Error(`Redis initialization failed: ${error}`);
    }
};
$10 Bounty

10 Replies

Railway
BOT

a month ago

Hey there! We've found the following might help you get unblocked faster:

If you find the answer from one of these, please let us know by solving the thread!


samgordon
PROTop 1% Contributor

a month ago

Can you share the REDIS_URL variable and what it's set to? Can you confirm the Redis service is running?

You may need to set ?family=0 but let's confirm the above first.


samgordon

Can you share the REDIS_URL variable and what it's set to? Can you confirm the Redis service is running?You may need to set ?family=0 but let's confirm the above first.

z-sako
PRO

a month ago

redis service is normal because I can connect to it through the public network.

set REDIS_URL to ${{Redis.REDIS_URL}} and also directly set the connection string redis://xxx:xxx@redis.railway.internal:6379


z-sako

redis service is normal because I can connect to it through the public network.set REDIS_URL to ${{Redis.REDIS_URL}} and also directly set the connection string redis://xxx:xxx@redis.railway.internal:6379

samgordon
PROTop 1% Contributor

a month ago

Great. Try adding ?family=0 to the end of that URL. In your case, it would be {{Redis.REDIS_URL}}?family=0.


a month ago

Hey!
Any update on this issue? did ?family=0 solve it?


titusg
PRO

17 days ago

I'm having the same issue. I'm using ioredis.
Adding ?family=0 did not solve the problem.
Using the public url works.
I'm also using another service (Infisical deployed from a template). When REDIS_URL is used the service crashes. When I use the public url everything works.


noahd

Hey!Any update on this issue? did ?family=0 solve it?

z-sako
PRO

17 days ago

not solve


titusg
PRO

17 days ago

The following solved my issue: i started my redis instance with "--bind ::" to make sure IPV6 was active


jasonfma
PRO

16 days ago

I just had a similar issue trying to connect via the internal ipv6 redis address. I set family: 6 and it started working. (This was for ioredis but similar fix for other node redis libraries)

EDIT: If you want to use ipv4 (public address) you should use ${{redis.REDIS_PUBLIC_URL}} instead of ${{redis.REDIS_URL}}


samgordon
PROTop 1% Contributor

15 days ago

You should NEVER be using the public URL within your project services. You can enable experimental IPv4 private networking in the feature flags tab if you for some reason need IPv4. IPv6 should be sufficient over private networking however.