As the title, I'm looking for a php Redis client that support persistent connection, because my web application receives a lot of requests(each request, it'll put an item in to Redis queue) and I want to avoid create new connection every request.
5 Answers
Not sure if this is supported but you should definitely look at Predis and Rediska, this two (especially Predis AFAIK) are the best PHP Redis clients available.

- 18,314
- 5
- 50
- 44
-
thank you antirez, I've never tried Predis before, it seem support persistent connection, I believe that a C implementation as php-module should be faster, but i'll try them and take a comparison. – secmask Sep 02 '10 at 14:20
-
The concern is whether Predis supports persistent connections since it's a purely PHP implementation and not a C-based extension. According to the maintainer, Predis does support persistent connections if your PHP processes are configured to stay resident. This is typical on a serious production PHP + Apache setup, though your particular install may vary and may require some configuration. The [Predis FAQ](https://github.com/nrk/predis/blob/master/FAQ.markdown) points out that connections are persistent when Predis is persistent and PHP processes are recycled for requests. – kevinlawler May 11 '11 at 19:15
-
Disclaimer: I haven't yet tested Predis. I am trying the C extension first since Predis looks to be built in using a very verbose style. I also do not like relying on PHP's autoload mechanisms. – kevinlawler May 11 '11 at 19:22
-
1phpredis is allegedly quite a lot faster than predis – Prof. Falken May 17 '13 at 15:02
PhpRedis currently supports persistent connections. Using PHP 7.0 and PhpRedis 3.0, making a persistent connection with pconnect()
like this:
for ($i=0;$i<1000;$i++) {
$redis = new Redis();
$result = $redis->pconnect('127.0.0.1');
$redis->set("iterator",$i);
$response=$redis->get("iterator");
$redis->close();
unset($redis);
}
is about 10 times faster (9.6 msec vs 0.83 msec per connection) than connect()
:
for ($i=0;$i<1000;$i++) {
$redis = new Redis();
$result = $redis->connect('127.0.0.1');
$redis->set("iterator",$i);
$response=$redis->get("iterator");
$redis->close();
unset($redis);
}
Note: "This feature is not available in threaded versions". (I'm running under IIS on Windows, so I run the NTS version.)

- 4,823
- 1
- 38
- 34
Predis supports persistent connection. you just need to add persistent paramater as 1.
you can use the code below
$client = new Predis\Client(array(
'scheme' => 'tcp',
'host' => '127.0.0.1',
'port' => 6379,
'database' => 15,
'persistent'=> 1
));
instead of
$client = new Predis\Client('tcp://127.0.0.1:6379?database=15');
you can find more parameters for the connection here : https://github.com/nrk/predis/wiki/Connection-Parameters

- 115
- 1
- 11
Predis supports persistent connections using it's PhpiredisStreamConnection
with the persistent=1
flag syntax since v0.8.0:
<?php
$client = new Predis\Client('tcp://127.0.0.1?persistent=1', array(
'connections' => array(
'tcp' => 'Predis\Connection\PhpiredisStreamConnection',
'unix' => 'Predis\Connection\PhpiredisStreamConnection',
),
);

- 675
- 1
- 10
- 8
-
I added persistant=1 with PhpiredisStreamConnection and looks like I still get the same TIME_WAIT socket after all. – Nico AD Jun 11 '14 at 14:31
PHP-Redis supports persistent connections since it uses a php extension written in C which gives it a mechanism for sharing connections between requests. Look at the documentation on popen and pconnect.
Predis cannot support persistent connections because it is 100% PHP and PHP shares nothing between each request.

- 634
- 1
- 6
- 12
-
1According to the Predis author, this answer is incorrect. He claims that PHP requests may share information when the PHP processes are configured to stay resident between requests, as is the case with many implementations. – kevinlawler May 11 '11 at 19:26
-
1Agreed, this answer is wrong. `popen()` is the wrong type of resource anyway (p == piped process). `pconnect()` is fully capable of supporting Zend's internal "xport" resource handling which allows persistent connections to be maintained. Predis itself uses [stream_socket_client()](http://php.net/stream_socket_client), with an optional `STREAM_CLIENT_PERSISTENT` flag. When the PHP process is maintained by something like Apache with a pool of backend processes/threads, those persistent connections survive within the lifetime of the backend. – Joe Nov 21 '11 at 23:13