Yireo - Developer training

Open main menu

Most commonly Magento sessions are stored in the database or in the filesystem, but for increased performance, sessions can also be stored in memory using memcached. This tutorial covers how to do this while accessing memcached via a UNIX-socket.

Installing memcached on your server

To use memcached in Magento, you will first need to install it on your server. As root you can use your package manager to install memcached. With CentOS Linux this will look like this:

yum install memcached php-pecl-memcached

After installing the packages, the service needs to be started up. We also use chkconfig to start the service automatically after reboots:

service memcached start
chkconfig memcached on

Now the memcached service is ready for use.

Configuring Magento for storing sessions in memcache

Next, the Magento application needs to be told to save sessions in the memcached database. To do this, we open up the Magento file app/etc/local.xml and locate the line mentioning <session_save>. When sessions are currently save in the database, it might look like this:


Replace this line with the following lines:


Afterwards, flush the Magento cache. Now, sessions should be saved into the memcached database. If you want to, you can check out the memcached-tool to see statistics on how the memcached database is behaving:

memcached-tool display
memcached-tool stats

Reconfiguring memcached to run over a UNIX socket

Now, memcached by default is setup for networking and is listening to the TCP-port 11211. This is useful when you have multiple servers, where for instance a Magento webserver is connecting to a different server for the database. The memcached database can then be accessed remotely via the network. But if you have only a single server, running Magento, MySQL and everything, then using memcached through TCP/IP is actually a (very small) waste of resources. Instead of using TCP/IP, you can also use UNIX sockets to skip networking and still use memcached.

To do this, memcached needs to reconfigured to listen to a UNIX-socket instead. Open up the file /etc/sysconfig/memcached and locate the following line:


Replace it with this line:

OPTIONS="-s /var/tmp/memcache.socket -a 0777"

This instructs memcached to bind itself not to a TCP/IP socket but a file socket /var/tmp/memcache.socket instead. Because the memcached service is being ran as UNIX-user memcached, the socket will also be owned by this user. To allow other UNIX-users like the webserver-user to read and write to memcached, we set the permissions to 777. If your servers is setup as shared hosting server, you will definitely need to look into the security aspects of this.

Restart the memcached service, so the changes take effect:

service memcached restart

You can verify now whether memcached is being used by using the memcached-tool command as follows:

memcached-tool /var/tmp/memcache.socket display

Reconfiguring Magento to talk to memcached via the UNIX socket

Next, we go back to the Magento app/etc/local.xml to reconfigure it so that Magento will access memcached via UNIX-socket and not TCP/IP. Locate the previous lines again and replace them with the following:


Again flush the Magento cache. If all goes well, memcached should still be usable and now accessible through the UNIX-socket instead.

Update 2014-12-05

We removed the line on a session_cache_limiter. It can cause issues in some environments when combined with other HTTP headers. It is therefore best to remove it alltogether.

Written on February 16, 2014 by 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.

Looking for a training in-house?

Let's get to it!

Do not miss out on what we say

This will be the most interesting spam you've ever read

We don't write too commercial stuff, we focus on the technology (which we love) and we regularly come up with innovative solutions. Via our newsletter, you can keep yourself up to date on all of this coolness. Subscribing only takes seconds.

Sign up for the Yireo newsletter



GitHub organizations

Legal information

Other Yireo sites

Get Social

About Yireo