Memcached

From wiki.mikejung.biz
Jump to: navigation, search

Liquidweb 728x90.jpg

Memcached Overview and Basic Information

  • Memcached is "Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load."
  • "Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering."

Memcached Installation

How to install and optimize memcached on CentOS

First you need to install memcached if it isn't already installed. On CentOS memcached is easy to install via yum.

yum install memcached

Once memcached is installed you can view the main configuration file under the /etc directory. -m allows you to chance the amount of memory that memcached will use. Typically 64MB or 128MB is a good start for smaller sites, for larger sites it's not uncommon to see values of 64GB. -p allows you to specify which port memcached will listen on. You can change this to whatever you want, just keep in mind that most applications and CMS like WordPress assume memcached listens on port 11211, so you will need to update any applications that rely on memcached if you change the port. -l is the listening address for memcached. If you are using a single memcached server for multiple web servers you will more than likely want to have memcached listen on a private IP. If you only have a single server than 127.0.0.1 is fine.

vim /etc/memcached.conf

##Default Memcached Settings##
-m 64
-p 11211
-u nobody
-l 127.0.0.1

Once you have finalized the memcached configuration file, go ahead and start up memcached and use chkconfig to make sure that memcached starts up on reboot. This should happen once you install memcached via yum, but it never hurts to double check that it's set to start up by default, otherwise your application may very well be broken if your server is ever rebooted.

service memcached start
chkconfig memcached on

Configure PHP to use Memcached to save sessions

Why would you want to use Memcached to store PHP sessions? Because this guy said so

Convinced yet? Open up the server's php.ini configuration file with your favorite text editor.

vim /usr/local/lib/php.ini

If you are using Vim, like a true boss, you already know you can press "/" and type in "session.save_handler", hit enter and then press "i" to change this value from files to memcached. If you aren't that cool then just find the line below, add a "#" in front of it and paste in the other stuff.

#session.save_handler = files

session.save_handler = memcached
session.save_path = "127.0.0.1:11211"

Save the file, then restart apache to apply the change.

How to install and enable memcache.so extension on cPanel

If you need to install the memcache PHP module you can do so via pecl, or via yum / apt-get.

pecl install memcache
echo "extension=memcache.so" >> /usr/local/lib/php.ini
service httpd restart

Run the command below to make sure that the memcache PHP module is loaded.

php -m | grep memcache

Configuring mod_pagespeed to use Memcached

Article found here

How to tell what memcache stats are

You can echo stats into netcat to get a quick idea of memcached usage.

echo stats | nc 127.0.0.1 11211


  • STAT curr_items will tell you how many items memcached currently has.
  • STAT evictions will tell you how many items have been removed from memcached to make way for new or more frequently used items. If this value is very high then you will want to consider allowing memcached to use more RAM. Small numbers are ok since often times memcached will remove items that are almost never accessed to make way for more frequently used items, in this case adding more RAM will not necessarily help performance, so keep track of this stat over time.
  • STAT get_misses will tell you how many times an item was not in memcache's cache. Often times this value will be high after a restart since it takes a while to warm up caches. If this value continues to rise after memcached has been active for a while you may want to look into why this is happening. It could be due to lack of RAM or lots of resources that are un cache-able for whatever reason.
  • STAT bytes_read This value is the amount of bytes memcached has read from the network.
  • STAT bytes_written This value is the amount of bytes that memcached has sent to the network.

How to tell how much memory memcached is using

Similar to the memcached stats command, this command gives you detailed slab information. memcached will display per slab stats as well as total active slabs and total malloced amounts for all the slabs. The values are in bytes.

echo stats slabs | nc 127.0.0.1 11211

At the bottom of the stats slabs command output you will find the total amount of active_slabs as well as the total amount of malloced usage for memcached. The total_malloced value is in bytes, so below you can see that I am using about 64MB of memory.

STAT active_slabs 39
STAT total_malloced 66377824

How to tell what version of Memcached is running

If you want to find out what version of memcached is running you can echo "version" and pipe that into netcat which will query memcached for the version.

echo version | nc 127.0.0.1 11211

This is what the output looks like when you query memcached for it's version

echo version | nc 127.0.0.1 11211
VERSION 1.4.20

Ubuntu 14.10 Memcached Configuration File and Log File Locations

memcached.conf default settings and location

This is the location of the main Memcached configuration file on Ubuntu. If you want to adjust settings like where the logs for memcached are stored, or the amount of memory that memcached is allowed to use, you will want to modify this file. The file shown below is what memcached uses by default.

vim /etc/memcached.conf

# memcached default config file

# 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

/etc/default/memcached default value

This memcached file controls whether or not memcached should be disabled. By default, at least on Ubuntu 14.10 memcached is enabled once you install it. It's possible that someone might mess around with this file and disable memcached for whatever reason, so if you cannot figure out why memcached isn't working, you may want to check /etc/default/memcached and make sure memcached is actually enabled.

/etc/default/memcached

# Set this to no to disable memcached.
ENABLE_MEMCACHED=yes

memcached initd location and contents

vim /etc/init.d/memcached

#! /bin/bash
### BEGIN INIT INFO
# Provides:            memcached
# Required-Start:      $remote_fs $syslog
# Required-Stop:       $remote_fs $syslog
# Should-Start:                $local_fs
# Should-Stop:         $local_fs
# Default-Start:       2 3 4 5
# Default-Stop:                0 1 6
# Short-Description:   Start memcached daemon
# Description:         Start up memcached, a high-performance memory caching daemon
### END INIT INFO

# Usage:
# cp /etc/memcached.conf /etc/memcached_server1.conf
# cp /etc/memcached.conf /etc/memcached_server2.conf
# start all instances:
# /etc/init.d/memcached start
# start one instance:
# /etc/init.d/memcached start server1
# stop all instances:
# /etc/init.d/memcached stop
# stop one instance:
# /etc/init.d/memcached stop server1
# There is no "status" command.

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


FILES=(/etc/memcached_*.conf)
# check for alternative config schema
if [ -r "${FILES[0]}" ]; then
  CONFIGS=()
  for FILE in "${FILES[@]}";
  do
    # remove prefix
    NAME=${FILE#/etc/}
    # remove suffix
    NAME=${NAME%.conf}

    # check optional second param
    if [ $# -ne 2 ];
    then
      # add to config array
      CONFIGS+=($NAME)
    elif [ "memcached_$2" == "$NAME" ];
    then
      # use only one memcached
      CONFIGS=($NAME)
      break;
    fi;
  done;

  if [ ${#CONFIGS[@]} == 0 ];
  then
    echo "Config not exist for: $2" >&2
    exit 1
  fi;
else
  CONFIGS=(memcached)
fi;

CONFIG_NUM=${#CONFIGS[@]}
for ((i=0; i < $CONFIG_NUM; i++)); do
  NAME=${CONFIGS[${i}]}
  PIDFILE="/var/run/${NAME}.pid"

case "$1" in
  start)
       echo -n "Starting $DESC: "
       if [ $ENABLE_MEMCACHED = yes ]; then
            start-stop-daemon --start --quiet --exec "$DAEMONBOOTSTRAP" -- /etc/${NAME}.conf $PIDFILE
            echo "$NAME."
       else
            echo "$NAME disabled in /etc/default/memcached."
       fi
       ;;
  stop)
       echo -n "Stopping $DESC: "
       start-stop-daemon --stop --quiet --oknodo --retry 5 --pidfile $PIDFILE --exec $DAEMON
       echo "$NAME."
       rm -f $PIDFILE
       ;;

  restart|force-reload)
       #
       #       If the "reload" option is implemented, move the "force-reload"
       #       option to the "reload" entry above. If not, "force-reload" is
       #       just the same as "restart".
       #
       echo -n "Restarting $DESC: "
       start-stop-daemon --stop --quiet --oknodo --retry 5 --pidfile $PIDFILE
       rm -f $PIDFILE
       if [ $ENABLE_MEMCACHED = yes ]; then
       		start-stop-daemon --start --quiet --exec "$DAEMONBOOTSTRAP" -- /etc/${NAME}.conf $PIDFILE
       		echo "$NAME."
       else
            echo "$NAME disabled in /etc/default/memcached."
       fi
       ;;
  status)
       status_of_proc -p $PIDFILE $DAEMON $NAME  && exit 0 || exit $?
       ;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
	exit 1
	;;
esac
done;

exit 0

Ubuntu 14.10 Memcached binary locations

This is where the memcached binary is located on Ubuntu. This is not a configuration file, but I am including it here just in case anyone is wondering where the memcached binary is located on Ubuntu 14.10.

/usr/bin/memcached

Ubuntu 14.10 Memcached log file locations

On Ubuntu 14.10 you can view the memcached logs in /var/log/memcached.log

/var/log/memcached.log

If you want to follow the memcached logs as they are written you can use the tail command to troubleshoot / analyze what memcached is doing

tail -f /var/log/memcached.log

Memcached Configuration Settings and Optimizations

If you notice that memcached is using too much memory, or unable to store enough information you can modify memcache's configuration file at the location shown below.

vim /etc/memcached.conf

These are the default settings (with comments removed). There are not a ton of "optimization" like tweaks for memcached, but the main way to improve performance is allow it to cache more things in RAM. I suggest giving memcached 64MB for memory for 1GB VPS servers, 128MB for 2GB VPS servers, and 256MB for 4GB VPS servers. If you are only running memcached on the vps then you can give it as much RAM as you want, just be sure to save about 20% - 30% of the VPS memory for the operating system and various other services otherwise you may cause the vps to swap and slow down.

Memcached optimized settings for a 1GB vps server

These settings assume you are running other services like Apache, MySQL and PHP-FPM. You should review those optimization wikis first and then allocate any left over memory to Memcached.

-d
logfile /var/log/memcached.log
-m 64
-p 11211
-u nobody
-l 127.0.0.1

Memcached optimized settings for a 2GB vps server

These 2GB server settings assume you are running other services like Apache, MySQL and PHP-FPM. You should review those optimization wikis first and then allocate any left over memory to Memcached.

-d
logfile /var/log/memcached.log
-m 128
-p 11211
-u nobody
-l 127.0.0.1

Memcached optimized settings for a 4GB vps server

These 4GB server settings assume you are running other services like Apache, MySQL and PHP-FPM. You should review those optimization wikis first and then allocate any left over memory to Memcached. 256MB of RAM should be enough for memcached to speed up your website, if you have lots of RAM to spare even after you tune other services then I would suggest raising -m higher if memcached fills up quickly.

-d
logfile /var/log/memcached.log
-m 256
-p 11211
-u nobody
-l 127.0.0.1

Memcached optimized config settings for a 8GB vps server

These 8GB VPS settings assume you are running other services like Apache, MySQL and PHP-FPM. You should review those optimization wikis first and then allocate any left over memory to Memcached.

-d
logfile /var/log/memcached.log
-m 512
-p 11211
-u nobody
-l 127.0.0.1