Problem with Redis client connection

1,256 views
Skip to first unread message

James Coglan

unread,
Sep 18, 2011, 4:41:14 PM9/18/11
to Node list
Hi all,

I'm trying to write an Express app with Redis for storage, but it keeps dropping the connection and failing to recover. Here's the code I'm using:

var Redis     = require('redis'),
    redisConf = JSON.parse(fs.readFileSync(__dirname + '/config/redis.json').toString()),
    redis     = null

var connect = function() {
  try {
    redis = Redis.createClient(redisConf.port, redisConf.host)
    redis.on('end', connect)
    redis.on('error', connect)
  } catch (e) {
    setTimeout(connect, 10)
  }
}

connect()

All the storage code uses this 'redis' reference to get the Redis client. I frequently have to restart my app because the connection dropped and my code didn't reconnect properly. Any advice?

--
James Coglan
http://jcoglan.com
+44 (0) 7771512510

James Coglan

unread,
Sep 18, 2011, 6:03:59 PM9/18/11
to Node list
On 18 September 2011 21:41, James Coglan <jco...@gmail.com> wrote:
I'm trying to write an Express app with Redis for storage, but it keeps dropping the connection and failing to recover.

To clarify, here's what I get after my app's been running a while:

TypeError: /home/jcoglan/www/sinewav.es/views/performance.ejs:2
    1| <h2>
 >> 2|   <a href="http://twitter.com/<%= author.screenName %>"><img src="<%= author.profileImage %>"></a>
    3|   <%= performance.title %>
    4|   <% if (performance.type === 'master') { %><span class="traffic">0</span><% } %>
    5| </h2>

Cannot read property 'screenName' of undefined
    at Object.anonymous (eval at <anonymous> (/home/jcoglan/www/sinewav.es/node_modules/ejs/lib/ejs.js:198:12))
    at Object.<anonymous> (/home/jcoglan/www/sinewav.es/node_modules/ejs/lib/ejs.js:200:15)
    at ServerResponse._render (/home/jcoglan/www/sinewav.es/node_modules/express/lib/view.js:422:21)
    at ServerResponse.render (/home/jcoglan/www/sinewav.es/node_modules/express/lib/view.js:315:17)
    at /home/jcoglan/www/sinewav.es/app.js:102:15
    at Command.callback (/home/jcoglan/www/sinewav.es/storage.js:31:34)
    at RedisClient.return_reply (/home/jcoglan/www/sinewav.es/node_modules/redis/index.js:425:29)
    at RedisReplyParser.<anonymous> (/home/jcoglan/www/sinewav.es/node_modules/redis/index.js:81:14)
    at RedisReplyParser.emit (events.js:64:17) 
at RedisReplyParser.add_multi_bulk_reply (/home/jcoglan/www/sinewav.es/node_modules/redis/lib/parser/javascript.js:311:14) 

This is because the Redis client starts yielding no data. Restarting my application always fixes the issue.

I've seen some examples that call redis.createClient() for every new web request, but this seems extremely wasteful. How do I get it to reconnect reliably?

Matthew Ranney

unread,
Sep 21, 2011, 2:03:57 AM9/21/11
to nod...@googlegroups.com
node_redis will reconnect automatically.  You don't need to add any explicit logic for this.  If you send a command while a reconnection is in progress, then this command will be queued.

James Coglan

unread,
Sep 25, 2011, 6:51:46 PM9/25/11
to nod...@googlegroups.com
On 21 September 2011 07:03, Matthew Ranney <matt....@gmail.com> wrote:
node_redis will reconnect automatically.  You don't need to add any explicit logic for this.  If you send a command while a reconnection is in progress, then this command will be queued.

That's not what I'm seeing in this application -- if I just do something like this:

    redis = require('redis').createClient()
    redis.select(1)

After a while it will start returning null for all queries. 

Lou-adrien Fabre

unread,
Sep 26, 2011, 4:39:52 AM9/26/11
to nodejs
You are using https://github.com/bnoguchi/redis-node right??
I have the same issue, after a while I get another exeption, but also
comming from

at RedisReplyParser.add_multi_bulk_reply
sinewav.es/node_modules/redis/lib/parser/javascript.js:311:14)

I think this is a issue from the lib, which do bad stuff on reconnect
(that is done automaticly by the way, you don't have to do anything
for that). But interesting thing is that everything was going OK
before, and then it started on day doing this. Maybe I couldn't see
the reason cause this bug is comming very unperioicaly. I you find a
solution I'd be interested in it! At this moment i think i'm going to
use some other drivers...

On 26 sep, 00:51, James Coglan <jcog...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages