PHP Redis timeout, read error on connection?
Asked Answered
K

5

9

"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."

Kollwitz answered 6/8, 2013 at 5:29 Comment(0)
P
14

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.

Pleuron answered 6/8, 2013 at 18:14 Comment(0)
C
3
$redis->connect(host, port, timeout1);

.....

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

In which timeout1 must be longer than timeout2.

Cephalonia answered 28/3, 2017 at 6:1 Comment(0)
D
2

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.

Dilatation answered 24/2, 2014 at 20:36 Comment(0)
M
1

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!

Metallic answered 27/4, 2014 at 9:42 Comment(0)
N
1

If you are using PHPRedis, In your PHP code add this line before using the redis long-time command:

ini_set('default_socket_timeout', -1);

and change timeout in the Redis configuration file (/etc/redis.conf):

timeout 0
Nonfeasance answered 1/5, 2023 at 20:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.