php
database
ai_generated
true
RedisException: read error on connection to 127.0.0.1:6379
ID: php/redis-gone-away
86%Fix Rate
88%Confidence
65Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 83 | active | — | — | — |
Root Cause
Redis connection errors in PHP occur when the Redis server closes the connection due to timeout, memory pressure, max client limit, or network issues. Long-lived PHP processes (workers, daemons) are especially susceptible because they hold connections across requests.
genericWorkarounds
-
90% success Implement connection retry with reconnection logic
Use a Redis client wrapper that catches RedisException and automatically reconnects. For Predis: configure the connection with read_write_timeout = 0 and set tcp_keepalive = 60. For PhpRedis: use $redis->setOption(Redis::OPT_READ_TIMEOUT, -1) and implement retry logic.
-
85% success Use persistent connections with health checks in long-running workers
Use pconnect() instead of connect() to reuse connections across requests. For queue workers, implement a periodic PING check before operations: if ($redis->ping() !== true) { $redis->connect(...); }. Set appropriate tcp-keepalive in redis.conf.
Dead Ends
Common approaches that don't work:
-
Setting Redis timeout to 0 (infinite) to prevent disconnections
70% fail
Disabling timeout allows dead connections to accumulate, eventually hitting Redis maxclients limit. The server becomes unable to accept new connections, affecting all applications.
-
Creating a new Redis connection on every operation
65% fail
Establishing a new TCP connection for each Redis operation adds significant latency (1-3ms per connection). Under high load, this can exhaust ephemeral ports and create thousands of TIME_WAIT sockets.
Error Chain
Leads to:
Preceded by:
Frequently confused with: