Laravel redis Error while reading line from the server., laravelredis
After the code runs for a period of time, the following error is reported.
[Predis \ Connection \ ConnectionException]
Error while reading line from the server. [tcp: // 127.0.0.1: 6379]
The above error occurs because the connection to the server fails due to the excessive number of connections. View the process and find a large number of tcp connections in the redis status of TIME_WAIT.
The first consideration is to reduce the TIME_WAIT process and keep it accessible to the server at any time. Therefore, we think of a process that reduces the TIME_WAIT status and quickly recycles the process. Modify the Kernel Parameter sysctl. conf, net. ipv4.tcp _ timestamps = 1 (1 is enabled), and enable fast recovery of net. ipv4.tcp _ tw_recycle = 1. Tw_recycle uses the timestamp to determine which process is the latest and recycles processes that are not the latest TIME_WAIT. Therefore, you need to enable tcp_timestamps first.
After modification, the system did not continue to report an error.
However, the TIME_WAIT process can be quickly recycled, which may cause packet loss. As a result, no response is received and a connection cannot be established successfully. However, this method is not the best solution. In particular, modifying the kernel parameters involves too many links and requires in-depth understanding before modification.
If you do not modify the underlying layer, you can start with the predis client. The source code finds the read_write_timeout parameter. You can set the timeout time so that no error will be reported when reading the stream data. The author of Predis suggested setting to disable timeout (modify timeout 0) in redis. conf, indicating that the connection to the client is not closed. I feel that this is resource-consuming and can increase the timeout time as appropriate.
So this is the solution for the time being. Set the timeout parameters of read_write_timeout =-1 and redis. conf.
There are actually many solutions, and we can continue to look for a better solution in the future.
We compared two common php connections to the redis client, phpredis and predis. The predis used in Laravel. connect to redis and close the connection when the request ends. Phpredis uses the pconnect connection, which depends on php-fpm. php-fpm is not closed, and the connection is always there. If you use pconnect again, the connection will be reused and will not be created again.
Later, I checked the predis source code and found that the persistent parameter was not mentioned in the manual description, but the literal meaning may be similar to the persistent connection in pconnect mode. I will study it later.