Using phpiredis with Laravel

Published by at 20th October 2017 9:55 pm

Laravel has support out of the box for using Redis. However, by default it uses a Redis client written in PHP, which will always be a little slower than one written in C. If you're making heavy use of Redis, it may be worth using the phpiredis extension to squeeze a little more performance out of it.

I'm using PHP 7.0 on Ubuntu Zesty and I installed the dependencies with the following command:

$ sudo apt-get install libhiredis-dev php-redis php7.0-dev

Then I installed phpiredis as follows:

1git clone https://github.com/nrk/phpiredis.git && \
2 cd phpiredis && \
3 phpize && \
4 ./configure --enable-phpiredis && \
5 make && \
6 sudo make install

Finally, I configured Redis to use phpiredis in the redis section of config/database.php for a Laravel app:

1 'redis' => [
2
3 'cluster' => false,
4
5 'default' => [
6 'host' => env('REDIS_HOST', 'localhost'),
7 'password' => env('REDIS_PASSWORD', null),
8 'port' => env('REDIS_PORT', 6379),
9 'database' => 0,
10 'options' => [
11 'connections' => [
12 'tcp' => 'Predis\Connection\PhpiredisStreamConnection', // PHP streams
13 'unix' => 'Predis\Connection\PhpiredisSocketConnection', // ext-socket
14 ],
15 ]
16 ],
17 ],

Now, I'm going to be honest - in a casual comparison I couldn't see much difference in terms of speed. I would probably only bother with setting this up on a site where high Redis performance was absolutely necessary. If you just want a quicker cache response it might make more sense to put Varnish in front of the site instead. However, in cases where Redis gets used heavily, it's probably worth doing.