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.
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.
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 127.0.0.1:11211 display
memcached-tool 127.0.0.1:11211 stats
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
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.
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.