Redis: weird protocol/network errors

2019-05-31 07:55发布

问题:

I'm running Redis and connecting from Ruby using ezmobius's Redis gem[1].

Periodically (about once a day) I get a series of exceptions in my Rails app caused by Redis returning strange results.

They are often triggered by an exception such at this:

Redis::ProtocolError: Protocol error, got '3' as initial reply byte                         

or

Redis::ProtocolError: Protocol error, got '9' as initial reply byte                      

or sometimes

Errno::EAGAIN: Resource temporarily unavailable - Timeout reading from the socket

It usually requires a restart of my Rails servers to clear up the connection problem. I'm running Fedora Core 8, Rails 2.3.8, Redis gem 2.0.3. I've got the system_timer gem installed. Anybody got any ideas how I can stop these errors?

[1]Redis gem

回答1:

I've just noticed the same thing in my background workers, which store tasks in queues in Redis and also communicate through Redis pub/sub. Google results suggest that this can happen if you use the same Redis object from more than one thread... I'm not sure if this is the case in my app, I'll have to investigate this (but I do have threads there).



回答2:

I had a slightly similar issue with

Errno::EAGAIN: Resource temporarily unavailable - Timeout reading from the socket

Turns out, my redis-server had a timeout set to 300 seconds on connections. After 5 minutes, redis was killing the connection to my workers and they were logging the error above.

If your socket timeouts are happening every x seconds, its no doubt redis killing your 'idle' connection!



回答3:

When initializing the connection, make sure to pass the :thread_safe option:

Redis.connect(:thread_safe => true)