PHP Redis timeout, read error on connection?

34,049

Solution 1

The current solution I know of is to disable persistent connections for phpredis, as they have been reported as buggy since October 2011. If you’re using php-fpm or other threaded models, the library specifically disables persistent connections.

Reducing the frequency of this error might be possible by adjusting the php.ini default_socket_timeout value.

Additionally, read timeout configurations in phpredis are not universally supported. The feature (look for OPT_READ_TIMEOUT) was introduced in tag 2.2.3.

Solution 2

$redis->connect(host, port, timeout1);

.....

$redis->blpop($key, timeout2);

In which timeout1 must be longer than timeout2.

Solution 3

After a lot of study of articles and doing my own strace's of redis and php, it seemed the issue was easily fixed by this solution. The main issue in my use case was that redis server is not able to fork a process towards saving the in-memory writes to the on-disk db.

I have left all the timeout values in php.ini and redis.conf as they were without making the hacky changes suggested and then tried the above solution alone, and this issue 'read error on connection' that was unfixable using all the suggestions around changing timeout values across php and redis conf files went away.

I also saw some suggestions around increasing limit on file descriptors to 100000 etc. I am running my use case on a cloud server with file descriptor limit at 1024 and my use case runs even with that limit perfectly.

Solution 4

I added the code ini_set(‘default_socket_timeout’, -1) in my php program, but I found it didn't work immediately.

However after 3 minutes when I started to run the php program again, at last I found the reason: the redis connection is not persistent

So I set timeout=0 in my redis.conf, and the problem is solved!

Share:
34,049
Farzher
Author by

Farzher

i.write(code);

Updated on July 12, 2022

Comments

  • Farzher
    Farzher almost 2 years

    "PHP Fatal error: Uncaught exception 'RedisException' with message 'read error on connection'"

    The driver here is phpredis

    $redis->blpop('a', 0);
    

    This always times out after ~1 minute. My redis.conf says timeout 0 and $redis->getOption(Redis::OPT_READ_TIMEOUT) returns double(0)

    If I do this it has never timed out $redis->setOption(Redis::OPT_READ_TIMEOUT, -1);

    Why do I need -1? Redis documentation says timeout 0 in redis.conf should never time me out.

    "By default recent versions of Redis don't close the connection with the client if the client is idle for many seconds: the connection will remain open forever."