«

»

Mar 12

How to: Configure memcache to use a unix socket

How to: Configure memcache to use a unix socket

I have a server which is going to have its own memcached instance installed on itself… so I was thinking using a unix socket instead of tcp should improve the overall performace at least a little. I had tried doing the same with MySQL and I was pleased on the improved speed if only a few ms (it adds up I think). Well, with no further due here it is:

First, you need to edit the main configuration file: # nano /etc/memcached.conf

# memcached default config file
# 2003 – Jay Bonci <[email protected]>
# This configuration file is read by the start-memcached script provided as
# part of the Debian GNU/Linux distribution.

# Run memcached as a daemon. This command is implied, and is not needed for the
# daemon to run. See the README.Debian that comes with this package for more
# information.
-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 64

# 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

# 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

# Return error when memory is exhausted (rather than removing items)
# -M

# Maximize core file limit
# -r

The main aspects we are going to look is the port, listening port, socket, and octal file mode:

  • Comment out the default port like so:

# -p 11211

  • Comment out the IP address option:

# -l 127.0.0.1

  • Add the following line to indicate you want to use a socket:

-s /var/run/memcached.sock

  • Optionally you could configure the octal file mode of the socket using the -a paramter:

-a 0766

Note that the two TPC options -l (listening ip) and -p (port) were commented out and we introduced socket specific options -s (socket) and -a (socket access file mode).

Furthermost note that we used the /var/run folder to avoid worrying about creating a special directory for memcache. It is though a good idea to establish a dedicated space to separate all memcache sockets and use a name that indicates the instance on the configuration and socket files. This though requires a little more work. After deciding on your nomenclature and directory we are going to configure the init.d script to create said folder if it is not available. Edit the memcached init.d script and find the following lines were you will insert the bold code:

nano /etc/init.d/memcached

[…]

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/memcached
DAEMONNAME=memcached
DAEMONBOOTSTRAP=/usr/share/memcached/scripts/start-memcached
DESC=memcached

test -x $DAEMON || exit 0
test -x $DAEMONBOOTSTRAP || exit 0

set -e

. /lib/lsb/init-functions

# Edit /etc/default/memcached to change this.
ENABLE_MEMCACHED=no
test -r /etc/default/memcached && . /etc/default/memcached

 

# Make /var/run/memcached directory

if [ ! -d “/var/run/memcached” ]; then

mkdir /var/run/memcached

chown memcache /var/run/memcached

fi

FILES=(/etc/memcached_*.conf)
# check for alternative config schema
if [ -r “${FILES[0]}” ]; then
CONFIGS=()

[…]

That should take care of it.

Enhanced by Zemanta

Leave a Reply

%d bloggers like this: