Pconnect, the API for client Connection server in Phpredis.
The connection is not being closed on close or end of request until the PHP process ends.
This is the original phrase in the API description
So here's the question:
1. does PHP process ends refer to the end of PHP execution, or the end of FPM? In the latter case, it means that the Redis connection will not be freed even once PHP is executed, and the Redis connection is reused the next time it executes.
2. The connection will is not being closed on close is saying that if pconnect is used, even if the call Close () is displayed in the code, the connection is not closed?
With these two questions, let's take a look at what Pconnect has done.
preparatory work
Environment:
Nginx + FPM
php5.3
We configured the FPM to
pm.max1pm.start1pm.max1
In this way, our page requests are executed by a deterministic FPM process, which facilitates strace tracking.
The PHP code that corresponds to the page request:
$ip = " 10.136.30.144 "; $port = 7777 ; $redis = new Redis (); $redis ->pconnect ( $ip , $port , 1 ); $key = "test" ; $value = "This is test" ; $redis ->set ( $key , $value ); $d = $redis ->get ( $key ); Var_dump ( $d );
The function of the code is simple, connect Redis, set a value first, and then remove it.
Test Question 1
Ideas:
Using Strace to observe FPM system calls, if the lifetime of the connection is PHP execution, then each page call will have a connect system call to connect Redis, if the life cycle of the connection is the end of FPM, Then only the first page call will have connect system call, then because the connection is reused, without connect, directly send a command request.
Start a new FPM (process number 28082).
Perform
-p28082-s1024-o redis_1
A system call that records a page request. As shown in the following:
You can see that the process first established the socket connection (the file descriptor is 9). It then sends a series of commands to Reids and finally takes the result string of "this is Test". And there are no Redis commands or system calls related to closing the connection.
After the page request is over, we execute
-n-p28082
As you can see, the FPM process still maintains a reids connection to 10.136.30.144 with a file descriptor of 9 (which is consistent with the results of strace).
Perform
-p28082-s1024-o redis_2
Record the system call for the second page request and get the following result.
The difference from the first request is that the process of establishing a connection is omitted, and the Reids command is sent directly to get the result!
Then use Lsof-n-P 28082 to view the FPM open file descriptor with the same result as the previous file.
Note that the connection is indeed reused and is not new.
Perform a 6th page request (because we are in the process of preparing the configuration, and the FPM is already a new one), use lsof to view the file descriptor opened by the process.
We found that although there is still a descriptor of 9 Reids connection, but the two TCP connection of the temporary port is different, that is, the connection changed!
So far, we have come to the conclusion of question 1:
when Pconnect is used, the connection is reused, and the lifetime of the connection is the life cycle of the FPM process, not the execution of PHP at a time. .
Test Question 2
For comparison, let's take a look at the system call that connects Redis with Connect and calls Redis->close (). (Change the pconnect in the above code to connect and add Redis->close () at the end)
We see that, in addition to establishing a connection, the QUIT command was sent to Reids at the end of the program, and the attached file descriptor was closed.
Next, we look at how Redis->close () behaves after using Pconnect.
The code is adjusted to:
$ip="10.136.30.144";$port=7777;$redis=NewRedis ();$redis->pconnect ($ip,$port,1);$key="Test";$value="This is test";$redis->set ($key,$value);$d=$redis->get ($key); Var_dump ($d);$redis->close ();Try{$redis->get ($key);}Catch(Exception $e){Echo $e->getmessage ();}
We look directly at the system call that executes the page request for the 2nd time
The connection is not established and the command is sent directly to get the result. Indicates that the connection is reused. At the same time, no quit command is sent to Reids server, and there is no system call to close the connection.
Note, however, that the results of the page request are returned:
So far, we have come to the conclusion of question 2:
If you use Pconnect in your code, close will only be able to make the current PHP no longer redis requests, but cannot actually close the Redis long connection, and the connection is still reused in subsequent requests until the FPM process life cycle ends.
Conclusion
1. When using Pconnect, the connection is reused, and the lifetime of the connection is the life cycle of the FPM process, not the execution of PHP at a time.
2. If Pconnect is used in the code, close only works to make the current PHP no longer redis requests, but does not actually close the Redis long connection, and the connection will still be reused in subsequent requests until the FPM process life cycle ends.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Drill down into PHP redis pconnect