I used redis as the cache system on the website and used its pecl client (this is github. comnicolasffphpredis ). There is no problem in use, but I found a problem when I looked at the number of links recently, that is, the number of backend links is very high. net...
I used redis as a cache system on my website and used its pecl client (that's the https://github.com/nicolasff/phpredis ). There is no problem in use, but I found a problem when I looked at the number of links recently, that is, the number of backend connections is very high.
We can see thatnetstat -na | grep 6379
In fact, there are still several screens for the port usage, so I will not post them. I want to ask, is this a client bug or is it like this? If it is like this, will there be any problems if it is occupied, or does it have its own upper limit? Because I have not seen its configuration or the initialization parameters have settings such as the connection pool, it is certain that this should not be the hold state of the connection pool.
I can see that its connection methods include:connect
Andpconnect
I currently useconnect
Becausemysql_pconnect
The current number of requests is not very large, so there is no need to useconnect
And it should release the connection after the script ends. The current situation is that these connections will only be automatically disconnected on the redis server after the timeout time is 30 seconds.
I am worried that if the number of requests is large in the future, the attack will be sustained in 30 seconds.
Reply content:
I used redis as a cache system on my website and used its pecl client (that's the https://github.com/nicolasff/phpredis ). There is no problem in use, but I found a problem when I looked at the number of links recently, that is, the number of backend connections is very high.
We can see thatnetstat -na | grep 6379
In fact, there are still several screens for the port usage, so I will not post them. I want to ask, is this a client bug or is it like this? If it is like this, will there be any problems if it is occupied, or does it have its own upper limit? Because I have not seen its configuration or the initialization parameters have settings such as the connection pool, it is certain that this should not be the hold state of the connection pool.
I can see that its connection methods include:connect
Andpconnect
I currently useconnect
Becausemysql_pconnect
The current number of requests is not very large, so there is no need to useconnect
And it should release the connection after the script ends. The current situation is that these connections will only be automatically disconnected on the redis server after the timeout time is 30 seconds.
I am worried that if the number of requests is large in the future, the attack will be sustained in 30 seconds.
I also use this plug-in. After my tests, this is definitely a pitfall design for this redis client! Because it has to close the connection by itself, closing the connection is almost a forgotten operation for php developers.
Generally, extended developers close their own connections at the end of the script, but this plug-in does not exist. Instead, you need to close it yourself. Of course, close the connection to onlyconnect
Valid function,pconnect
You do not need to close the connection. The specific method is to call the close method of the redis object before the script ends to close the connection. Or it is easier to use code similar to the following.
register_shutdown_function(function () { global $redis; $redis->close();});
After testing, you can usenetstat -na
The number of connections to port 6379 is very small.
You are using short connections. All connections with so many timewait instances are normal. Try pconnect and it will be better. This is normal for transient connections.
How can I answer you? Please answer a few questions first
First, the default time of Redis is set to 300 seconds in redis. conf. Have you modified this parameter?
Second, you use this redis extension. This usage$redis->connect('127.0.0.1', 6379);
This means that the time-out period is not set and will not time out. For more information, see the extension article.
Based on your estimation of a small amount of information, it is most likely that you have not set the connection time in the second article, using the default non-Timeout method. However, you are talking about disconnecting after 30 seconds, and it is not the default configuration that complies with redis. conf. Unless you modify the default configuration.
Pconnect is required. This is a connection pool.
I also encountered the above problem. I used pconnect connections, but there were a large number of connections in the test phase. sometimes it even causes the php process to crash. is pconnect or short connection + closed? Is there a reasonable suggestion? Worship again !!!