Best practices for Drupal sessions in memcached?

Events happening in the community are now at Drupal community events on www.drupal.org.
blender1968's picture

Hi All,

I'm using memcache module for cache tables.

Now I am interested in doing some testing of using memcache for sessions.

In production we are using RHEL and CentOS in testing so for setup of memcached I'm using the start-memcached and memcached init scripts offered here:

http://www.dev411.com/wiki/Memcached_startup_files_for_Red_Hat_(RHEL)

This uses a /etc/memcached.conf which looks something like this:

-u apache
-d
-m 32
-l 127.0.0.1
-p 11211

Based on info contained within this thread:

http://drupal.org/node/354420 (Cannot access modules page when using memcache-session.inc)

it states that "Note you MUST have a session and a users server set up for memcached sessions to work."

My question then is: Using RHEL start-memcached/memcached init scripts how does one go about creating a second memcached server on the same Linux server?

There is a very good article from lullabot (http://www.lullabot.com/articles/how_install_memcache_debian_etch) in Debian environment that just uses a bash script to start several memcached servers but I would like to stick with the RHEL init scripts if at all possible.

Anyone been down this path before?

Thanks!

Cheers

P.S.

It seems like the docs for the memcache module are dated? At least on the project page (http://drupal.org/project/memcache) they seem to be:

  • "Also note that the 3.x versions of the PECL package have not been tested with this module and are reported not to work."

3.0.3 seems to be working fine for me. At least I hope it is! Logged in as admin I see gets, sets, bins and hits so it looks good. Are others using memcache module with PECL memcache 3.0.x?

  • ""Drop-in" replacement libraries for session handling. One that uses memcache exclusively (session-memcache.inc) and one that uses memcache if available and fails over to the database if memcache becomes unavailable.(session-memcache-db.inc)"

The README.txt makes no mention of session-memcache-db.inc and this file does not exist in the memcache-6.x-1.2.tar.gz download. Was this removed for some reason?

Comments

Hacked RHEL init scripts

blender1968's picture

I have Drupal sessions in memcached.

I basically just added another set of RHEL init scripts. i.e.:

[root@XXXXX bin]# diff start-memcached start-memsessiond
17c17
< my $etcfile = shift || "/etc/memcached.conf";
---
> my $etcfile = shift || "/etc/memsessiond.conf";
25c25
< my $pidfile = "/var/run/memcached.pid";
---
> my $pidfile = "/var/run/memsessiond.pid";

[root@XXXXX etc]# diff memcached.conf memsessiond.conf
10c10
< -p 11211
---
> -p 11212

[root@XXXXX init.d]# diff memcached memsessiond
6c6
< # chkconfig: - 80 12
---
> # chkconfig: - 81 13
19,21c19,21
< DAEMONBOOTSTRAP=/usr/local/bin/start-memcached
< DAEMONCONF=/etc/memcached.conf
< NAME=memcached
---
> DAEMONBOOTSTRAP=/usr/local/bin/start-memsessiond
> DAEMONCONF=/etc/memsessiond.conf
> NAME=memsessiond

[root@XXXXX init.d]# ps -ef | grep mem
apache 21791 1 0 13:52 ? 00:00:00 /usr/local/bin/memcached -u root -u apache -m 32 -l 127.0.0.1 -p 11212

apache 30288 1 0 Jan30 ? 00:00:00 /usr/local/bin/memcached -u root -u apache -m 32 -l 127.0.0.1 -p 11211

Needs more testing before I'd consider deploying - but it works.

Cheers

P.S.

Would still like to hear about session-memcache-db.inc since if the memcached process providing bins for session and users dies no one (including admin) can login.

Falling back to DB sessions?

markus_petrux's picture

In case memcache is dead during bootstrap... maybe adding a fallback path to DB sessions? It would have to create some kind of alert so the site admin knows something is going wrong.

DB sessions

blender1968's picture

Yah, I assume that's what session-memcache-db.inc was supposed to do. Still interested in why this option seems to have been withdrawn from the memcache module... it's a dead file in CVS without much reasoning (AFAICT) why it was removed.

Definitely could run sessions in memcached, monitor process (check_tcp) and alert if down.

Cheers

Because it was not worth the

slantview's picture

Because it was not worth the added overhead. It was slowing down page loads considerably which is what you are trying to avoid if you are using memcache sessions.

For an all in one script

mwillis's picture

Just an alternative way of doing it. Benefit (as well as drawback) is starting and stopping all memcached instances at once and being able to throw a new *conf file in to run additional instances later.

Replace your default /etc/init.d/memcached script with:

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


# Check that networking is up.
. /etc/sysconfig/network

if [ "$NETWORKING" = "no" ]
then
        exit 0
fi

RETVAL=0
CONF=`ls /etc/sysconfig/memcached/*.conf 2>/dev/null`
PROG="memcached"
OPTIONS="";

start () {
    if [ "$CONF" ]; then
        for i in $CONF; do
            source $i
            echo -n $"Starting $PROG-$PORT ($CACHESIZE MB): "
            # insure that /var/run/memcached has proper permissions
            if [ "`stat -c %U /var/run/memcached`" != "$USER" ]; then
                chown $USER /var/run/memcached
            fi
            daemon --pidfile $PIDFILE memcached -d -p $PORT -u $USER -m $CACHESIZE -c $MAXCONN -P $PIDFILE $OPTIONS
            RETVAL=$?
            [ $RETVAL -eq 0 ] && success || failure
            echo
        done
    fi

    return $RETVAL

}
stop () {
    if [ "$CONF" ]; then
        for i in $CONF; do
            source $i
            if [ -f "$PIDFILE" ]; then
                echo -n $"Stopping $PROG-$PORT ($CACHESIZE MB): "
                killproc -p $PIDFILE /usr/bin/memcached
                rm -f $PIDFILE
                RETVAL=$?
                [ $RETVAL -eq 0 ] && success || failure
                echo
            fi
        done
     fi
     return $RETVAL
}

restart () {
        stop
        start
}


# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status memcached
        ;;
  restart|reload|force-reload)
        restart
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|reload|force-reload}"
        exit 1
esac

exit $?

Then create a directory called /etc/sysconfig/memcached/ and place however many conf files you want in there one per memcached instance with varying port numbers and pid files:

/etc/sysconfig/memcached/default_memcached.conf

PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="256"
OPTIONS=""
PIDFILE="/var/run/memcached/default_memcached.pid"

/etc/sysconfig/memcached/session_memcached.conf

PORT="11212"
USER="memcached"
MAXCONN="1024"
CACHESIZE="32"
OPTIONS=""
PIDFILE="/var/run/memcached/session_memcached.pid"

/etc/sysconfig/memcached/users_memcached.conf

PORT="11213"
USER="memcached"
MAXCONN="1024"
CACHESIZE="32"
OPTIONS=""
PIDFILE="/var/run/memcached/users_memcached.pid"

Re: For an all in one script

michee.lengronne's picture

Is it adapted to Debian ?
I think there is a problem with /etc/init.d/functions and the function 'source' is unknown.

My Profile

Blog articles on Medium

Live stream on Twitch

<

div>Videos on <a href="http

Which bins to memcache?

ex dj's picture

Right now, our production server memcaches everything. This results in a VERY slow (> 20min) cache clear time.

I haven't been able to find a definitive set of best practices for this. I would love to know, which cache bins do y'all include in memcache, and why?

Maybe clearing the cache is

rodrigoeg's picture

Maybe clearing the cache is not the issue. You might be having some kind of semaphore issue, or your cache takes a lot of time to rebuild.

What is the Drupal version you are using?

You might will have do a profiling with Xhprof (or other tool) to check what is the culprit that is taking a lot of time to execute.

Also, please check the README.txt of the module depending of your version.
https://git.drupalcode.org/project/memcache/-/blob/7.x-1.x/README.txt

As a recommendation, cache_form should be excluded from memcached because of the size and it is considered non-volatile. The semaphore and stampede protections configuration should also be analyzed.

High performance

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: