Nosql-why is the number of php clients in redis so large?

Source: Internet
Author: User
Tags pconnect
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 6379In 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:connectAndpconnectI currently useconnectBecausemysql_pconnectThe current number of requests is not very large, so there is no need to useconnectAnd 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 6379In 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:connectAndpconnectI currently useconnectBecausemysql_pconnectThe current number of requests is not very large, so there is no need to useconnectAnd 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 onlyconnectValid function,pconnectYou 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 -naThe 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 !!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.