A typical Developer Blog
by Gordon Franke
Icon

How can i run multiple instances of memcached on one server?

Problem

I need more then one memcached server.

Solution

Virtual machine

I can build for each memcached server a virtal machine.

Problems

  • more memory (system+memcached)
  • one ip address for each instance
  • more work

Multiple instances via ports

For this it needs to patch the init.d and start-memcached scripts to run multiple instances.

After the patch it search for configuration files that match the following pattern /etc/memcached_*.conf. When it found no file it use the /etc/memcached.conf configuration file. So it breaks no backward compatibility.

When you want to create 2 memached instances copy the configuration file and change the port, memory …

cp /etc/memcached.conf /etc/memcached_myserver_1.conf
cp /etc/memcached.conf /etc/memcached_myserver_2.conf

Commands

start all servers

/etc/init.d/memcached start

start only a specific server

/etc/init.d/memcached start myserver_1

stop all servers

/etc/init.d/memcached stop

stop only a specific server

/etc/init.d/memcached stop myserver_1

The patches

/usr/share/memcached/scripts/start-memcached

26,30d25
> if (scalar(@ARGV) == 2) {
> 	$etcfile = shift(@ARGV);
> 	$pidfile = shift(@ARGV);
> }
>

/etc/init.d/memcached

16a17
> DAEMONNAME=memcached
18d18
< NAME=memcached
20d19
< PIDFILE=/var/run/$NAME.pid
26a26,63
> 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";
>
30c67
<       start-stop-daemon --start --quiet --exec $DAEMONBOOTSTRAP
---
>       start-stop-daemon --start --quiet --exec "$DAEMONBOOTSTRAP" -- /etc/${NAME}.conf $PIDFILE
50c87
<       start-stop-daemon --start --quiet --exec $DAEMONBOOTSTRAP
---
>       start-stop-daemon --start --quiet --exec "$DAEMONBOOTSTRAP" -- /etc/${NAME}.conf $PIDFILE
54c91
<       N=/etc/init.d/$NAME
---
>       N=/etc/init.d/$DAEMONNAME
59a97
> done;

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Author:

Category: Uncategorized, unix

Tagged: , , , , , ,

23 Responses

  1. Dave says:

    Could you post the full files or email me the 2 files? I’m having some problems when trying to run the patch

  2. FYI

    Make sure you check line 1 on /etc/init.d/memcached

    it should be :

    #! /bin/bash

    not

    #! /bin/sh

    else the code will not work.

  3. sahid says:

    Very nice,
    thanks

  4. Chris says:

    Welche Version von Memcached wurde hier gepatched? Ich arbeite gerade an 1.4.2. und es scheint nicht zusammen zu passen.

  5. John says:

    Unfortunately i can’t get that to work. How do you apply the patch files? I tried it by hand but there seems to be an error. all i get is a

    /etc/init.d/memcached: line 100: syntax error near unexpected token `;’

    that’s weird. I should have patched this automatically but this isn’t a diff-file is it?

  6. Mike says:

    Looks like the patch has a tiny syntax bug-

    > CONFIG_NUM=${#CONFIGS[@]};
    > for ((i=0; i NAME=${CONFIGS[${i}]};
    > PIDFILE=”/var/run/${NAME}.pid”;

    should be

    > CONFIG_NUM=${#CONFIGS[@]};
    > for ((i=0; i NAME=${CONFIGS[${i}]};
    > PIDFILE=”/var/run/${NAME}.pid”;
    > done

    Note the done

  7. have you apply this patch? i think the “done” is in line 59

  8. Andrew says:

    Hello,

    How is one supposed to use the code provided in the patch windows?

    Regards

    Andrew

  9. You can use the command patch or manually change the files.

  10. Andrew says:

    I had googled this page: http://linux.about.com/od/commands/l/blcmdl1_patch.htm

    I am new to using the patch command so bear with me! I created a file on my server called diff1 containing the code for /usr/share/memcached/scripts/start-memcached. I then created a backup of my start-memcached. When I run patch start-memcached.bak < diff1, I receive this error message:

    patching file start-memcached.bak
    patch: **** `<' expected at line 2 of patch

    Am I doing something stupid?

    Regards

    Andrew

  11. which version of memcache you use?

  12. I am currently using memcached 1.4.2-1ubuntu3 and these patches will not work for me at all. on the first patch I keep getting this error: patch: **** ` was expected, this was caused by there not being a space after those such as ‘> ‘. I fixed that and got the above errors.

    Any assistance?
    Joseph Crawford´s last [type] ..Repairing the Oil Pan on my 2006 Katana 600

  13. UndiFineD says:

    #full /etc/init.d/memcached for my ubuntu 10.04+

    #! /bin/bash
    ### BEGIN INIT INFO
    # Provides: memcached
    # Required-Start: $syslog
    # Required-Stop: $syslog
    # Should-Start: $local_fs
    # Should-Stop: $local_fs
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: memcached – Memory caching daemon
    # Description: memcached – Memory caching daemon
    ### END INIT INFO

    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    DAEMON=/usr/bin/memcached
    DAEMONBOOTSTRAP=/usr/share/memcached/scripts/start-memcached
    DAEMONNAME=memcached
    DESC=memcached
    PIDFILE=/var/run/$NAME.pid
    FILES=(/etc/memcached_*.conf);
    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 &2
    exit 1
    ;;
    esac

    exit 0

  14. Jon says:

    Here are the complete patched files for Ubuntu 8.04.
    http://gist.github.com/516746

  15. xSmurf says:

    Awesome tip, thank very much for the info!

  16. flo says:

    small Fix:
    use:
    FILES=(/etc/memcached*.conf);
    instead of:
    FILES=(/etc/memcached_*.conf);
    to also hit the standard-configfile

    and simply add the line:
    start-stop-daemon –stop –quiet –oknodo –pidfile $PIDFILE
    whenever a serverstop is invoked.

  17. flo says:

    forget the “start-stop-daemon –stop –quiet –oknodo –pidfile $PIDFILE” Line cause its already there ;)

  18. i have forked the gist and make some small improvements http://gist.github.com/612961.

    tested under:
    ubuntu and memcache 1.4.5
    debian lenny memcache 1.4.5

  19. Kaanon says:

    You guys are lifesavers, I didn’t want to have to write this myself!

    Works perfectly!!!!!!

  20. Aleka says:

    Thanks for your scripts, I have tested it in Debian lenny – memcached 1.4.5-1 and Works perfectly.

    But, It would be possible to run more than 2 instances of memcache?.
    If so, what should I change in the scripts?

  21. simple add another memcache config file. you are only limited on ram and ports ;) so i run 22 memcache instances on one server

  22. Aleka says:

    Thanks for your answer!

    In this regard. Can I do some questions?

    1. Do you know any tool that allows me to make monitoring multiple instances at once?

    2. Do you know any tool that allows me to make “Stress Testing” on a site configured with memcache, which only has access through a “logon”?

    Thanks in advance!!

  23. april14344 says:

    oh cool, this is great! just like what i needed! thanks so much!
    april14344´s last [type] ..Young Driver Insurance – The Best Strategy to Save You Money!

Leave a Reply

CommentLuv badge