{"id":382,"date":"2015-12-11T10:09:26","date_gmt":"2015-12-11T10:09:26","guid":{"rendered":"http:\/\/www.nicovs.be\/?p=382"},"modified":"2015-12-11T10:09:26","modified_gmt":"2015-12-11T10:09:26","slug":"running-multiple-redis-instances-on-the-same-server","status":"publish","type":"post","link":"https:\/\/www.nicovs.be\/?p=382","title":{"rendered":"Running multiple Redis instances on the same server."},"content":{"rendered":"<p>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&#8217;ll need to play with the init scripts of redis-server.<\/p>\n<p>I needed this to be able to offer Redis buckets to different customers on a shared platform.<\/p>\n<p>This is how I managed to set up multiple instances on the same server.<br \/>\nSince installing redis-server is out of the scope of this article, I&#8217;ll only explain what I did to manage multiple Redis Servers.<\/p>\n<p>* Setting up a new INIT script:<\/p>\n<pre class=\"lang:zsh decode:true \" >cd \/etc\/init.d\r\nmv redis-server ~\/redis-server.bak\r\ntouch redis-server\r\nchmod 755 redis-server\r\nvim redis-server<\/pre>\n<p>* Paste this into the new redis-server init script:<\/p>\n<pre class=\"lang:sh decode:true \" >#!\/bin\/sh\r\n### BEGIN INIT INFO\r\n# Provides:             redis-server\r\n# Required-Start:       $syslog $remote_fs\r\n# Required-Stop:        $syslog $remote_fs\r\n# Should-Start:         $local_fs\r\n# Should-Stop:          $local_fs\r\n# Default-Start:        2 3 4 5\r\n# Default-Stop:         0 1 6\r\n# Short-Description:    redis-server - Persistent key-value db\r\n# Description:          redis-server - Persistent key-value db\r\n### END INIT INFO\r\n\r\nif [ \"$#\" -eq 2 ]\r\nthen\r\nNAME=$2\r\n\r\nPATH=\/usr\/local\/sbin:\/usr\/local\/bin:\/sbin:\/bin:\/usr\/sbin:\/usr\/bin\r\nDAEMON=\/usr\/bin\/redis-server\r\nDAEMON_ARGS=\/etc\/redis\/servers\/$NAME.conf\r\nDESC=redis-server\r\n\r\nRUNDIR=\/var\/run\/redis\r\nPIDFILE=$RUNDIR\/$NAME.pid\r\n\r\necho \"Managing Redis server with the following attributes:\"\r\necho \"   Daemon Args: $DAEMON_ARGS\"\r\necho \"   Pidfile: $PIDFILE\"\r\n\r\ntest -x $DAEMON || exit 0\r\n\r\n#if [ -r \/etc\/default\/$NAME ]\r\n#then\r\n#       . \/etc\/default\/$NAME\r\n#fi\r\n\r\n. \/lib\/lsb\/init-functions\r\n\r\nset -e\r\n\r\ncase \"$1\" in\r\n  start)\r\n        echo -n \"Starting $DESC: \"\r\n        mkdir -p $RUNDIR\r\n        touch $PIDFILE\r\n        chown redis:redis $RUNDIR $PIDFILE\r\n        chmod 755 $RUNDIR\r\n\r\n        if [ -n \"$ULIMIT\" ]\r\n        then\r\n                ulimit -n $ULIMIT\r\n        fi\r\n\r\n        if start-stop-daemon --start --quiet --umask 007 --pidfile $PIDFILE --chuid redis:redis --exec $DAEMON -- $DAEMON_ARGS\r\n        then\r\n                echo \"$NAME.\"\r\n        else\r\n                echo \"failed\"\r\n        fi\r\n        ;;\r\n  stop)\r\n        echo -n \"Stopping $DESC: \"\r\n        if start-stop-daemon --stop --retry forever\/QUIT\/1  --oknodo --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_ARGS\r\n        then\r\n                echo \"$NAME.\"\r\n        else\r\n                echo \"failed\"\r\n        fi\r\n        rm -f $PIDFILE\r\n        sleep 1\r\n        ;;\r\n\r\n  restart|force-reload)\r\n        ${0} stop $2\r\n        ${0} start $2\r\n        ;;\r\n\r\n  status)\r\n        echo -n \"$DESC is \"\r\n        if start-stop-daemon --stop --quiet --signal 0 --name ${NAME} --pidfile ${PIDFILE}\r\n        then\r\n                echo \"running\"\r\n        else\r\n                echo \"not running\"\r\n                exit 1\r\n        fi\r\n        ;;\r\n\r\n  *)\r\n        echo \"Usage: \/etc\/init.d\/redis-server\/$NAME {start|stop|restart|force-reload|status}\" &gt;&amp;2\r\n        exit 1\r\n        ;;\r\nesac\r\n\r\nelse\r\nFILES=\/etc\/redis\/servers\/*\r\nfor f in $FILES\r\ndo\r\n  echo $f\r\n  SERVERNAME=`echo ${f##*\/}`\r\n  SERVERNAME=${SERVERNAME%.conf}\r\n  \/etc\/init.d\/redis-server \"$1\" \"$SERVERNAME\"\r\ndone\r\nfi\r\n\r\nexit 0<\/pre>\n<p>Once that is done, we need to add multiple config files for our different buckets:<\/p>\n<pre class=\"lang:zsh decode:true \" >mkdir \/etc\/redis\/servers\r\ncd \/etc\/redis\/servers\r\nvim www.nicovs.be.conf\r\n<\/pre>\n<p>And enter some config settings looking like this:<\/p>\n<pre class=\"lang:sh decode:true \" >#Include the default redis settings\r\ninclude \/etc\/redis\/redis.conf\r\n\r\n#Only enable 1 Database for this instance, not the default 16 (or change the default in \/etc\/redis\/redis.conf to 1\r\ndatabases 1\r\n\r\n#Max amount of RAM this instance can use:\r\nmaxmemory 100mb\r\n\r\n#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.\r\nmaxmemory-policy allkeys-lru\r\n\r\n#Placeholder to be able to save the DB to an rdb dump file.\r\n#save 900 1\r\n#save 300 10\r\n#save 60 10000\r\n\r\n#Pidfile\r\npidfile \/var\/run\/redis\/www.nicovs.be.pid\r\n#Which Port to use for this instance\r\nport 5033\r\n#Where to log the output of this instance\r\nlogfile \/var\/log\/redis\/www.nicovs.be.log\r\n\r\n# The RDB Dump base dir\r\ndir \/backups\/redis\r\n#The RDP Dump filename\r\ndbfilename www.nicovs.be.rdb\r\n\r\n## Security settings:\r\n# Disable the config command, so that customers are not able to change the server config from command line.\r\nrename-command CONFIG \"\"\r\n\r\nrequirepass aVery_Long_!1290-PasSwooo00Ord_ToProtectBruteForce<\/pre>\n<p>Creating new instances is easily done by copying this initial conf file and adjusting the params (port, name, pid, savefilename, password,&#8230;)<\/p>\n<p>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:<\/p>\n<pre class=\"lang:default decode:true \" >service redis-server start\r\nservice redis-server stop\r\nservice redis-server restart\r\nservice redis-server status<\/pre>\n<p>The new init script we create above, will simply index all config instances from \/etc\/redis\/servers and manage those 1 at a time.<\/p>\n<p>Or, if you only want to manage 1 instance at a time, just do something like this for example:<\/p>\n<pre class=\"lang:sh decode:true \" >service redis-server restart www.nicovs.be<\/pre>\n<p>which will restart only the instance running for website www.nicovs.be<\/p>\n<p>Since we use this on a shared environment, we have a provisioning system in place, where our customers can change some settings<\/p>\n<p>This article is based on: <a href=\"http:\/\/robofirm.com\/insights\/2014\/04\/setting-up-multiple-redis-instances-on-a-single-magento-server\/\" target=\"_blank\">Robofirm: Setting up multiple redis instances<\/a>, 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!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[1],"tags":[],"class_list":["post-382","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/posts\/382","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=382"}],"version-history":[{"count":3,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/posts\/382\/revisions"}],"predecessor-version":[{"id":385,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/posts\/382\/revisions\/385"}],"wp:attachment":[{"href":"https:\/\/www.nicovs.be\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}