Setting up multiple Redis instances on the same server is pretty easy, but if you want to be able to easily start/stop and restart instances, you’ll need to play with the init scripts of redis-server.
I needed this to be able to offer Redis buckets to different customers on a shared platform.
This is how I managed to set up multiple instances on the same server.
Since installing redis-server is out of the scope of this article, I’ll only explain what I did to manage multiple Redis Servers.
* Setting up a new INIT script:
1 2 3 4 5 |
cd /etc/init.d mv redis-server ~/redis-server.bak touch redis-server chmod 755 redis-server vim redis-server |
* Paste this into the new redis-server init script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
#!/bin/sh ### BEGIN INIT INFO # Provides: redis-server # Required-Start: $syslog $remote_fs # Required-Stop: $syslog $remote_fs # Should-Start: $local_fs # Should-Stop: $local_fs # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: redis-server - Persistent key-value db # Description: redis-server - Persistent key-value db ### END INIT INFO if [ "$#" -eq 2 ] then NAME=$2 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DAEMON=/usr/bin/redis-server DAEMON_ARGS=/etc/redis/servers/$NAME.conf DESC=redis-server RUNDIR=/var/run/redis PIDFILE=$RUNDIR/$NAME.pid echo "Managing Redis server with the following attributes:" echo " Daemon Args: $DAEMON_ARGS" echo " Pidfile: $PIDFILE" test -x $DAEMON || exit 0 #if [ -r /etc/default/$NAME ] #then # . /etc/default/$NAME #fi . /lib/lsb/init-functions set -e case "$1" in start) echo -n "Starting $DESC: " mkdir -p $RUNDIR touch $PIDFILE chown redis:redis $RUNDIR $PIDFILE chmod 755 $RUNDIR if [ -n "$ULIMIT" ] then ulimit -n $ULIMIT fi if start-stop-daemon --start --quiet --umask 007 --pidfile $PIDFILE --chuid redis:redis --exec $DAEMON -- $DAEMON_ARGS then echo "$NAME." else echo "failed" fi ;; stop) echo -n "Stopping $DESC: " if start-stop-daemon --stop --retry forever/QUIT/1 --oknodo --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_ARGS then echo "$NAME." else echo "failed" fi rm -f $PIDFILE sleep 1 ;; restart|force-reload) ${0} stop $2 ${0} start $2 ;; status) echo -n "$DESC is " if start-stop-daemon --stop --quiet --signal 0 --name ${NAME} --pidfile ${PIDFILE} then echo "running" else echo "not running" exit 1 fi ;; *) echo "Usage: /etc/init.d/redis-server/$NAME {start|stop|restart|force-reload|status}" >&2 exit 1 ;; esac else FILES=/etc/redis/servers/* for f in $FILES do echo $f SERVERNAME=`echo ${f##*/}` SERVERNAME=${SERVERNAME%.conf} /etc/init.d/redis-server "$1" "$SERVERNAME" done fi exit 0 |
Once that is done, we need to add multiple config files for our different buckets:
1 2 3 |
mkdir /etc/redis/servers cd /etc/redis/servers vim www.nicovs.be.conf |
And enter some config settings looking like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#Include the default redis settings include /etc/redis/redis.conf #Only enable 1 Database for this instance, not the default 16 (or change the default in /etc/redis/redis.conf to 1 databases 1 #Max amount of RAM this instance can use: maxmemory 100mb #If no TTL is being set by the application using this instance, this policy will evict all keys by an approximated LRU algorithm as long as we hit the memory limit. maxmemory-policy allkeys-lru #Placeholder to be able to save the DB to an rdb dump file. #save 900 1 #save 300 10 #save 60 10000 #Pidfile pidfile /var/run/redis/www.nicovs.be.pid #Which Port to use for this instance port 5033 #Where to log the output of this instance logfile /var/log/redis/www.nicovs.be.log # The RDB Dump base dir dir /backups/redis #The RDP Dump filename dbfilename www.nicovs.be.rdb ## Security settings: # Disable the config command, so that customers are not able to change the server config from command line. rename-command CONFIG "" requirepass aVery_Long_!1290-PasSwooo00Ord_ToProtectBruteForce |
Creating new instances is easily done by copying this initial conf file and adjusting the params (port, name, pid, savefilename, password,…)
Now, if you want to start/stop or restart all redis instances at the same time, this can be done with the normal service command:
1 2 3 4 |
service redis-server start service redis-server stop service redis-server restart service redis-server status |
The new init script we create above, will simply index all config instances from /etc/redis/servers and manage those 1 at a time.
Or, if you only want to manage 1 instance at a time, just do something like this for example:
1 |
service redis-server restart www.nicovs.be |
which will restart only the instance running for website www.nicovs.be
Since we use this on a shared environment, we have a provisioning system in place, where our customers can change some settings
This article is based on: Robofirm: Setting up multiple redis instances, but I had to change some configuration to make it work. Anyway, a big thanks to Kirk Madera for pointing me in the right direction!