Memcached is a high speed caching system for storing objects in RAM. It works well with WordPress and WooCommerce as an object cache for storing transients so they are not stored in the MySQL database. On WooCommerce particularly object caching can help considerably because of the number of transients used by plugins and orders.
Unix sockets operate at a lower level OSI model layer than TCP sockets so they should be faster.
When I ran some basic benchmarks I found the results quite surprising – unix sockets were 33% faster than TCP sockets for memcached.
Granted we are talking about microsecond benefits, but why not use them if we can get some extra performance boost from enabling memcached unix sockets?
I will assume you already have Memcached installed and working on Debian or Ubuntu, here is the PHP5 Memcached installation guide (PHP7 coming soon).
Configure Memcached with Unix Socket
Modify your memcache user – even if you use memcached the user created is called memcache on Debian and Ubuntu.
We are going to make the memcache user a member of the www-data group which Apache, nginx, php5-fpm and php7.0-fpm run as by default on Debian and Ubuntu systems.
sudo usermod -g www-data memcache
Open your Memcached configuration
sudo nano /etc/memcached.conf
Comment out the port and listen lines to disable TCP by adding # to the beginning of the line.
Add the lines for specifying the Memcached socket path and permissions.
The unix socket will have
775 permissions so both the user (memcache) and group (www-data) can execute it.
#daemonize memcached -d # Log memcached's output to /var/log/memcached logfile /var/log/memcached.log # Be verbose # -v # Be even more verbose (print client commands as well) -vv # Start with a cap of 64 megs of memory. It's reasonable, and the daemon default # Note that the daemon will grow to this size, but does not start out holding this much # memory -m 50 # Default connection port is 11211 #-p 11211 # Run the daemon as root. The start-memcached will default to running as root if no # -u command is present in this config file -u memcache # Specify which IP address to listen on. The default is to listen on all IP addresses # This parameter is one of the only security measures that memcached has, so make sure # it's listening on a firewalled interface. #-l 127.0.0.1 # Set unix socket which we put in the folder /var/run/memcached and made memcache user the owner -s /tmp/memcached.sock # set permissions for the memcached socket so memcache user and www-data group can execute -a 775 # Limit the number of simultaneous incoming connections. The daemon default is 1024 # -c 1024 # Lock down all paged memory. Consult with the README and homepage before you do this # -k # Enable SASL #-S # Enable performance tweaks if using memcached 1.4.31 #-o modern
Ctrl+X, Y and Enter to Save and Exit.
Now restart Memcached
sudo service memcached restart
Now check if the Memcached unix socket is there
ls -lh /tmp
You should see the
memcached.sock file there
total 0 srwxr-xr-x 1 memcache www-data 0 Sep 10 17:00 memcached.sock
Now you can configure your service to use Memcached unix sockets instead of TCP.
Configure Memcached WordPress Plugin to Use Unix Sockets
I recommend using my forked Memcached is your friend WordPress plugin that works on both PHP7 and PHP5, you can specify the memcached server with a unix socket at the top of your wp-config.php.
Notice we have set the port to 0.
<?php $memcached_servers = array( 'default' => array( '/tmp/memcached.sock:0') ); /** * The base configuration for WordPress *
Under Tools > Memcached you can see the unix socket is in use.