Memcached Not Working in Magento: Troubleshooting Guide
The Memcached Setup Scenario
You’ve set up a robust e-commerce infrastructure on AWS using Scalr.net, running Magento CE 1.7 across multiple servers including two Apache web servers and one dedicated database server. Your architecture also includes an Nginx load balancer for efficient resource distribution and a single Memcached server to handle file caches and session caches.
Your local.xml configuration file appears correctly configured:
<session_save><![CDATA[memcache]]></session_cache_lifetime>
<session_save_path><![CDATA[tcp://XX.XX.XX.XX:11211?persistent=1&weight=2&timeout=10&retry_interval=10]]></session_cache_lifetime>
<session_cache_limiter><![CDATA[]]></session_cache_limiter>
<cache>
<backend>memcached</backend>
<slow_backend>database</slow_backend>
<slow_backend_store_data><![CDATA[]]></slow_backend_store_data>
<auto_refresh_fast_cache><![CDATA[]]></auto_refresh_fast_cache>
<lifetime>259200</lifetime>
<backend_options>
<servers>
<server>
<host><![CDATA[XX.XX.XX.XX]]></host>
<port><![CDATA[11211]]></port>
<persistent><![CDATA[0]]></persistent>
<weight><![CDATA[2]]></weight>
<timeout><![CDATA[10]]></timeout>
<retry_interval><![CDATA[10]]></retry_interval>
<status><![CDATA[1]]></status>
</server>
</servers>
<compression><![CDATA[0]]></compression>
</backend_options>
</cache>
Your Memcached server appears to be running and accepting connections, as shown by the stats command output:
STAT pid 1086
STAT uptime 79364
STAT time 1352912987
STAT version 1.4.2
STAT pointer_size 64
STAT rusage_user 2.340000
STAT rusage_system 0.000000
STAT curr_connections 5
STAT total_connections 11
STAT connection_structures 6
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT get_hits 0
STAT get_misses 0
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT bytes_read 28
STAT bytes_written 2194
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT evictions 0
END
However, despite the apparent setup correctness, you’re encountering two significant issues:
Error Message Analysis
Your Magento frontend is throwing errors related to cache directory configuration:
a:4:{i:0;s:29:"cache_dir must be a directory";i:1;s:1097:"#0 /var/www/includes/src/Zend_Cache_Backend_File.php(154): Zend_Cache::throwException('cache_dir must ...')
#1 /var/www/includes/src/Zend_Cache_Backend_File.php(121): Zend_Cache_Backend_File->setCacheDir('')
#2 /var/www/includes/src/__default.php(60504): Zend_Cache_Backend_File->__construct(Array)
#3 /var/www/includes/src/__default.php(60445): Zend_Cache::_makeBackend('File', Array, true, true)
#4 /var/www/includes/src/Mage_Core_Model_Cache.php(124): Zend_Cache::factory('Varien_Cache_Co...', 'File', Array, Array, true, true, true)
#5 /var/www/includes/src/__default.php(23463): Mage_Core_Model_Cache->__construct(Array)
#6 /var/www/app/Mage.php(462): Mage_Core_Model_Config->getModelInstance('core/cache', Array)
#7 /var/www/includes/src/__default.php(20134): Mage::getModel('core/cache', Array)
#8 /var/www/includes/src/__default.php(20028): Mage_Core_Model_App->_initCache(Array)
#9 /var/www/includes/src/__default.php(20070): Mage_Core_Model_App->baseInit(Array)
#10 /var/www/app/Mage.php(683): Mage_Core_Model_App->run(Array)
#11 /var/www/index.php(87): Mage::run('', 'store')
#12 {main}";s:3:"url";s:1:"/";s:11:"script_name";s:10:"/index.php";}
This suggests that while the Memcached backend is being referenced, the cache is trying to fall back to a file-based cache with an undefined directory.
The Cache Directory Issue
When you add the following lines in the backend_options section of your local.xml configuration:
<cache_dir><![CDATA[]]></cache_dir>
<hashed_directory_level><![CDATA[]]></hashed_directory_level>
<hashed_directory_umask><![CDATA[]]></hashed_directory_umask>
<file_name_prefix><![CDATA[]]></file_name_prefix>
The error disappears, but this doesn’t resolve the actual Memcached functionality.
The Core Problem: Missing memcache_api Extension
Upon deeper investigation, it appears that neither your Memcached server nor your Magento servers have the necessary memcache_api extension installed. This is a critical component required for PHP to interact with Memcached.
To verify this, open a terminal on one of your Magento servers and run:
php -i | grep memcache
If no results are returned or if you see “memcache” => enabled => 0, the extension is missing. In either case, you need to install it immediately.
The Solution: Installing memcache_api Extension
You can install the memcache_api extension using PECL (PHP Extension Community Library). Here’s how:
- Connect to your server via SSH.
- Run the following command:
sudo pecl install memcache
- After the installation is complete, copy the generated configuration file to your PHP configuration directory:
sudo cp /etc/php.d/memcache.ini /etc/php.d/70-memcache.ini
- Restart your web server and Memcached service for the changes to take effect.
Verifying Memcached Functionality
Re-run the stats command on your Memcached server:
stats
You should now see increased activity in the cmd_get, cmd_set, get_hits, and get_misses statistics, indicating that PHP is effectively communicating with Memcached through the installed extension.
Conclusion
The key to resolving this issue was identifying the missing memcache_api extension. This demonstrates the importance of ensuring all necessary dependencies are installed before configuring any complex caching architecture in a production environment.
Remember, while Memcached can significantly enhance performance, it requires careful configuration and monitoring. Regularly review your cache settings and server statistics to ensure optimal utilization.
For more detailed guides on Magento performance optimization and AWS architecture best practices, check out our Magento Performance Tuning Guide and AWS Architecture Best Practices for E-commerce.