kidcapital
kidcapital

Reputation: 5174

Do I need to quit my node redis client instance using .quit()?

Looking at this code below (taken from the git page)

var redis  = require("redis"),
    client = redis.createClient(), multi;

// runs immediately
client.mset("incr thing", 100, "incr other thing", 1, redis.print);
multi = client.multi();

// drains multi queue and runs atomically
multi.exec(function (err, replies) {
    console.log(replies); // 101, 2
    client.quit(); // IS THIS OPTIONAL?
});

I want to know if client.quit() is optional, or if multi.exec() automatically performs a quit for me? I'm trying to debug a memory leak in my redis and I realized I am not using .quit() anywhere. Should I be?

Meaning, should my code look like this?

client = redis.createClient();
multi = clent.multi();
multi.exec( {something} );
client.quit();

Basically, where does client.quit go and do I even need it?

Upvotes: 2

Views: 8330

Answers (1)

malletjo
malletjo

Reputation: 1786

"MULTI commands are queued up until an EXEC is issued, and then all commands are run atomically by Redis."

This is a example from github:

// multi chain with an individual callback
client.multi()
    .scard("bigset")
    .smembers("bigset")
    .keys("*", function (err, replies) {
        client.mget(replies, redis.print);
    })
    .dbsize()
    .exec(function (err, replies) {
        console.log("MULTI got " + replies.length + " replies");
        replies.forEach(function (reply, index) {
            console.log("Reply " + index + ": " + reply.toString());
        });
    });

Your question: do i need to use client.quit() ? Yes, you need to because your redis connection will not be closed until you restart your redis server. You should use client.quit() when all your process have been completed. (In your last callback for example)

Upvotes: 7

Related Questions