Jump to: navigation, search

Memcached Overview and Basic Information[edit]

  • 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[edit]

How to install and optimize memcached on CentOS[edit]

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 is fine.

vim /etc/memcached.conf

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

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[edit]

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 = ""

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

How to install and enable extension on cPanel[edit]

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

pecl install memcache
echo "" >> /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[edit]

Article found here

How to tell what memcache stats are[edit]

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

echo stats | nc 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[edit]

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 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[edit]

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 11211

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

echo version | nc 11211
VERSION 1.4.20

Ubuntu 14.10 Memcached Configuration File and Log File Locations[edit]

memcached.conf default settings and location[edit]

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.

# 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.

# 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[edit]

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.


# Set this to no to disable memcached.

memcached initd location and contents[edit]

vim /etc/init.d/memcached

#! /bin/bash
# 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

# 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.


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

set -e

. /lib/lsb/init-functions

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

# check for alternative config schema
if [ -r "${FILES[0]}" ]; then
  for FILE in "${FILES[@]}";
    # remove prefix
    # remove suffix

    # check optional second param
    if [ $# -ne 2 ];
      # add to config array
    elif [ "memcached_$2" == "$NAME" ];
      # use only one memcached

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

for ((i=0; i < $CONFIG_NUM; i++)); do

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

       #       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."
            echo "$NAME disabled in /etc/default/memcached."
       status_of_proc -p $PIDFILE $DAEMON $NAME  && exit 0 || exit $?
	echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
	exit 1

exit 0

Ubuntu 14.10 Memcached binary locations[edit]

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.


Ubuntu 14.10 Memcached log file locations[edit]

On Ubuntu 14.10 you can view the memcached logs in /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[edit]

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[edit]

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.

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

Memcached optimized settings for a 2GB vps server[edit]

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.

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

Memcached optimized settings for a 4GB vps server[edit]

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.

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

Memcached optimized config settings for a 8GB vps server[edit]

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.

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