Setting up Multiple Redis Instances on a Single Magento Server

by Kirk Madera

Engineers reusable modular solutions to maintain environments and deployment strategies across teams. Works for food.

I had a bit of trouble figuring out a scalable way to set up multiple Redis instances on a single Magento server. There are a few posts (123) that were helpful, but included too much manual setup for my purposes. They all also required starting/stopping all redis instances individually.

I have set this up for both RedHat 6.5 and Ubuntu 13.x. This setup shares a Redis default config among all servers. Each new server only requires that a server specific configuration file be created with only four required settings. All redis instances may be started at once with

  1. service redis-server start

or each server may be started individually with

  1. service redis-server start server1

. This works will all service commands.

Install Redis and PHP Redis Client

RedHat

  1. yum install -y php54-pecl-redis redis;
  2. pecl install redis;
  3. echo'extension=redis.so'>/etc/php5/conf.d/redis.ini;
  4. service php5-fpm restart;#If you are running PHP-FPM

Ubuntu

  1. apt-get update;
  2. apt-get install -y php-pear php5-dev make redis-server;
  3. pecl install redis;
  4. echo'extension=redis.so'>/etc/php5/conf.d/redis.ini;
  5. service php5-fpm restart;#If you are running PHP-FPM


Set Up Your Init Script


  1. cd /etc/init.d;
  2. mv redis-server redis-server.bak;# Ubuntu only
  3. touch redis-server;
  4. chmod0755 redis-server;
  5. nano redis-server;

Paste these contents:
Redhat

  1. #!/bin/sh
  2. #
  3. # redis        init file for starting up the redis daemon
  4. #
  5. # chkconfig:   - 20 80
  6. # description: Starts and stops the redis daemon.
  7. if[-n "$2"]
  8. then
  9. NAME=$2
  10. # Source function library.
  11. ./etc/rc.d/init.d/functions
  12. exec="/usr/sbin/redis-server"
  13. pidfile="/var/run/redis/$NAME.pid"
  14. REDIS_CONFIG="/etc/redis/servers/$NAME.conf"
  15. [-e /etc/sysconfig/redis ]&&./etc/sysconfig/redis
  16. lockfile=/var/lock/subsys/redis
  17. start(){
  18.     [-f $REDIS_CONFIG]||exit6
  19.     [-x $exec]||exit5
  20.     echo-n $"Starting $NAME: "
  21.     daemon --user ${REDIS_USER-redis}"$exec$REDIS_CONFIG"
  22.     retval=$?
  23.     echo
  24.     [$retval-eq 0]&&touch$lockfile
  25.     return$retval
  26. }
  27. stop(){
  28.     echo-n $"Stopping $NAME: "
  29.     killproc -p $pidfile$NAME
  30.     retval=$?
  31.     echo
  32.     [$retval-eq 0]&& rm -f $lockfile
  33.     return$retval
  34. }
  35. restart(){
  36.     stop
  37.     start
  38. }
  39. reload(){
  40.     false
  41. }
  42. rh_status(){
  43.     status -p $pidfile$NAME
  44. }
  45. rh_status_q(){
  46.     rh_status >/dev/null2>&1
  47. }
  48.  
  49. case"$1" in
  50.     start)
  51.         rh_status_q &&exit0
  52.         $1
  53.         ;;
  54.     stop)
  55.         rh_status_q ||exit0
  56.         $1
  57.         ;;
  58.     restart)
  59.         $1
  60.         ;;
  61.     reload)
  62.         rh_status_q ||exit7
  63.         $1
  64.         ;;
  65.     force-reload)
  66.         force_reload
  67.         ;;
  68.     status)
  69.         rh_status
  70.         ;;
  71.     condrestart|try-restart)
  72.         rh_status_q ||exit0
  73.         restart
  74.         ;;
  75.     *)
  76.         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart}"
  77.         exit2
  78. esac
  79. exit $?
  80. else
  81.  
  82. FILES=/etc/redis/servers/*
  83. for f in $FILES
  84. do
  85. SERVERNAME=$(sed 's|/etc/redis/servers/||g' <<< $f)
  86. SERVERNAME=$(sed 's|.conf||g' <<< $SERVERNAME)
  87. /etc/init.d/redis-server "$1" "$SERVERNAME"
  88. done
  89.  
  90. fi
  91.  
  92. exit 0

  1. #!/bin/bash
  2. ### BEGIN INIT INFO
  3. # Provides:             redis-server
  4. # Required-Start:       $syslog $remote_fs
  5. # Required-Stop:        $syslog $remote_fs
  6. # Should-Start:         $local_fs
  7. # Should-Stop:          $local_fs
  8. # Default-Start:        2 3 4 5
  9. # Default-Stop:         0 1 6
  10. # Short-Description:    redis-server - Persistent key-value db
  11. # Description:          redis-server - Persistent key-value db
  12. ### END INIT INFO
  13.  
  14. if[-n "$2"]
  15. then
  16. NAME=$2
  17.  
  18. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  19. DAEMON=/usr/bin/redis-server
  20. DAEMON_ARGS=/etc/redis/servers/$NAME.conf
  21. DESC=redis-server
  22.  
  23. RUNDIR=/var/run/redis
  24. PIDFILE=$RUNDIR/$NAME.pid
  25.  
  26. test -x $DAEMON||exit0
  27.  
  28. set -e
  29.  
  30. case"$1" in
  31.   start)
  32.         echo-n "Starting $DESC: "
  33.         mkdir-p $RUNDIR
  34.         touch$PIDFILE
  35.         chown redis:redis $RUNDIR$PIDFILE
  36.         chmod755$RUNDIR
  37.         if start-stop-daemon --start --quiet --umask007--pidfile $PIDFILE--chuid redis:redis --exec$DAEMON--$DAEMON_ARGS
  38.         then
  39.                 echo"$NAME."
  40.         else
  41.                 echo"failed"
  42.         fi
  43.         ;;
  44.   stop)
  45.         echo-n "Stopping $DESC: "
  46.         if start-stop-daemon --stop --retry forever/QUIT/1--quiet --oknodo --pidfile $PIDFILE--exec$DAEMON
  47.         then
  48.                 echo"$NAME."
  49.         else
  50.                 echo"failed"
  51.         fi
  52.         rm -f $PIDFILE
  53.         ;;
  54.  
  55.   restart|force-reload)
  56.         ${0} stop $2
  57.         ${0} start $2
  58.         ;;
  59.  
  60.   status)
  61.         echo-n "$DESC is "
  62.         if start-stop-daemon --stop --quiet --signal 0--name ${NAME}--pidfile ${PIDFILE}
  63.         then
  64.                 echo"running"
  65.         else
  66.                 echo"not running"
  67.                 exit1
  68.         fi
  69.         ;;
  70.  
  71.   *)
  72.         echo"Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}">&2
  73.         exit1
  74.         ;;
  75. esac
  76.  
  77. else
  78.  
  79. FILES=/etc/redis/servers/*
  80. for f in $FILES
  81. do
  82. SERVERNAME=$(sed 's|/etc/redis/servers/||g' <<< $f)
  83. SERVERNAME=$(sed 's|.conf||g' <<< $SERVERNAME)
  84. /etc/init.d/redis-server "$1" "$SERVERNAME"
  85. done
  86.  
  87. fi
  88.  
  89. exit 0


Set Up Your Config

Default config file

  1. cd /etc/redis;
  2. mkdir servers;
  3. mv redis.conf servers/server1.conf;
  4. cd servers;
  5. nano server1.conf;

Each server file will look like this:

  1. # Redis server config
  2. include/etc/redis/redis.conf
  3.  
  4. # When running daemonized, Redis writes a pid file in /var/run/redis.pid by
  5. # default. You can specify a custom pid file location here.
  6. pidfile /var/run/redis/exampleproject-data.pid
  7.  
  8. # Accept connections on the specified port, default is 6379.
  9. # If port 0 is specified Redis will not listen on a TCP socket.
  10. port 6379
  11.  
  12. # Specify the log file name. Also 'stdout' can be used to force
  13. # Redis to log on the standard output. Note that if you use standard
  14. # output for logging but daemonize, logs will be sent to /prod/null
  15. logfile /var/log/redis/exampleproject-data.log
  16.  
  17. # The filename where to dump the DB
  18. dbfilename dump-exampleproject-data.rdb

Restart Redis instances:

  1. service redis-server restart;

Additional Servers

To create new servers, simply copy the server1.conf file to server2.conf and update all relevant strings to your new server name (in this case, server2). If your server name is unique enough, you can just run the file through sed. You'll also need to update the port manually to a new port.

  1. cd /etc/redis/servers;
  2. cp server1.conf server2.conf;
  3. sed -i s/server1/server2/g server2.conf;
  4. grep -hR ^port .|sort;# Determine the highest used port number
  5. nano server2.conf;#Update the port number

Start the new Redis instance(s):

  1. service redis-server start;

Magento Specific Adjustments

Run this command as root, per Fabrizio Branca's Redis Blog Post:

  1. echo"vm.overcommit_memory = 1">/etc/sysctl.conf; sysctl vm.overcommit_memory=1;

Add these settings to your Redis server configuration files if using Redis for Magento.

For Data Cache

  1. maxmemory 1gb

For Full Page Cache

  1. maxmemory 1gb

For Sessions

  1. save 9001
  2. save 30010
  3. save 6010000
  4. maxmemory 1gb

Setting up Multiple Redis Instances on a Single Magento Server

Setting up Multiple Redis Instances on a Single Magento Server

by Kirk Madera

Engineers reusable modular solutions to maintain environments and deployment strategies across teams. Works for food.

I had a bit of trouble figuring out a scalable way to set up multiple Redis instances on a single Magento server. There are a few posts (123) that were helpful, but included too much manual setup for my purposes. They all also required starting/stopping all redis instances individually.

I have set this up for both RedHat 6.5 and Ubuntu 13.x. This setup shares a Redis default config among all servers. Each new server only requires that a server specific configuration file be created with only four required settings. All redis instances may be started at once with

  1. service redis-server start

or each server may be started individually with

  1. service redis-server start server1

. This works will all service commands.

Install Redis and PHP Redis Client

RedHat

  1. yum install -y php54-pecl-redis redis;
  2. pecl install redis;
  3. echo'extension=redis.so'>/etc/php5/conf.d/redis.ini;
  4. service php5-fpm restart;#If you are running PHP-FPM

Ubuntu

  1. apt-get update;
  2. apt-get install -y php-pear php5-dev make redis-server;
  3. pecl install redis;
  4. echo'extension=redis.so'>/etc/php5/conf.d/redis.ini;
  5. service php5-fpm restart;#If you are running PHP-FPM


Set Up Your Init Script


  1. cd /etc/init.d;
  2. mv redis-server redis-server.bak;# Ubuntu only
  3. touch redis-server;
  4. chmod0755 redis-server;
  5. nano redis-server;

Paste these contents:
Redhat

  1. #!/bin/sh
  2. #
  3. # redis        init file for starting up the redis daemon
  4. #
  5. # chkconfig:   - 20 80
  6. # description: Starts and stops the redis daemon.
  7. if[-n "$2"]
  8. then
  9. NAME=$2
  10. # Source function library.
  11. ./etc/rc.d/init.d/functions
  12. exec="/usr/sbin/redis-server"
  13. pidfile="/var/run/redis/$NAME.pid"
  14. REDIS_CONFIG="/etc/redis/servers/$NAME.conf"
  15. [-e /etc/sysconfig/redis ]&&./etc/sysconfig/redis
  16. lockfile=/var/lock/subsys/redis
  17. start(){
  18.     [-f $REDIS_CONFIG]||exit6
  19.     [-x $exec]||exit5
  20.     echo-n $"Starting $NAME: "
  21.     daemon --user ${REDIS_USER-redis}"$exec$REDIS_CONFIG"
  22.     retval=$?
  23.     echo
  24.     [$retval-eq 0]&&touch$lockfile
  25.     return$retval
  26. }
  27. stop(){
  28.     echo-n $"Stopping $NAME: "
  29.     killproc -p $pidfile$NAME
  30.     retval=$?
  31.     echo
  32.     [$retval-eq 0]&& rm -f $lockfile
  33.     return$retval
  34. }
  35. restart(){
  36.     stop
  37.     start
  38. }
  39. reload(){
  40.     false
  41. }
  42. rh_status(){
  43.     status -p $pidfile$NAME
  44. }
  45. rh_status_q(){
  46.     rh_status >/dev/null2>&1
  47. }
  48.  
  49. case"$1" in
  50.     start)
  51.         rh_status_q &&exit0
  52.         $1
  53.         ;;
  54.     stop)
  55.         rh_status_q ||exit0
  56.         $1
  57.         ;;
  58.     restart)
  59.         $1
  60.         ;;
  61.     reload)
  62.         rh_status_q ||exit7
  63.         $1
  64.         ;;
  65.     force-reload)
  66.         force_reload
  67.         ;;
  68.     status)
  69.         rh_status
  70.         ;;
  71.     condrestart|try-restart)
  72.         rh_status_q ||exit0
  73.         restart
  74.         ;;
  75.     *)
  76.         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart}"
  77.         exit2
  78. esac
  79. exit $?
  80. else
  81.  
  82. FILES=/etc/redis/servers/*
  83. for f in $FILES
  84. do
  85. SERVERNAME=$(sed 's|/etc/redis/servers/||g' <<< $f)
  86. SERVERNAME=$(sed 's|.conf||g' <<< $SERVERNAME)
  87. /etc/init.d/redis-server "$1" "$SERVERNAME"
  88. done
  89.  
  90. fi
  91.  
  92. exit 0

  1. #!/bin/bash
  2. ### BEGIN INIT INFO
  3. # Provides:             redis-server
  4. # Required-Start:       $syslog $remote_fs
  5. # Required-Stop:        $syslog $remote_fs
  6. # Should-Start:         $local_fs
  7. # Should-Stop:          $local_fs
  8. # Default-Start:        2 3 4 5
  9. # Default-Stop:         0 1 6
  10. # Short-Description:    redis-server - Persistent key-value db
  11. # Description:          redis-server - Persistent key-value db
  12. ### END INIT INFO
  13.  
  14. if[-n "$2"]
  15. then
  16. NAME=$2
  17.  
  18. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  19. DAEMON=/usr/bin/redis-server
  20. DAEMON_ARGS=/etc/redis/servers/$NAME.conf
  21. DESC=redis-server
  22.  
  23. RUNDIR=/var/run/redis
  24. PIDFILE=$RUNDIR/$NAME.pid
  25.  
  26. test -x $DAEMON||exit0
  27.  
  28. set -e
  29.  
  30. case"$1" in
  31.   start)
  32.         echo-n "Starting $DESC: "
  33.         mkdir-p $RUNDIR
  34.         touch$PIDFILE
  35.         chown redis:redis $RUNDIR$PIDFILE
  36.         chmod755$RUNDIR
  37.         if start-stop-daemon --start --quiet --umask007--pidfile $PIDFILE--chuid redis:redis --exec$DAEMON--$DAEMON_ARGS
  38.         then
  39.                 echo"$NAME."
  40.         else
  41.                 echo"failed"
  42.         fi
  43.         ;;
  44.   stop)
  45.         echo-n "Stopping $DESC: "
  46.         if start-stop-daemon --stop --retry forever/QUIT/1--quiet --oknodo --pidfile $PIDFILE--exec$DAEMON
  47.         then
  48.                 echo"$NAME."
  49.         else
  50.                 echo"failed"
  51.         fi
  52.         rm -f $PIDFILE
  53.         ;;
  54.  
  55.   restart|force-reload)
  56.         ${0} stop $2
  57.         ${0} start $2
  58.         ;;
  59.  
  60.   status)
  61.         echo-n "$DESC is "
  62.         if start-stop-daemon --stop --quiet --signal 0--name ${NAME}--pidfile ${PIDFILE}
  63.         then
  64.                 echo"running"
  65.         else
  66.                 echo"not running"
  67.                 exit1
  68.         fi
  69.         ;;
  70.  
  71.   *)
  72.         echo"Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}">&2
  73.         exit1
  74.         ;;
  75. esac
  76.  
  77. else
  78.  
  79. FILES=/etc/redis/servers/*
  80. for f in $FILES
  81. do
  82. SERVERNAME=$(sed 's|/etc/redis/servers/||g' <<< $f)
  83. SERVERNAME=$(sed 's|.conf||g' <<< $SERVERNAME)
  84. /etc/init.d/redis-server "$1" "$SERVERNAME"
  85. done
  86.  
  87. fi
  88.  
  89. exit 0


Set Up Your Config

Default config file

  1. cd /etc/redis;
  2. mkdir servers;
  3. mv redis.conf servers/server1.conf;
  4. cd servers;
  5. nano server1.conf;

Each server file will look like this:

  1. # Redis server config
  2. include/etc/redis/redis.conf
  3.  
  4. # When running daemonized, Redis writes a pid file in /var/run/redis.pid by
  5. # default. You can specify a custom pid file location here.
  6. pidfile /var/run/redis/exampleproject-data.pid
  7.  
  8. # Accept connections on the specified port, default is 6379.
  9. # If port 0 is specified Redis will not listen on a TCP socket.
  10. port 6379
  11.  
  12. # Specify the log file name. Also 'stdout' can be used to force
  13. # Redis to log on the standard output. Note that if you use standard
  14. # output for logging but daemonize, logs will be sent to /prod/null
  15. logfile /var/log/redis/exampleproject-data.log
  16.  
  17. # The filename where to dump the DB
  18. dbfilename dump-exampleproject-data.rdb

Restart Redis instances:

  1. service redis-server restart;

Additional Servers

To create new servers, simply copy the server1.conf file to server2.conf and update all relevant strings to your new server name (in this case, server2). If your server name is unique enough, you can just run the file through sed. You'll also need to update the port manually to a new port.

  1. cd /etc/redis/servers;
  2. cp server1.conf server2.conf;
  3. sed -i s/server1/server2/g server2.conf;
  4. grep -hR ^port .|sort;# Determine the highest used port number
  5. nano server2.conf;#Update the port number

Start the new Redis instance(s):

  1. service redis-server start;

Magento Specific Adjustments

Run this command as root, per Fabrizio Branca's Redis Blog Post:

  1. echo"vm.overcommit_memory = 1">/etc/sysctl.conf; sysctl vm.overcommit_memory=1;

Add these settings to your Redis server configuration files if using Redis for Magento.

For Data Cache

  1. maxmemory 1gb

For Full Page Cache

  1. maxmemory 1gb

For Sessions

  1. save 9001
  2. save 30010
  3. save 6010000
  4. maxmemory 1gb