Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redis crash on heroku with node.js

I have a very simple app running on heroku with nodejs and redis. It takes data posted to it periodically via an ajax post, and stores the data in a list in Redis.

I've run the app locally without issue, where it logs the data sent to it to redis without complaint. However, when I run it on heroku I get about 5-10 requests into it before it crashes, with a pretty non-specific redis error.

Dependencies:

"redis": "~0.7.1",
"hiredis": "~0.1.14",
"redis-url": "~0.1.0"

Code writing to redis (coffeescript):

app.post '/track', (req, res) -> 
  redis = require('redis-url').connect(app.settings.redis_url)

  if(req.body.userid)
    key = "locations:#{req.body.userid}"
    redis.rpush key, JSON.stringify({time: (new Date()).toString(), lat: req.body.latitude, lon: req.body.longitude})

The error I am getting is as follows:

Error: Uncaught, unspecified 'error' event.
2012-04-21T06:12:00+00:00 app[web.1]:     at Command.callback (/app/node_modules/redis/index.js:159:29)
2012-04-21T06:12:00+00:00 app[web.1]:     at HiredisReplyParser.<anonymous> (/app/node_modules/redis/index.js:256:14)
2012-04-21T06:12:00+00:00 app[web.1]:     at RedisClient.return_error (/app/node_modules/redis/index.js:446:25)
2012-04-21T06:12:00+00:00 app[web.1]:     at HiredisReplyParser.execute (/app/node_modules/redis/lib/parser/hiredis.js:41:18)
2012-04-21T06:12:00+00:00 app[web.1]:     at HiredisReplyParser.emit (events.js:67:17)
2012-04-21T06:12:00+00:00 app[web.1]:     at RedisClient.on_data (/app/node_modules/redis/index.js:422:27)
2012-04-21T06:12:00+00:00 app[web.1]:     at Socket.emit (events.js:67:17)
2012-04-21T06:12:00+00:00 app[web.1]:     at Socket.<anonymous> (/app/node_modules/redis/index.js:66:14)
2012-04-21T06:12:00+00:00 app[web.1]:     at TCP.onread (net.js:367:14)

This crashes the app, which heroku brings back eventually, but it then crashes again pretty quickly, within a few requests.

Anyone run into this before? I'm pretty new to node/redis, so this is probably something obvious. It's just odd that it runs happily pretty much forever locally, but is dying like this on heroku...

Thanks!

like image 273
stockli Avatar asked Oct 30 '25 04:10

stockli


1 Answers

Well, this was a clear case of RTFM and not sleeping on it before posting.

I saw on another SO post that I could attach an error handler to the redis client via:

redis.on "error", (err) ->
   console.log("Redis error: #{err}")

This yielded

Redis error: Auth error: Error: Error: ERR max number of clients reached

in the logs, which was happening because was opening a new connection on each request and not closing it. I then moved my connection instantiation to the server.js file and then passed it into my route handlers as a parameter. Now the app runs fine with just one active connection...

Hopefully this will help future people who have made a similar mistake...

like image 200
stockli Avatar answered Oct 31 '25 21:10

stockli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!