Magento Optimizations

Last Updated December 2014

Pagoda Box has several optimization tools available to help you get the most out of your Magento application. After reading this doc, you should be familiar with:

  • Installing a bytecode caching extension
  • Configuring Magento to use Redis
  • Other optimizations to improve Magento's performance

Use this Doc with the Magento Doc

This doc is intended to be used in conjunction with the Magento Configuration doc. If you don't have Magento up and running yet, start there first.

Bytecode Caching

Bytecode caching stores the code generated by PHP so it doesn't have to parse and compile scripts on every request. APC, eAccelerator, OPCache, and XCache are bytecode caching extensions available on Pagoda Box. Which one you use depends on what version of PHP you're using and personal preference. Each is added as a php_extension or zend_extension in your Boxfile. What extensions are available to each version of PHP can be found in the PHP Extensions doc.

Simply add your preferred opcode cacher as a php_extension or zend_extension in your Boxfile and deploy. No additional configuration is required.

Enabling OpCode Cachers in the Boxfile YAML

/Boxfile

Redis Sessions & Cache

Redis is an advanced key-value store that makes for an extremely powerful caching mechanism and/or session store. We highly recommend using Redis to store sessions and caches instead of storing them in network directories or in your database.

Add a Redis Service

Redis can be added via your Boxfile or the dashboard. In either case, you'll need to add the "redis" php extension to your web service. This is the Redis/PHP adapter that will allow your PHP process to communicate with Redis.

Adding a Redis service in the Boxfile YAML

/Boxfile

Redis Sessions

To store Magento sessions in Redis, modify the session handler section of your local.xml, and set the session save path as follows:

Redis Session Save Path in local.xml XML

  # These are not your Redis connection credentials.
  # Use the Redis connection credentials found in your dashboard.

  <![CDATA[redis]]>
  <![CDATA[tcp://192.168.0.3:6379]]>
/pagoda/local.xml

Note: If you using the technique outlined in the Magento Configuration doc of using two local.xml's, note that only changes to pagoda/local.xml will be reflected in your app on Pagoda Box.

Next, add Redis support to Magento by overriding the core Magento file:

app/code/core/Mage/Core/Model/Session/Abstract/Varien.php

Simply copy the file and create your own local version at:

app/code/local/Mage/Core/Model/Session/Abstract/Varien.php.

In the new file, around line 62, add the following:

Setting the Magento Session Save Handler PHP

/app/code/local/Mage/Core/Model/Session/Abstract/Varien.php

Magento is now configured to use Redis as a session store.

Redis Cache

To use Redis as a Magento cache, add the Credis library and Cm Module. The module consists of one file, which you can download here. Although the Cm module author recommends installing via modman, modman doesn’t correctly register all submodules with Pagoda Box. Instead, save the file as: app/code/community/Cm/Cache/Backend/Redis.php.

Add the Credis Library to your Project

Credis is a lightweight PHP/Redis client. You'll want to add it to your project either as a Git submodule or, if you're using Composer, as a Composer dependency.

Edit Your Magento Cache Declaration

Edit the cache delcaration and its contents in your local.xml.

Changing Magento's Cache Declaration XML

/pagoda/local.xml

For <server> and <port>, be sure to use the host and port for your Redis service provided in your dashboard.

Also notice that <database> is set to 1 in the example. Redis allows up to 16 databases within a single Redis service. Setting the cache database as 1 (or any other number) will isolate cache records from your sessions. This isn't necessary, but does help to keep things organized.

Set Far-Future Expires

To drastically reduce the number of requests per visitor as they browse your site, set far future expires. This should be done after finishing front-end theme work. Adding the following httpd_static_expire directive to the web1 section of your Boxfile will expire static asset caches after 1 year.

Setting Static Expire in the Boxfile YAML

/Boxfile

Go ahead and deploy your changes. Once they're deployed, you'll need to flush your Magento cache. Log into your Magento admin and go to System > Cache Management, then click "Flush Magento Cache".

Flush Magento's Cache

Verify Redis is Working

Now that Magento is configured for Redis, verify that everything is working. Browse and update your site to create sessions and cached data in your Redis service. Enable SSH access on your Redis service and either use the Pagoda Tunnel or setup SSH port forwarding from your local machine. Then use the Redis-CLI to connect. Once connected, run the following command to display a list of all stored keys.

Connecting To & Displaying Keys in Redis Terminal

  # Use the Pagoda CLI to open a Tunnel to Redis
  $ pagoda tunnel cache1

  # In a separate terminal session, connect using the redis-cli
  $ redis-cli -h 127.0.0.1 -p 6379

  # List all keys
  $ keys *

You should see a list of assorted keys. If you see session keys only, cache keys only, or if you don’t see any keys at all, review your configuration settings and ensure you’ve cleared the Magento cache.

For a full list of useful commands when using Redis, see their official documentation.

Magento Settings

Avoid Storing JS & CSS in Network Storage

Magento has a few optimization utilities that merge javascript and css files, then place them directories that are typically specified as network directories. Most of the time, that’s a non-issue, but at high traffic volume, Pagoda Box can access files from web instances much faster than files in network storage. For high traffic performance, keeping javascript and css in the repo outperforms minification.

To turn file merging off, log into your Magento admin panel, then navigate to: System > Configuration > Developer.

Choose “No” under JavaScript Settings and CSS Settings as follows:

Disable Unnecessary Logs

Magento has a few logging utilities that can provide helpful information, but also dramatically increase the number of database reads and writes.

If you don't need the System & Exception logs, disable them by going to System > Configuration > Developer > Log Settings and change "Enabled" to "No".

If you don't need the user browsing activity logs, disable some or all of them by adding the following code to your app/etc/local.xml file.

Disabling User Activity Logging XML

/pagoda/local.xml

Enable All Default Caching Options

This should go without saying, but ensure you've enabled all the default caching options available at System > Cache Management.

Disable Unused Modules

Disable any unused Modules at System > Configuration > Advanced > Advanced.

Expires Browser Caching

You can increase the number of files browsers cache by adding the following code to the .htaccess file in the root of your application.

Expires Browser Caching Apache

/.htaccess

Scale Server Resources

After you've optimized your application and environment as much as possible, it's time to scale your server resources. Since Magento is extremely database intensive, DB RAM will most likely be the first place you'll want to start. Large catalog/high traffic stores will most likely require a dedicated database.

If you have any questions, suggestions, or corrections, let us know.