Go deep into PHP redis pconnect

Source: Internet
Author: User
Tags fpm pconnect php redis redis


Pconnect, the API for client Connection server in Phpredis.



The connection is not is closed on "close" or end of request until the PHP process ends.
This is the original sentence in the API description



So here's the question:
1. PHP process ends refers to the completion of a PHP execution, or the end of FPM. If this is the latter, it means that even once PHP is finished, the Redis connection will not be released and the Redis connection is reused the next time it executes.
2. The connection will is closed on close to say that if pconnect is used, the connection is not closed even if the call Close () is displayed in code.



With these two questions, let's do an experiment and take a closer look at what Pconnect has done. 


preparatory work



Environment:
Nginx + FPM
php5.3
We configure the FPM to





Pm.max_children = 1
pm.start_servers = 1
pm.max_spare_servers = 1


In this way, our page requests are executed by a determined FPM process to facilitate strace tracking.



The PHP code corresponding 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 very simple, connect Redis, set a value first, then remove. 


Test Issue 1



idea:
Using Strace to observe FPM system calls, if the connection life cycle is a PHP execution, then each page call, there will be connect system calls to connect the Redis; if the life cycle of the connection is the end of FPM, Then only the first page call will have the Connect system call, then because the connection is reused, no need to connect, Direct Send command request.



Start a new FPM (process number 28082).
Perform





Strace-p 28082-s 1024-o Redis_1


A system call that records a page request. As shown in the following illustration:

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 ends with a result string of "this is Test". The Redis command or system call associated with the connection is not closed.



After the page request is finished, we execute





Lsof-n-P 28082



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 strace result).



Perform





Strace-p 28082-s 1024-o redis_2


The system call that records the second page request gets the following result.

The difference from the first request is that it eliminates the process of establishing the connection, sends the Reids command directly, and gets the result.
Then use Lsof-n-P 28082 to view the file descriptor opened by FPM, with the same result as on the file.
Note that the connection is indeed reused and no new.



To perform a 6th page request (because we are in the process of preparing the configuration, at which point FPM is already a new one), use lsof to view the file descriptor opened by the process.

We found that although there was still a reids connection with a descriptor of 9, the temporary ports of two TCP connections were different, that is, the connection changed.



So far, we have come to the conclusion of question 1:

when using Pconnect, the connection is reused, and the life cycle of the connection is the lifecycle of the FPM process, not the execution of PHP at a time. .



  Test Issue 2

For comparison, let's take a look at the connection Redis using connect and call the Redis->close () system call. (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, at the end of the program, the QUIT command was sent to Reids and the attached file descriptor was closed.



Next, we look at how the Redis->close () behaves after using Pconnect
The code is adjusted to:





$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);
$redis->close ();

try{
    $redis->get ($key);
} 
catch (Exception $e) {
    echo $e->getmessage ();
}


We look directly at the 2nd time the system call that executes the page request
The connection is not established, and the same is true for sending commands directly to get results. Indicates that the connection is reused. Also, there is no quit command sent to Reids server and no system calls to close the connection.
Note, however, that the return result of the page request:

So far, we have come to the conclusion of question 2:
If you use Pconnect in your code, close only makes the current PHP no longer redis requests, but does not actually turn off the Redis long connection, which will still be reused in subsequent requests until the FPM process lifecycle is over. Conclusions



1. When using Pconnect, the connection is reused, and the life cycle of the connection is the lifecycle of the FPM process, not the execution of PHP at a time.
2. If you use Pconnect in your code, close acts only to make the current PHP no longer redis requests, but does not actually turn off the Redis long connection, which will still be reused in subsequent requests until the FPM process lifecycle is over.


Related Article

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.