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.

Setting for this tutorial

Starting from Magento 1.8.1.0, 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 on your CentOS server

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.

Default configuration of Redis

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:

{snippet tutorials/magento_redis_default.xml}

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.

Reconfiguring Redis to listen to a UNIX socket

To use a UNIX socket instead, open up the file /etc/redis.conf and locate the line mentioning unixsocket. Replace it with the following:

unixsocket /var/tmp/redis.sock
unixsocketperm 777

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.

Revised Magento configuration

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.

{snippet tutorials/magento_redis_socket.xml}

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.

Posted on January 5, 2014

About the author

Author Jisse Reitsma

Jisse Reitsma is the founder of Yireo, extension developer, developer trainer and 3x Magento Master. His passion is for technology and open source. And he loves talking as well.

Sponsor Yireo

Looking for a training in-house?

Let's get to it!

We schrijven niet te commerciële dingen, we richten ons op de technologie (waar we dol op zijn) en we komen regelmatig met innovatieve oplossingen. Via onze nieuwsbrief kun je op de hoogte blijven van al deze coolness. Inschrijven kost maar een paar seconden.

Do not miss out on what we say

This will be the most interesting spam you have ever read

We schrijven niet te commerciële dingen, we richten ons op de technologie (waar we dol op zijn) en we komen regelmatig met innovatieve oplossingen. Via onze nieuwsbrief kun je op de hoogte blijven van al deze coolness. Inschrijven kost maar een paar seconden.