Changing the maximum item size allowed by Memcache

 
Published on 2013-02-21 by John Collins. Socials: YouTube - X - Spotify - Amazon Music - Apple Podcast

Memcache will only accept items of 1MB or below in size for caching by default. If you are using a version of Memcache greater than 1.4.2 however, you can make use of the -I parameter when starting the daemon to override this default limit.

Perhaps the easiest way to do this is to update the init script that starts the Memcache daemon to include the new limit. In this tutorial, we will do just that on a CentOS system (as always, the steps will be a little different for other Linux variants).

Update the init script

On my system, the script used for starting/stopping/restarting Memcached is here:

nano /etc/init.d/memcached

Once open, add the following new variable set to the desired item size limit you want:

MAXITEMSIZE=5m

You can also use kilobytes:

MAXITEMSIZE=50k

Now modify the line that starts the daemon to pass the MAXITEMSIZE variable to the I parameter:

daemon --pidfile ${pidfile} memcached -d -p $PORT -u $USER -I $MAXITEMSIZE -m $CACHESIZE -c $MAXCONN -P ${pidfile} $OPTIONS

The final complete file should look something like this:

#! /bin/sh
#
# chkconfig: - 55 45
# description:  The memcached daemon is a network memory cache service.
# processname: memcached
# config: /etc/sysconfig/memcached
# pidfile: /var/run/memcached/memcached.pid
 
# Standard LSB functions
#. /lib/lsb/init-functions
 
# Source function library.
. /etc/init.d/functions
 
PORT=11211
USER=memcached
MAXCONN=1024
CACHESIZE=64
MAXITEMSIZE=5m
OPTIONS=""
 
if [ -f /etc/sysconfig/memcached ];then 
    . /etc/sysconfig/memcached
fi
 
# Check that networking is up.
. /etc/sysconfig/network
 
if [ "$NETWORKING" = "no" ]
then
    exit 0
fi
 
RETVAL=0
prog="memcached"
pidfile=${PIDFILE-/var/run/memcached/memcached.pid}
lockfile=${LOCKFILE-/var/lock/subsys/memcached}
 
start () {
    echo -n $"Starting $prog: "
    # Ensure that $pidfile directory has proper permissions and exists
    piddir=`dirname $pidfile`
    if [ ! -d $piddir ]; then
        mkdir $piddir
    fi
    if [ "`stat -c %U $piddir`" != "$USER" ]; then
        chown $USER $piddir
    fi
 
    daemon --pidfile ${pidfile} memcached -d -p $PORT -u $USER -I $MAXITEMSIZE -m $CACHESIZE -c $MAXCONN -P ${pidfile} $OPTIONS
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch ${lockfile}
}
stop () {
    echo -n $"Stopping $prog: "
    killproc -p ${pidfile} /usr/bin/memcached
    RETVAL=$?
    echo
    if [ $RETVAL -eq 0 ] ; then
        rm -f ${lockfile} ${pidfile}
    fi
}
 
restart () {
    stop
    start
}
 
 
# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status -p ${pidfile} memcached
    RETVAL=$?
    ;;
  restart|reload|force-reload)
    restart
    ;;
  condrestart|try-restart)
    [ -f ${lockfile} ] && restart || :
    ;;
  *)
    echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart|try-restart}"
    RETVAL=2
    ;;
esac
 
exit $RETVAL

Once you save the updated script, restart Memcached like so:

service memcached restart

Testing that it worked

The best thing to do is to try to cache an item larger than 1MB using your favourite Memcache client (assuming you have increased the limit). If you get an error back like this from Memcache, then you know it did not work:

MemcachePool::set(): Server 127.0.0.1 (tcp 11211, udp 0) failed with: SERVER_ERROR object too large for cache

Updated 2023 : note that the above post was originally published in 2013 and may be outdated, but is left here for archival purposes.