Yash Dixit
Yash Dixit

Reputation: 276

Reusing Redis Connection: Socket Closed Unexpectedly - node-redis

First, let me tell you how I'm using Redis connection in my NodeJS application:

class RDB {

    static async getClient() {
        if (this.client) {
            return this.client
        }

        let startTime = Date.now();

        this.client = createClient({
            url: config.redis.uri
        });

        await this.client.connect();

        return this.client;
    }

}

For some reason - that I don't know - time to time my application crashes giving an error without any reason - this happens about once or twice a week:

Error: Socket closed unexpectedly

Now, my questions:

  1. Is using Redis connections like this alright? Is there something wrong with my approach?
  2. Why does this happen? Why is my socket closing unexpectedly?
  3. Is there a way to catch this error (using my approach) or any other good practice for implementing Redis connections?

Upvotes: 13

Views: 20041

Answers (6)

Janiis
Janiis

Reputation: 1576

There is option pingInterval you can pass to createClient function to keep socket alive. Ping server every 3 seconds.

export const redis = createClient({
    url: 'redis://localhost:6379',
    pingInterval: 3000,
});

Upvotes: 1

Phat Tran
Phat Tran

Reputation: 3870

I switched to ioredis with the same configuration and this issue is gone.

Upvotes: 0

Giangimgs
Giangimgs

Reputation: 1200

I got the same issue (Socket Closed Unexpectedly) but only with the remote Redis servers, so I figured out that maybe there is a problem with the TLS. So I tried to use redis:// instead of rediss://, and yes it was working stable.

Upvotes: 4

Ján Sopko
Ján Sopko

Reputation: 89

You should declare a private static member 'client' of the RDB class, like this:

private static client;

In a static method, you can't reference instance of 'this', you need to reference the static class member like this:

RDB.client

And it would be better to check, whether the client's connection is open, rather than simply checking if the client exists (considering you are using the 'redis' npm library). Like this:

if (RDB.client && RDB.client.isOpen)

After the changes, your code should look like this:

class RDB {
    private static client;

    static async getClient() {
        if (RDB.client && RDB.client.isOpen) {
            return RDB.client;
        }

        RDB.client = createClient({
            url: config.redis.uri
        });

        await RDB.client.connect();

        return RDB.client;
    }
}

Note: the connect() method and isOpen property only exist in redis version ^4.0.0.

Upvotes: 3

Yash Dixit
Yash Dixit

Reputation: 276

I solved this using the 'error' listener. Just listening to it - saves the node application from crashing.

client.on("error", function(error) {
   console.error(error);
   // I report it onto a logging service like Sentry. 
});

Upvotes: 11

Jack Lin
Jack Lin

Reputation: 121

I had similar issue of socket close unexpectedly. The issue started while I upgraded node-redis from 3.x to 4.x. The issue was gone after I upgraded my redis-server from 5.x to 6.x.

Upvotes: 6

Related Questions