Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@izevaka
Created July 6, 2012 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save izevaka/3060634 to your computer and use it in GitHub Desktop.
Save izevaka/3060634 to your computer and use it in GitHub Desktop.
Connection pooling
//Problem: In node.js, one must pool redis db connections, otherwise they die
//Solution: Use generic-pool
pool = poolModule.Pool({
name: 'redis',
create: function (callback) {
var redisClient = redis.createClient();
callback(null, redisClient);
},
destroy : function (client) {
client.quit();
},
max: 10,
min: 0,
idleTimeoutMillis : 4000,
log : false
});
//Using the pool
pool.acquire(function (err, client){
client.hgetall("hash", function (err, data) {
pool.release(client);
//do something with data
});
});
//Problem 2: Hide connection pooling from app code
//Solution - use pool.pooled
var pooledRedis = {
hgetall: pool.pooled(function (client, hashName, cb) {
client.hgetall(hashName, cb);
}),
lrange: pool.pooled(function (client, key, start, length, cb) {
client.lrange(key, start, length, cb);
})
};
//Problem 3: Duplication, ugly
//Solution - some argument slicin'
var methods = ["hgetall", "lrange"];
methods.forEach(function (funcName) {
pooledRedis[funcName] = pool.pooled(function (client) {
var args = Array.prototype.slice.call(arguments, 1, arguments.length);
client[funcName].apply(client, args);
});
});
@sugendran
Copy link

Why are you pooling redis? the client has an auto reconnect option... also make sure you use the optional hiredis bindings

@izevaka
Copy link
Author

izevaka commented Jul 13, 2012

@sugendran You are correct, pooling was a red herring after all. The problem was connect-redis module(session persistence). It has its own client and I had to add an error handler to that so that redis connection loss doesn't bring the app down. Let's see if http://runheat.jit.su survives for a while.

@izevaka
Copy link
Author

izevaka commented Jul 13, 2012

Hmm alright, might need to fork connect-redis and add ability to inject a pool. Seems like the connection eventually (after 30 minutes) enters a bad state that it doesn't recover from - [Error: Redis connection to failed - read ETIMEDOUT].

@cyberwombat
Copy link

Do you have an update on this? I suffer from same issue

@tacomilkshake
Copy link

I too am having this issue; it's odd I seemed to have the issue using my own redis server on Joyent, but not using RedisToGo, which makes me wonder if there's something in the Redis configuration that can resolve it.

@waylonflinn
Copy link

I seem to be having this issue too. After about 20 minutes my entire app/server becomes unresponsive. After a slightly variable but usually large amount of time I get [Redis connection to failed - read ETIMEDOUT] and it starts working again.

Anyone have any more information about this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment