The main method of optimizing Magento is through caching, and the more efficient caching is handled, the faster Magento becomes. Using Redis as caching backend is a popular way of handling caching entries as efficiently as possible. This guide covers how to configure Magento for Redis using sockets, instead of a TCP/IP port.
Starting from Magento 184.108.40.206, Redis-support is shipping in the Magento core, due to the fact that the Cm_RedisSession created by Colin Mollenhour is fully integrated into the Magento distro. The procedure of how to enable this extension when you are not using Magento 1.8, is described in the official Magento guide How to use Redis. We recommend you to read that guide first, because our guide more or less extends that guide. The guide tells you how to configure Magento with Redis by default.
Installing Redis itself on your server is not that hard. Under CentOS (or similar Linux distributions) you can use the following commands to install the Redis packages:
yum install redis
yum install php-pecl-redis
It is best to automatically start the Redis service after every reboot:
chkconfig redis on
And let's start the service:
service redis start
We'll come back to the configuration-file /etc/redis.conf later on.
After Redis has been configured within the Magento app/etc/local.xml (following the official Magento documentation) you should end with a caching section similar to this:
<cache> <backend>Cm_Cache_Backend_Redis</backend> <backend_options> <server>127.0.0.1</server> <port>6379</port> <database>0</database> <password></password> <force_standalone>0</force_standalone> <connect_retries>1</connect_retries> <automatic_cleaning_factor>0</automatic_cleaning_factor> <compress_data>1</compress_data> <compress_tags>1</compress_tags> <compress_threshold>20480</compress_threshold> <compression_lib>gzip</compression_lib> <persistent>1</persistent> </backend_options> </cache>
As you can see, Magento is connecting to the Redis server through the local IP-address with port 6379. But if both Redis as Magento are running on the same server, using a network connection for this is less efficient. It would be faster to use a UNIX socket instead.
To use a UNIX socket instead, open up the file /etc/redis.conf and locate the line mentioning unixsocket. Replace it with the following:
The location of the socket-file /var/tmp/redis.sock is a rather personal choice - it could be another location as well, but make sure it is not /tmp which would be insecure. Also note the permissions are set to 777 - this means that any user is able to read and write to the socket. We assumed this is not really bad because we' re dealing with a webserver anyway. If you wish to modify this to a more secure mode, make sure that the UNIX user that runs the webservice processes is able to read and write to the socket.
After modifying the Redis file, restart Redis itself:
service redis restart
If all goes well, Redis should be listening now to both through TCP/IP as through socket.
Next, open up the app/etc/local.xml file and replace the server-element with the location of your socket-file. Also, set the persistent-element to 0. Persistent connection are not allowed for UNIX sockets. Note that the port-element can be dropped entirely. But unfortunately, this throws a PHP Notice in the current code, so we'll set it to a dummy value of 1.
<cache> <backend>Cm_Cache_Backend_Redis</backend> <backend_options> <server>/var/tmp/redis.sock</server> <port>0</port> <database>0</database> <password></password> <force_standalone>0</force_standalone> <connect_retries>1</connect_retries> <automatic_cleaning_factor>0</automatic_cleaning_factor> <compress_data>1</compress_data> <compress_tags>1</compress_tags> <compress_threshold>20480</compress_threshold> <compression_lib>gzip</compression_lib> <persistent>0</persistent> </backend_options> </cache>
After flushing the cache, you should be using a socket now to connect to Redis. Things should be slightly faster - and that's ofcourse what tuning is all about.