Redis Cache can be used as both a backend cache and a session store for Magento e-Commerce Sites bringing significant speed improvements for your web store.
Probably the single biggest improvement you can make to a Magento installation is implementation of a fast cache backend for both backend cache files and session storage, there are many cache implementations you can choose from including APC & Memcache however for the purposes of this blog we will focus on using Redis Cache for both Sessions and Backend Cache file storage.
Please note the following article deals with Magento CE only and not Magento Enterprise Edition and was tested and based on Magento CE Version 22.214.171.124.
Installing Redis Cache
The Redis Cache itself is simple to install on any LAMP server we have not included instructions here for installation of Redis Cache itself as full instructions are available on the Redis Website, and we suggest you use these as they will contain the latest information and documentation regarding the installation of the Redis Cache package on your specific server.
Also because Redis can be implemented in the cloud as seperate server or cloud instance as with Amazon’s AWS etc, we have included the IP address for LocalHost in all configuration files and examples, for cloud installs this would need to be modified to that of your Redis Server.
Implementing Redis Session Storage for Magento
Magento has by default a module installed for implementing session storage on Redis, however this module is disable by default and needs to be enabled prior to use, to¬†enable the CM-RedisSession module within magento open the¬†magento-install-dir/app/etc/modules/Cm_RedisSession.xml in a text editor and change the value of the <active> tag to true before saving.
Additionally you will need to modify your magento config file (magento-install-dir/app/etc/local.xml) to tell Magento which Redis database to use for sessions etc see below:
<session_save>db</session_save> <redis_session> <host>127.0.0.1</host> <port>6379</port> <password></password> <timeout>2.5</timeout> <persistent>sess-db0</persistent> <db>0</db> <compression_threshold>2048</compression_threshold> <compression_lib>gzip</compression_lib> <log_level>1</log_level> <max_concurrency>6</max_concurrency> <break_after_frontend>5</break_after_frontend> <break_after_adminhtml>30</break_after_adminhtml> <bot_lifetime>7200</bot_lifetime> </redis_session>
You will notice we have highlighted the <db> tag in the above XML configuration for Redis, the reason for this is that you will need to ensure that if you are running multiple implementations of Magento on a single server then¬†each implementation will need 2 seperate Redis databases (one for Sessions and one for the backend cache), If you do not configure each installation to use a seperate Redis database then there is a significant possibility that the cache will return incorrect results.
We also recommend implementing two Redis databases to split sessions from the main backend Magento cache in order to allow the backend cache to be flushed completely without destroying the session data, and vice versa.
There is a wealth of information on Cm_RedisSession configuration on GitHub , along with the latest version of the Cm_RedisSession module source code.
Once you have got Redis Sessions installed and configured it is possible to migrate the existing session from your disk or database sessions storage into your new Redis cache sessions database.
There are two procedures for migrating sessions, which one you use is down to which previous session storage method you were using.
The migration procedure for file based session storage is:
cd /path/to/magento/root touch maintenance.flag sleep 10 wget https://raw2.github.com/colinmollenhour/Cm_RedisSession/master/migrateSessions.php php migrateSessions.php -y rm maintenance.flag
And for database session storage:
cd /path/to/magento/root touch maintenance.flag sleep 10 wget https://raw2.github.com/colinmollenhour/Cm_RedisSession/master/migrateSessions_mysql_redis.php php migrateSessions_mysql_redis.php -y rm maintenance.flag
Once you have completed implementation of your Magento sessions on Redis, you can clear both the /var/sessions directory within your public_html folder and also the core_sessions table in your database, and both should remain empty.
Installing the Redis Cache Backend ¬†for Magento
The backend Redis Cache for Magento is implemented in much the same way as the sessions cache however there is at time of writing no built in Magento module to support Redis Cache backend for Magento, however Cm_Cache_Backend_Redis is available via GitHub¬†and can be installed manually by following the simple instructions included with the module.
You will again need to modify the local.xml within your Magento installation, the following is an example of the cache entry you will need to add to the local.xml file
<cache> <backend>Cm_Cache_Backend_Redis</backend> <backend_options> <server>127.0.0.1</server> <port>6379</port> <persistent></persistent> <database>1</database> <password></password> <force_standalone>0</force_standalone> <connect_retries>1</connect_retries> <read_timeout>10</read_timeout> <automatic_cleaning_factor>0</automatic_cleaning_factor> <compress_data>1</compress_data> <compress_tags>1</compress_tags> <compress_threshold>20480</compress_threshold> <compression_lib>gzip</compression_lib> <use_lua>0</use_lua> </backend_options> </cache>
Again, we have highlighted the database number configuration option¬†this will need to be set to a unique number on¬†your server, to ensure that each implementation running on the server uses a totally seperate Redis database.
Once installed, you will need to flush the Magento Cache to force¬†it to start using the new Redis cache immediately.
Monitoring and Managing the Redis Backend & Session Cache
As a quick check that redis is running and working you can you the following command via a SSH terminal command line to check that Redis is running and working.
> redis-cli MONITOROK 1434959408.731767 "MONITOR" 1434959408.745281 (db 11) "HGET" "zc:k:13e_Zend_LocaleC_en_GB_time_" "d" 1434959408.745838 (db 11) "HGET" "zc:k:13e_Zend_LocaleC_de_time_" "d" 1434959408.748487 (db 11) "HGET" "zc:k:13e_Zend_LocaleC_en_GB_currencynumber_" "d" 1434959408.748680 (db 11) "HGET" "zc:k:13e_Zend_LocaleL_en_GB_symbols_" "d" 1434959408.785205 (db 11) "HGET" "zc:k:13e_Zend_LocaleC_en_GB_time_" "d" 1434959408.785665 (db 11) "HGET" "zc:k:13e_Zend_LocaleC_de_time_" "d" 1434959408.788057 (db 11) "HGET" "zc:k:13e_Zend_LocaleC_en_GB_currencynumber_" "d"
When you issue this command on the SSH command line you will see all instructions that pass through the Redis cache, to exit the Redis monitor stream use ctrl-c to exit.
It is also possible to quickly and easily flush the Redis Cache database via the command line if needed via the following command.
> redis-cli FLUSHALL
Obviously this type of command line monitoring does not tell you a lot more than just that the Redis Cache is ¬†running and accepting commands, what you really need to see is whats in the Redis Cache databases that we have created, for this we can use phpRedisAdmin which is available from¬†GitHub.
phpRedisAdmin is simple to install and configure, with full installation instructions provided on the GitHub page, so we wont go into the installation here, however given we have been dealing with a multiple Redis Cache database install above, then I have included a sample config file below to assist you in setting up phpRedisAdmin once you have it installed.
'servers' => array( array( 'name' => 'Sessions', 'host' => '127.0.0.1', 'port' => 6379, 'db' => 0, // Optional database number, see http://redis.io/commands/select 'filter' => '*' // Show only parts of database for speed or security reasons ), array( 'name' => 'Backend', 'host' => '127.0.0.1', 'port' => 6379, 'db' => 1, // Optional database number, see http://redis.io/commands/select 'filter' => '*' // Show only parts of database for speed or security reasons ) ),
Above is the servers section of the phpRedisAdmin includes/config.inc.php¬†file, this array in the config file controls which databases and servers you will see in the admin panel, the above file shows databases 0 & 1 (highlighted in red in the config) for the two databases we have configured.
We recommend that you install the phpRedisAdmin code on a sub¬†domain or seperate management domain to that of your main site¬†so that it is not accessible via your actual production website, and also that you password protect the installation either via .htaccess or via the inbuilt password authorisation procedure within phpRedisAdmin (this can be set in the config file).
Cleaning up the Magento Redis Cache Databases
As above if you need to clear the Redis Cache database quickly then you can issue a flush command via the SSH Command line
> redis-cli FLUSHALL
Or you can manually remove single keys via the¬†phpRedisAdmin app discussed above, however it is also a good idea to run systematic maintenance on the Redis Cache database
In addition to the above manual methods of cleaning either all or parts of the Redis Cache you can also implement a little housekeeping via a cron job, there is a good example of this on the AtWix website with full details of a Scheduled Redis Cache Clean Up cron module for Magento, whilst this appears to be a good module for doing a total Redis Cache flush.
We would not 100% recommend the total cache flush¬†approach to cleaning the Redis Cache as it means the cache would need to be fully rebuilt after each flush causing a heavy server load during the initial build phase when the cache is totally empty, much better would be to flush the cache based on age and the TTL of the stored keys within Redis, the cm_redis_tools implementation on GitHub allows you to schedule cache cleaning based on clearing old keys, or matched tags etc. the cm_redis_tools script also allows you to use cron to schedule cleaning¬†on specific Redis databases.
Another option is to run a Magento garbage collection script via a cron job, to do this create the file ./shell/redisgarbage.php containing the following script.
<?php count($argc) or die('<pre>:P</pre>'); ini_set('memory_limit','1024M'); set_time_limit(0); error_reporting(E_ALL | E_STRICT); require_once '../app/Mage.php'; Mage::app()->getCache()->getBackend()->clean('old'); ?>
And then add the following cron job to your server to execute the job at a timed interval in this case every four hours.
0 */4 * * */usr/bin/php /home/yourpath/public_html/shell/redisgarbage.php
If you want to have the script run silenty without any feebback from the cron manager then add the following to the end of the command
We think the above information covers 99% of what you need to know regarding using Redis Cache with Magento CE, if you have any questions or require any clarification, then please comment below and we will do our best to answer any questions you may have.